【发布时间】:2016-04-14 07:03:29
【问题描述】:
我查看了有关此错误的其他问题,但到目前为止我还没有找到看起来像我的问题的答案。我有两个班级Message 和ColorString。在前者的方法中,我通过将Message 的成员传递给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
ColorString 的构造函数如下所示:
/**
* @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)
{
}
导致错误的部分是这样的:
#ifdef UNIXLIKE
colstr lbl(label, c);
colstr msg(message);
colstr ln(linestr);
colstr fl(file);
#else
错误:
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);
这里有什么问题?
【问题讨论】:
-
与您的问题无关,但不要在代码中使用带有双下划线的符号。这些符号是为“实现”(即编译器和标准库)保留的。有关详细信息,请参阅this question and answers。
-
至于你的问题,看起来可能与the most vexing parse有关,编译器认为你是在声明函数原型。尝试例如
colstr lbl = colstr(label, c);代替。 -
谢谢,但同样的错误。
message.hpp:58:29: error: invalid use of non-static data member 'label' colstr lbl = colstr(label, c);
标签: c++ static-members