在头文件上为 class 声明 = 和 [] 运算符,"must be a nonstatic member function" 错误
Declaring = and [] operators for a class on the header file, "must be a nonstatic member function" error
我制作了一个 class 块和一个结构坐标,在实现运算符时我遇到了错误:
'coords operator[](const Block&, const size_t&)' must be a nonstatic member function
'bool operator=(Block&, const Block&)' must be a nonstatic member function
我在 class 块的头文件中声明了这两个,如下所示:
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&);
//...
};
只有运算符 [] 和 = 会出现此错误,我不确定原因。
我试图更改 return 值和参数类型,但它总是遇到同样的问题。
这两个运营商很特别吗?还是我的声明有误?
我已经寻找解决这个问题的方法,但找不到合适的答案。
感谢您的回复。
并非所有运算符都可以使用非成员函数重载。 []
和 =
就是两个这样的运算符。它们只能作为成员函数重载。
有关详细信息,请参阅 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的link很有用,说明[]和=不能声明为非成员,但并没有真正解释为什么。
@Baum mit aguen 的 link 也清除了一些其他问题。
(感谢提供信息)
所以,我针对这个新信息调整了我的代码如下:
Block.h
class Block
{
public:
//...
coords* operator[](size_t);
Block operator=(Block);
//...
};
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修饰符对其他类型的实现很有用,虽然我是在那之后才意识到必须完成inline的实现在头文件中,not 在 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”参数传递给该函数,因为这些是非成员函数,应在 class,在头文件中.
希望对大家有所帮助,大家好好编码。
我制作了一个 class 块和一个结构坐标,在实现运算符时我遇到了错误:
'coords operator[](const Block&, const size_t&)' must be a nonstatic member function
'bool operator=(Block&, const Block&)' must be a nonstatic member function
我在 class 块的头文件中声明了这两个,如下所示:
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&);
//...
};
只有运算符 [] 和 = 会出现此错误,我不确定原因。 我试图更改 return 值和参数类型,但它总是遇到同样的问题。 这两个运营商很特别吗?还是我的声明有误? 我已经寻找解决这个问题的方法,但找不到合适的答案。
感谢您的回复。
并非所有运算符都可以使用非成员函数重载。 []
和 =
就是两个这样的运算符。它们只能作为成员函数重载。
有关详细信息,请参阅 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的link很有用,说明[]和=不能声明为非成员,但并没有真正解释为什么。 @Baum mit aguen 的 link 也清除了一些其他问题。 (感谢提供信息)
所以,我针对这个新信息调整了我的代码如下:
Block.h
class Block
{
public:
//...
coords* operator[](size_t);
Block operator=(Block);
//...
};
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修饰符对其他类型的实现很有用,虽然我是在那之后才意识到必须完成inline的实现在头文件中,not 在 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”参数传递给该函数,因为这些是非成员函数,应在 class,在头文件中.
希望对大家有所帮助,大家好好编码。