//Header FILE:
#ifndef BMI_H_INCLUDED
#define BMI_H_INCLUDED
#include<iostream>
#include<string>
using namespace std;
class BMI{
public:
//default constructer
BMI();
//overloaded
BMI(string,int,double);
private:
string newName;
int newHeight;
double newWeight;
};
#endif // BMI_H_INCLUDED
//Implementation file:
#include "BMI.h"
BMI ::BMI(){
newHeight=0;
newWeight=0.0;
}
BMI::BMI(string name,intheight,double weight){
newName=name;
newHeight=height;
newWeight=weight;
}
//Main file:
#include <iostream>
#include<string>
#include "BMI.h"
using namespace std;
int main()
{
string name;
int height;
double weight;
cout<<"Name:\n";
getline(cin,name);
cout<<"Height(Inches):\n";
cin>>height;
cout<<"Weight(Pounds):";
cin>>weight;
BMI person_1("John",89,90.0);
//I get the error on the above line error is //undefinedreferenceto`BMI::BMI(std::string, int, double)'
}
有谁知道为什么这种情况一直发生在我身上?
我使用Codeblocks,如果是这样,我该如何解决这个问题并防止它再次发生。
每次将类分成头文件和cpp文件时都会发生这种情况。这只是我的程序因此而无法编译的次数之一。
答案 0 :(得分:3)
在BMI::BMI(string name,intheight,double weight){
中,int和height之间没有空格。
这会导致BMI person_1("John",89,90.0);
引用不存在的构造函数。