c++ - 在 C++ 中获取错误消息

标签 c++ constructor overloading

我是 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/

相关文章:

c++ - 从网站读取csv文件到c++

templates - 具有特征和函数重载的静态类型语言?

c++ - 错误 : No instance of overloaded function "mbed::Ticker::attach" matches the argument list

c++ - 重载的 Operator 无法正常工作?

c++ - "undefined reference"到从静态方法访问的模板类的静态成员

c++ - 值(value)与对象

c++ - 关闭 LoadLibrary 打开的 DLL 文件句柄

c++ - 调试器卡在函数开始处

c++ - union 类型数据成员的初始化

java - 使用反射获取构造函数(或方法)参数注释