-4

I have looked at other questions regarding this error, but I so far have not found what looked like the answer to my problem there. I have two classes Message and ColorString. In a method of the former I make several instances of the latter by passing members of Message to the constructor of ColorString

message.hpp

#ifndef __MESSAGE__HPP
#define __MESSAGE__HPP
#if defined __linux || defined __APPLE__
#define UNIXLIKE
#endif
////////////////////////////////////////////////////////////////////////////////
// Message helps organize error and warning messages                          //
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <sstream>
#include <fstream>
#include "color.hpp"
////////////////////////////////////////////////////////////////////////////////
//                                  MESSSAGE                                  //
////////////////////////////////////////////////////////////////////////////////
class Message
{
////////////////////////////////////////////////////////////////////////////////
public: // types
////////////////////////////////////////////////////////////////////////////////
    typedef COLOR::ColorString                                           colstr;
    typedef COLOR::ColorID                                                color;
////////////////////////////////////////////////////////////////////////////////
public: // methods
////////////////////////////////////////////////////////////////////////////////
    Message(std::ostream& o = std::cerr)
    : o(std::cerr)
    {

    }
////////////////////////////////////////////////////////////////////////////////
    Message(const std::string& message,
        const std::string& label ="",
        const std::string file="",
        const int line = -1,
        std::ostream& o = std::cerr,
        const color c = COLOR::RED
        )
    : o(std::cerr)
    {

    }
////////////////////////////////////////////////////////////////////////////////
    friend std::ostream& operator<<(std::ostream& o, Message& m)
    {
        #ifdef UNIXLIKE
        colstr lbl(label, c);
        colstr msg(message);
        colstr ln(linestr);
        colstr fl(file);
        #else
    std::string lbl(label);
        std::string msg(message);
        std::string ln(linestr);
        std::string fl(file);
        #endif
    o << fl << ln;
    if (fl.size() > 0 || ln.size() > 0) o << ": ";
    o << lbl << " " << msg << "\n";
    o.flush();
    return o;
    }
////////////////////////////////////////////////////////////////////////////////
private: // methods
////////////////////////////////////////////////////////////////////////////////
    void init(const std::string& message,
          const std::string& label = "",
          const std::string file="",
          const int line = -1,
          std::ostream& o = std::cerr,
          const color c = COLOR::RED)
    {
    this->message = message;
    this->label = label;
    this->file = file;
    this->line = line;
    this->c = c;

    if (this->line > -1)
    {
        std::stringstream ss;
        ss << this->line;
        ss >> linestr;
    }

    if (this->file.size() > 0)
    {
        this->file = this->file+":";
        if (this->line > -1)
        {
        this->file = this->file+linestr; linestr="";
        }
    }
    }
////////////////////////////////////////////////////////////////////////////////
private : // fields
////////////////////////////////////////////////////////////////////////////////
    std::string label;
    std::string message;
    std::string file;
    int line;
    std::ostream& o;
    color c;
    std::string linestr;
};
#endif

The constructor of ColorString looks like this:

/**
 * @brief constructs a ColorString with color @p c and the string
 *        @p s.
 * @param s string to wrap
 * @param c color to print @p s in
 * @param bold determines whether @p s will be printed bold
 */
ColorString(str s, ColorID c=DEF, bool bold=1)
:string(s),
color(c),
bold(bold)
{
}

The part causing the errors is this:

    #ifdef UNIXLIKE
    colstr lbl(label, c);
    colstr msg(message);
    colstr ln(linestr);
    colstr fl(file);
    #else

Errors:

    message.hpp:58:20: error: invalid use of non-static data member 'label'
        colstr lbl(label, c);
                   ^~~~~
message.hpp:58:27: error: invalid use of non-static data member 'c'
        colstr lbl(label, c);
                          ^
message.hpp:59:20: error: invalid use of non-static data member 'message'
        colstr msg(message);
                   ^~~~~~~
message.hpp:60:19: error: invalid use of non-static data member 'linestr'
        colstr ln(linestr);
                  ^~~~~~~
message.hpp:61:19: error: invalid use of non-static data member 'file'
        colstr fl(file);

What's the problem here?

lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
  • 1
    Unrelated to your problem, but don't use symbols with double underscore in your code. Such symbols are reserved for the "implementation" (i.e. compiler and standard library). See [this question and answers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more details. – Some programmer dude Apr 14 '16 at 07:06
  • 1
    As for your problem, it looks like it could be related to [the most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse), and the compiler thinks you are declaring function prototypes instead. Try e.g. `colstr lbl = colstr(label, c);` instead. – Some programmer dude Apr 14 '16 at 07:10
  • Thanks, but same error. `message.hpp:58:29: error: invalid use of non-static data member 'label' colstr lbl = colstr(label, c);` – lo tolmencre Apr 14 '16 at 07:13

1 Answers1

3

The problem is that you are defining a friend function. Even when you define them "inline" in the class, they are still non-member functions and need an object instance to access their members.

You need to do e.g.

colstr lbl(m.label, m.c);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621