函数已经有一个函数体
Error C2084 Function already has a body
我在整个网站都看到了这个问题,但仍然没有任何东西能够解决我的问题。这是我唯一的错误,我不知道如何解决这个问题。我试过注释掉代码的不同部分,只是想看看会发生什么,但除了错误之外什么都没有。发生了什么事?
这也是构建错误
错误C2084:函数'HeroProfile::HeroProfile(void)'已经有body
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()
是同一个东西的事实弄糊涂,所以我想我应该指出来。
消息的意思就是它所说的。忽略输入错误,第二个.cpp中的第一个和第三个定义是针对同一个函数的。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 有一个构造函数,但有两个析构函数
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- C++ 这里有一个返回 (24) 的布尔返回类型函数
- 我有一个应该打印数组的函数,但由于某种原因它不起作用
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- 我可以在函数中有一个函数吗?
- 为什么静态指针返回函数中有一个"静态"键?
- 重新定义strdup函数已经有一个Body
- 我们是否需要在 x86 上 Peterson 锁的 unlock() 函数中有一个 mfence?
- min_element是否有一个采用投影函数的变体
- Constexpr函数必须有一个参数值
- 函数已经有一个函数体
- 是否有一个编译时函数/宏来确定c++ 0x结构体是否为POD
- 错误C2084:函数已经有一个函数体