c++ - Error C2084 函数已有主体

标签 c++ compiler-construction compiler-errors

<分区>

我在整个网站上都看到了这一点,但仍然无法解决我的问题。这是我唯一的错误,我不知道如何解决这个问题。我已经尝试注释掉代码的不同部分,只是为了看看会发生什么,但仍然只有错误。这是怎么回事?

她也是构建错误

error C2084: 函数 'HeroProfile::HeroProfile(void)' 已经有主体

main.cpp

#include <iostream>
#include <string>

#include "HeroProfile.h"

using namespace std;

int main()
{
string name;
int race;
double weight;

    cout << "Enter your Hero's name:";
cin>> name;
cout << "Enter your Hero's Race (1:Elf 2:Human 3 Dwarf)";
cin >> race;
cout << "Enters your Hero's weight ( in pounds):";
cin >> weight;

HeroProfile Hero_1(name, race, weight);

cout << endl << "hero's Name: " << Hero_1.getName() << endl <<
    "Race: " << Hero_1.getRace() << endl <<
    "Weight:" << Hero_1.getWeight() << endl;

cout << endl;

cout << "Enter your Hero's Name:";
cin >> name;
cout << "Enter your Race (1:Elf 2:Human 3 Dwarf)";
cin >> race;
cout <<"Enter your Hero's weight (in pounds)";
cin >> weight;

HeroProfile Hero_2(name, race, weight);

Hero_2.setName(name);
Hero_2.setRace(race);
Hero_2.setWeight(weight);

cout << endl << "Hero's Name: " << Hero_2.getName() << endl <<
    "Race: " << Hero_2.getRace() << endl << "Weight: " << Hero_2.getWeight() <<     endl;

return 0;
}

头文件

#include <iostream>
#include <string>

using namespace std;

#ifndef HeroProfiel_H 
#define HeroProfile_H

class HeroProfile 
{

public:
    //default constructor
    HeroProfile();

    //overlaod constructor
    HeroProfile(string, int, double);

    //destructor
    ~HeroProfile();

    //Accesor functions
    string getName() const;
        // get name - returns name of patient

    int getRace() const;
        // getHeight-returns height of patient

    double getWeight() const;
        // getWeight- returns weight of patient

    //Mutator Functions
    void setName(string);
        //set name of patient
        //@param string - name of patient

    void setRace(int);
        //setHeight-sets heigh tof patient
        //@param int- height iof patient

    void setWeight(double);
    //setWeight-sets weight of patiend
    //@param double-weight of patient



private:
    //Memeber variables
    string newName;
    int newRace;
    double newWeight;




};

#endif

和第二个 .cpp 文件

#include "HeroProfile.h"

eroProfile::HeroProfile() //Default constructor
{
newRace = 0;
newWeight = 0.0;
}

HeroProfile::HeroProfile(string name, int race, double weight) //Overloaded Constructor
{
newName = name;
newRace = race;
newWeight = weight;
}

HeroProfile::HeroProfile()
}
}

//Accessors
string HeroProfile::getName() const 
{
return newName;
}

int HeroProfile::getRace() const 
{
return newRace;
}

double HeroProfile::getWeight() const
{
return newWeight;
}

//Mutators
void HeroProfile::setName(string name)
{ 
newName = name;
}

void HeroProfile::setRace(int race)
{
newRace = race;
}


void HeroProfile::setWeight(double weight)
{
newWeight = weight;
}

最佳答案

正如其他人所指出的,您在第二个 cpp 文件中为 HeroProfile 默认构造函数 HeroProfile::HeroProfile() 定义了两个定义:

第一个是

HeroProfile::HeroProfile() //Default constructor
{
newRace = 0;
newWeight = 0.0;
}

第二个是

HeroProfile::HeroProfile()
}
}

基于我没有看到的事实,您可能打算将第二个作为您的类析构函数(在您的头文件中声明但未在您的 cpp 文件中定义),在这种情况下您应该将其替换为这个:

HeroProfile::~HeroProfile()
}
}

我希望你不会因为 HeroProfile::HeroProfile(void)HeroProfile::HeroProfile() 是同一件事而感到困惑,所以我我想我应该指出来。

关于c++ - Error C2084 函数已有主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24101308/

相关文章:

c - 使用gmp编译时出错

asp.net - 预期表达

c++ - 使用 typedef 和匿名类定义与传统类定义的等效性

C++:二进制搜索字符串的一部分

c++ - 我需要帮助来选择合适的 C++ 容器

javascript - 世界上有没有针对简单函数式语言自身的强大优化编译器?

compiler-construction - 使用 LLVM 达到定义

c++ - 为什么没有专门用于 c 或 c++ 的编译器?

c++ - 在Vector类对象上调用push_back()方法时收到错误

c++ - CMake + 查找包或 checkout 并安装