无法将对象存储到数组中? C++
我试图做一些简单的事情并将一个对象存储到 C++ 中的数组中,但它一直说我不能使用 = 运算符,而右手操作数属于我的类。这是代码:
class Player {
string name;
double points;
bool wonLastRound;
public:
Player() {}
Player(string n)
{
name = n;
}
const Player &operator=(const Player &);
void addPoints(double p)
{
points += p;
}
};
这是实例化的代码,
void initPlayers()
{
for(int i = 0; i < 4; i++)
players[i] = new Player("Player " + i);
}
我们将不胜感激,我真的需要尽快完成这个项目!
I'm trying to do something simple and store an object into an Array in C++ but it keeps saying that I can't use the = operator with the right hand operand being of my class. Here's the code:
class Player {
string name;
double points;
bool wonLastRound;
public:
Player() {}
Player(string n)
{
name = n;
}
const Player &operator=(const Player &);
void addPoints(double p)
{
points += p;
}
};
and here's the code to instantiate
void initPlayers()
{
for(int i = 0; i < 4; i++)
players[i] = new Player("Player " + i);
}
any help would be appreciated, I really need to get this project finished soon!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是,
实际问题在于
for
循环内的赋值。您不必new
对象,因为您存储的是值而不是指针。用法:
It should be,
Actual problem lies in assignment inside
for
loop. You don't have tonew
the objects, as you are storing value and not the pointer.Usage: