C ++ - 错误C2511:'BMI'中找不到重载的成员函数

时间:2021-03-11 14:14:38

Got an error on my C++ program. It is likely to be something simple as I have only just started programming.

我的C ++程序出错了。它可能很简单,因为我刚开始编程。

The error is:

错误是:

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

bmi.h:

#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

bmi.cpp:

// 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;
}

2 个解决方案

#1


4  

Ok, when I try to compile the code I see a couple of problems:

好的,当我尝试编译代码时,我发现了一些问题:

  • The setName(string) function in the .cpp doesn't match anything in the header.
  • .cpp中的setName(string)函数与标头中的任何内容都不匹配。

  • The setHeight(int) function in the .cpp doesn't match anything in the header.
  • .cpp中的setHeight(int)函数与标头中的任何内容都不匹配。

  • The setWeight(double) function in the .cpp doesn't match anything in the header.
  • .cpp中的setWeight(double)函数与标题中的任何内容都不匹配。

I would try to solve the compilation errors in the order they occur, and then see if you still have a problem with getWeight. I'm assuming that you are seeing the same problems with the undeclared functions that I'm seeing.

我会尝试按照它们发生的顺序解决编译错误,然后看看你是否仍然遇到getWeight问题。我假设你看到了我所看到的未声明功能的相同问题。

#2


0  

The error appears to be telling you that you are trying to call BMI::getWeight() somewhere and you are passing in it a parameter with a double type. This error is a bit perplexing as no such function that matches void BMI::getWeight(double) defined in either the BMI class in the header file or the cpp file. If you have changed the code since you posted it up then please do update and post ALL of the compiler messages. I suspect that you have not posted all of the compiler messages because SetName,setHeight and setWeight are all missing from the BMI class definition. So make sure you add all of those into the BMI class.

该错误似乎告诉您,您正在尝试在某处调用BMI :: getWeight()并且您正在传递一个带有double类型的参数。这个错误有点令人困惑,因为没有匹配在头文件或cpp文件中的BMI类中定义的void BMI :: getWeight(double)的函数。如果您在发布之后更改了代码,请更新并发布所有编译器消息。我怀疑您没有发布所有编译器消息,因为BMI类定义中缺少SetName,setHeight和setWeight。因此,请确保将所有这些添加到BMI类中。

Also I think that it's good practice to initialize your data members differently. So instead of:

此外,我认为以不同方式初始化您的数据成员是一个好习惯。所以代替:

BMI::BMI(string name, int height, double weight) {
  newName = name;
  newHeight = height;
  newWeight = weight;
}

you should prefer:

你应该更喜欢:

BMI::BMI(string name, int height, double weight):
  newName(name),
  newHeight(height),
  newWeight(weight)
{ }

#1


4  

Ok, when I try to compile the code I see a couple of problems:

好的,当我尝试编译代码时,我发现了一些问题:

  • The setName(string) function in the .cpp doesn't match anything in the header.
  • .cpp中的setName(string)函数与标头中的任何内容都不匹配。

  • The setHeight(int) function in the .cpp doesn't match anything in the header.
  • .cpp中的setHeight(int)函数与标头中的任何内容都不匹配。

  • The setWeight(double) function in the .cpp doesn't match anything in the header.
  • .cpp中的setWeight(double)函数与标题中的任何内容都不匹配。

I would try to solve the compilation errors in the order they occur, and then see if you still have a problem with getWeight. I'm assuming that you are seeing the same problems with the undeclared functions that I'm seeing.

我会尝试按照它们发生的顺序解决编译错误,然后看看你是否仍然遇到getWeight问题。我假设你看到了我所看到的未声明功能的相同问题。

#2


0  

The error appears to be telling you that you are trying to call BMI::getWeight() somewhere and you are passing in it a parameter with a double type. This error is a bit perplexing as no such function that matches void BMI::getWeight(double) defined in either the BMI class in the header file or the cpp file. If you have changed the code since you posted it up then please do update and post ALL of the compiler messages. I suspect that you have not posted all of the compiler messages because SetName,setHeight and setWeight are all missing from the BMI class definition. So make sure you add all of those into the BMI class.

该错误似乎告诉您,您正在尝试在某处调用BMI :: getWeight()并且您正在传递一个带有double类型的参数。这个错误有点令人困惑,因为没有匹配在头文件或cpp文件中的BMI类中定义的void BMI :: getWeight(double)的函数。如果您在发布之后更改了代码,请更新并发布所有编译器消息。我怀疑您没有发布所有编译器消息,因为BMI类定义中缺少SetName,setHeight和setWeight。因此,请确保将所有这些添加到BMI类中。

Also I think that it's good practice to initialize your data members differently. So instead of:

此外,我认为以不同方式初始化您的数据成员是一个好习惯。所以代替:

BMI::BMI(string name, int height, double weight) {
  newName = name;
  newHeight = height;
  newWeight = weight;
}

you should prefer:

你应该更喜欢:

BMI::BMI(string name, int height, double weight):
  newName(name),
  newHeight(height),
  newWeight(weight)
{ }