我的C ++程序出错了。它可能很简单,因为我刚开始编程。
错误是:
Error 1 error C2511: 'void BMI::getWeight(double)' : overloaded member function not found in 'BMI' c:\users\**********\documents\visual studio 2012\projects\project2\project2\bmi.cpp 40 1 Project2
#include <iostream>
#include <string>
using namespace std;
#ifndef BMI_H
#define BMI_H
class BMI {
public:
//Defualt Constructor
BMI();
//Overload Constructor
BMI(string, int, double);
//Destructor
~BMI();
//Accessor Functions
string getName() const;
// getName - returns name of paitent
int getHeight() const;
//getHeight - returns height of paitent
double getWeight() const;
//getWeight returns weight of paitent
private:
//Member Variables
string newName;
int newHeight;
double newWeight;
};
#endif
// Function Definitions
#include "BMI.h"
BMI::BMI() {
newHeight = 0;
newWeight = 0.0;
}
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
BMI::~BMI() {
}
string BMI::getName() const {
return newName;
}
int BMI::getHeight() const {
return newHeight;
}
double BMI::getWeight() const {
return newWeight;
}
void BMI::setName(string name) {
newName = name;
}
void BMI::setHeight(int height) {
newHeight = height;
}
void BMI::setWeight(double weight) {
newWeight = weight;
}
答案 0 :(得分:4)
好的,当我尝试编译代码时,我发现了一些问题:
我会尝试按照它们发生的顺序解决编译错误,然后看看你是否仍然遇到getWeight问题。我假设你看到了我所看到的未声明功能的相同问题。
答案 1 :(得分:0)
错误似乎在告诉您,您正尝试在某处调用BMI::getWeight()
,并且您正在传递一个double
类型的参数。这个错误有点令人困惑,因为没有匹配头文件或cpp文件中的BMI类中定义的void BMI::getWeight(double)
的函数。如果您在发布之后更改了代码,请更新并发布所有编译器消息。我怀疑您没有发布所有编译器消息,因为SetName
,setHeight
和setWeight
都缺少BMI类定义。因此,请确保将所有这些添加到BMI类中。
此外,我认为以不同方式初始化您的数据成员是一种好习惯。所以而不是:
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
你应该更喜欢:
BMI::BMI(string name, int height, double weight):
newName(name),
newHeight(height),
newWeight(weight)
{ }