我是 C++ 的新手(我是一名 C 程序员),所以如果这看起来像是一个愚蠢的问题,我深表歉意。
当我运行这个程序时,我收到以下错误消息:
错误 C2661:“Student::Student”:没有重载函数接受 2 个参数
我评论了错误发生的地方(2 个实例)。谢谢。
//Definition.cpp
#include "Student.h"
Student::Student(string initName, double initGPA) //error here and in main.cpp
{
name = initName;
GPA = initGPA;
}
string Student::getName()
{
return name;
}
double Student::getGPA()
{
return GPA;
}
void Student::printInfo()
{
cout << name << " is a student with GPA: " << GPA << endl;
}
//student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
private:
string name;
double GPA;
public:
string getName();
double getGPA();
void setGPA(double GPA);
void printInfo();
};
//main.cpp
#include <iostream>
#include "Student.h"
int main() {
Student s("Lemuel", 3.2); //here is the error
cout << s.getName() << endl;
cout << s.getGPA() << endl;
cout << "Changing gpa..." << endl;
s.setGPA(3.6);
s.printInfo();
return 0;
}
最佳答案
您的构造函数未声明。
试试这个:
class Student
{
private:
string name;
double GPA;
public:
Student(string initName, double initGPA);
string getName();
double getGPA();
void setGPA(double GPA);
void printInfo();
};
关于c++ - 在 C++ 中获取错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21841363/