不确定是什么导致代码中的错误c2600



我看了其他论坛的帖子,我仍然很困惑。我是非常新的编码,所以一个简单的答案将不胜感激。我试图创建一个简单的程序,使用集合和获取来设置玩家的属性,然后用函数获取它们。然而,每当我调用这些函数时,就会得到错误。

这是我的。h文件:

#pragma once
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
using namespace std;
class Player
{
private:
string name;
int health;
int strength;
int stamina;
int experience;
bool passive;
public:
string GetName();
string SetName(string tName);
int GetHealth();
int SetHealth(int tHealth);
int GetStrength();
int SetStrength(int tStrength);
int GetStamina();
int SetStamina(int tStamina);
int GetExperience();
int SetExperience(int tExperience);
bool GetPassive();
bool SetPassive(bool tPassive);
};

这是我的第一个和第二个cpp文件:

#include <iostream>
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
#include "C:\Users\Ryan Bell\Desktop\School\2nd Year\Quarter 1\Programming\Week 1\PlayerClass\PlayerClass\PlayerClass.h"

Player::Player()
{
name = "";
health = 100;
strength = 30;
stamina = 100;
experience = 20;
passive = false;
}
string Player::GetName()
{
return name;
}
string Player::SetName(string tName)
{
name = tName;
return "Ok";
}
int Player::GetHealth()
{
return health;
}
int Player::SetHealth(int tHealth)
{
health = tHealth;
}
int Player::GetStrength()
{
return strength;
}
int Player::SetStrength(int tStrength)
{
strength = tStrength;
}
int Player::GetStamina()
{
return stamina;
}
int Player::SetStamina(int tStamina)
{
stamina = tStamina;
}
int Player::GetExperience()
{
return experience;
}
int Player::SetExperience(int tExperience)
{
experience = tExperience;
}
bool Player::GetPassive()
{
return passive;
}
bool Player::SetPassive(bool tPassive)
{
passive = tPassive;
}
#include <iostream>
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
#include "C:\Users\Ryan Bell\Desktop\School\2nd Year\Quarter 1\Programming\Week 1\PlayerClass\PlayerClass\PlayerClass.h"
int main()
{
Player Player1;
Player1.SetName("Jake");
Player1.SetHealth(100);
Player1.SetStrength(30);
Player1.SetStamina(50);
Player1.SetExperience(0);
Player1.SetPassive(true);
cout << "Player " << Player1.GetName() << ".";
}

感谢您的时间和帮助!

我没有看到Player构造函数声明:

// I see the Player Constructor definition here.
Player::Player()
{
.....

但是这个方法没有在类中声明。

class Player
{
private:
.....
public:
Player();           // Add this line.
string GetName();

p。你的代码应该可以编译并且"可能"跑步,但不是很好。如果你能证明它是有效的,你可以把它带到Code Review,让他们给你一些建议,告诉你如何使它更好(并纠正一些明显的错误)。

编译后的代码如下:

2.cpp: In member function ‘int Player::SetStamina(int)’:
2.cpp:18:1: warning: no return statement in function returning non-void [-Wreturn-type]
18 | }
| ^
2.cpp: In member function ‘int Player::SetExperience(int)’:
2.cpp:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
28 | }
| ^
2.cpp: In member function ‘bool Player::SetPassive(bool)’:
2.cpp:38:1: warning: no return statement in function returning non-void [-Wreturn-type]
38 | }

这些问题可以通过将所有setter转换为返回void的函数来解决,例如:

void Player::SetStamina(int tStamina)

*.hplayer.cpp中同时做。

修复后,我们碰到了链接器错误:

/usr/bin/ld: /tmp/ccQk0cvm.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `Player::SetName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0xa7): undefined reference to `Player::SetHealth(int)'
/usr/bin/ld: main.cpp:(.text+0xb8): undefined reference to `Player::SetStrength(int)'
/usr/bin/ld: main.cpp:(.text+0x11a): undefined reference to `Player::GetName[abi:cxx11]()'
collect2: error: ld returned 1 exit status

这意味着您忘记定义Player::GetNamePlayer::SetStrength(int)和其他2个方法。

这让我们找到了最简单的解决方案(目前):修改类的public部分,使其实现所有函数:
public:
std::string GetName() const { return name; }
void SetName(string tName) { name = tName; }
int GetHealth() const { return health; }
void SetHealth(int tHealth) { health = tHealth; }
int GetStrength() const { return strength; }
void SetStrength(int tStrength) { strength = tStrength; }
int GetStamina() const { return stamina; }
void SetStamina(int tStamina) { stamina = tStamina; }
int GetExperience() const { return experience; }
void SetExperience(int tExperience) { experience = tExperience; }
bool GetPassive() const { return passive; }
void SetPassive(bool tPassive) { passive = tPassive; }

,不要为类成员定义使用单独的源文件。

或者,您可以保留该文件,但必须在其中添加所有缺失的函数定义。

注意,我将所有getter声明为const成员函数。相信我,这就是它们应该被定义的方式。

相关内容

  • 没有找到相关文章

最新更新