首页 > 解决方案 > 不确定是什么导致代码中出现错误 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() << ".";
}

感谢您的时间和帮助!

标签: c++

解决方案


我没有看到 Player Constructor 声明:

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

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

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

PS。您的代码应该编译并“可能”运行但不好。如果你能证明它有效,你可以把它带到Code Review并让他们给你建议,告诉你如何让它变得更好(并纠正一些明显的错误)。


推荐阅读