c++中操作符重载编译错误
Operator overloading compile error in C++
我正在尝试制作一个程序来模拟在c++中玩战舰
#include <iostream>
#include <vector>
class Ship {
public:
Ship();
Ship operator<<(const Ship&);
private:
int x = 0;
int y = 0;
std::vector<std::pair<int, int>> ship_loc; //Ship location
};
Ship::Ship() {
srand(time(NULL));
std::vector<std::pair<int, int>> ship_loc; //Ship location
for (int i = 0; i < 4; ++i) {
x = rand() % 20;
y = rand() % 20;
std::pair<int, int> coordinates = std::make_pair(x, y);
ship_loc.push_back(coordinates);
//ship_loc.push_back(std::pair<x, y>)
};
};
Ship operator<<(const Ship &s) {
std::cout << ship_loc[0] << ship_loc[1] << ship_loc[2] << ship_loc[3]
<< std::endl;
}
int main()
{
Ship first_ship;
std::cout << first_ship;
}
当我试图编译这个时,它给我:
battleship.cpp:26:30: error: âShip operator<<(const Ship&)â must take exactly two arguments
battleship.cpp: In function âint main()â:
battleship.cpp:34:25: error: cannot bind âstd::ostream {aka std::basic_ostream<char>}â lvalue to âstd::basic_ostream<char>&&â
In file included from /usr/include/c++/4.7/iostream:40:0,
from battleship.cpp:1:
/usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of âstd::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Ship]â
显然对类还不是很有经验。div。
您将operator <<
声明为成员函数
Ship operator<<(const Ship&);
表示左操作数是Ship类的实例。所以它可以被命名为
Ship a, b;
a << b;
但是很明显你并不想这样。
如果要在输出流中输出Ship类的对象,则操作符必须是非成员函数。您可以将它定义为类的友元函数。例如
class Ship {
public:
Ship();
friend std::ostream & operator <<( std::ostream &os, const Ship& );
private:
int x = 0;
int y = 0;
std::vector<std::pair<int, int>> ship_loc; //Ship location
};
然后你可以像下面这样定义
std::ostream & operator <<( std::ostream &os, const Ship &s)
{
os << "( " << s.ship_loc[0].first << ", " << s.ship_loc[0].second << " )"
<< ", ( " << s.ship_loc[1].first << ", " << s.ship_loc[1].second << " )"
<< ", ( " << <.ship_loc[2].first << ", " << s.ship_loc[3].second << " ) "
<< ", ( " << <.ship_loc[3].first << ", " << s.ship_loc[3].second << " ) "
<< std::endl;
return os;
}
同样,由于坐标的数量是固定的,那么就不需要使用std::vector
。用std::array<std::pair<int, int>, 4>
代替
也删除语句
std::vector<std::pair<int, int>> ship_loc; //Ship location
构造函数中的。它是一个局部变量。您需要填充数据成员ship_loc,而不是这个局部变量。
下面这行并没有达到你想要的效果:
Ship operator<<(const Ship&);
当您重载<<
操作符时,您不能返回Ship
并且第一个参数必须为ostream
类型。它的返回类型必须是std::ostream&
。引用来自这个线程的一个答案:c++ Operator重载
流操作符是最常见的重载操作符之一,是二进制中缀操作符,语法对其是成员还是非成员没有规定限制。由于它们改变了左参数(它们改变了流的状态),因此根据经验规则,它们应该作为左操作数类型的成员来实现。然而,它们的左操作数是来自标准库的流,虽然标准库定义的大多数流输出和输入操作符确实被定义为流类的成员,但当你为自己的类型实现输出和输入操作时,你不能改变标准库的流类型。这就是为什么需要为自己的类型实现这些非成员函数的操作符。
一种方法是用规范形式实现它:
std::ostream& operator<<(std::ostream& os, const Ship& obj)
{
// write obj information
return os;
}
您错误地重载了operator<<
,您想要的签名是这样的:
std::ostream& operator<<(std::ostream& os, const Ship& s);
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?