在头文件上为类声明 = 和 [] 运算符,"must be a nonstatic member function"错误
Declaring = and [] operators for a class on the header file, "must be a nonstatic member function" error
我做了一个类块和一个结构坐标,在实现运算符时,我遇到了错误:
'coords operator[](const Block&, const size_t&)' must be a nonstatic member function
'bool operator=(Block&, const Block&)' must be a nonstatic member function
我在类块的头文件中声明了这 2,如下所示:
class Block
{
friend Block operator+(const Block&, const coords&);
friend Block operator+(const Block&, const Block&);
friend coords operator[](const Block&, const std::size_t&);
friend void operator+=(Block&, const coords&);
friend void operator+=(Block&, const Block&);
friend bool operator=(Block&, const Block&);
//...
};
只有运算符 [] 和 = 会收到此错误,我不确定为什么。我尝试更改返回值和参数类型,但它一直遇到同样的问题。这两个运营商很特别吗?还是我的声明有误?我一直在寻找解决这个问题的方法,但找不到合适的答案。
感谢您的回复。
并非所有运算符都可以使用非成员函数重载。 []
和=
就是两个这样的运营商。它们只能作为成员函数重载。
有关更多详细信息,请参阅 http://en.cppreference.com/w/cpp/language/operators。
这些运算符不能声明为好友。相反,您应该像这样声明:
coords operator[](const std::size_t&);
bool operator=(const Block&);
您的操作员也没有真正遵循约定。运算符+=
和=
应该返回一个Block&
,即*this
。
原因正是错误消息所说的:这两个必须是非静态成员函数。摆脱他们面前的friend
并删除第一个参数。
此外,operator+=
通常也作为成员函数实现,尽管并非必须如此。但如果是这样,它为您提供了一种简单的方法来实施operator+
而无需将其作为朋友。
R Sahu的链接很有用,表明[]和=不能声明为非成员,但它并没有真正解释原因。@Baum mit aguen的链接也清除了其他一些问题。(感谢您提供的信息)
因此,我根据这些新信息调整了我的代码,如下所示:
Block.h
class Block
{
public:
//...
coords* operator[](size_t);
Block operator=(Block);
//...
};
阻止.cpp
//...
coords* Block::operator[](size_t index)
{
if(index >= 0 && index < block.size())
return &block.at(index);
coords *tmp = new coords(-1, -1);
return tmp;
}
Block Block::operator=(Block b2)
{
block.empty();
block.reserve(b2.block.size());
append(b2.block);
return *this;
}
//...
这样,您就可以将*(*b1)[0] = c1;
称为Block* b1
和coords c1
。
friend 修饰符对于其他类型的实现很有用,尽管我只是在那之后才意识到内联的实现必须在头文件中完成,而不是在 cpp 上完成。Block.h
class Block
{
public:
//...
friend std::ostream& operator<<(std::ostream&, const Block&);
friend std::ostream& operator<<(std::ostream&, Block&);
//...
};
inline std::ostream& operator<<(std::ostream& out, const Block& b)
{
// do something
return out;
};
inline std::ostream& operator<<(std::ostream& out, Block& b)
{
// do something
return out;
};
在这种情况下,您需要将"this"参数传递给函数,因为这些是非成员函数,应该在类外部的头文件中实现。
我希望这对大家有所帮助。
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- C++复制函数重载导致"must be a nonstatic member function"错误
- C++ 错误:"array must be initialized with a brace-enclosed initializer"
- "expression must be a modifiable lvalue"是什么意思?
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- "[ptr, ptr+len) must be a valid range"是什么意思?
- 谷歌模拟 - 怎么说"function must be called ONCE with a certain parameter but ok to be called many times with
- 如何解决访问不同功能的"nonstatic member reference must be relative to a specific object"
- 为什么我的某些数据成员"expression must be an lvalue or a function designator"收到此错误?
- "Member function must be called or its address taken in function" C++错误
- "uses of target_link_libraries must be either all-keyword or all-plain"
- Visual Studio C++ test "name must be a namespace name" error
- 为什么我会看到"member function with the same name as its class must be a constructor"?
- 如何修复"initial value of reference to non-const must be an lvalue?"
- 产生错误"expression must be a modifiable lvalue"
- WxWidgets Hello World 编译失败:"wchar_t must be available"
- 获取此地址时出现"expression must be an l-value or function designator"错误
- 在这种情况下如何解决"Reference to non-static member function must be called"?
- 为什么我会收到错误"parameter pack 'F' must be at the end of the template parameter list"