在C++中收到错误消息

Getting an error message in C++

本文关键字:错误 消息 C++      更新时间:2023-10-16

我是C++新手(我是C程序员),所以如果这看起来像是一个愚蠢的问题,我深表歉意。

当我运行此程序时,我收到以下错误消息:

错误 C2661:"学生::学生":没有重载函数占用 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();
};