我们在运行VS2019代码时,报错如下信息:
反复检查了代码,发现代码是没有问题的。
我的代码如下:
#include <iostream>
using namespace std;
#include <string>
//定义student类
class student
{
//一般成员函数定义在公有中
public: //公有
int a = 0;
//构造函数
student() {
cout << "我是构造函数" << endl;
};
student(int s1) {
a = s1;
cout << "我是有参构造函数" <<a<< endl;
};
student(const student &s1) {
a = s1.a;
cout << "我是拷贝构造函数" <<a<< endl;
};
~student() { //析构函数
cout << "我是析构函数" << endl;
};
//成员变量
void getScore(); //定义成员函数
void getAge(int age); //定义成员函数
private: //私有成员变量,只能被类体内部访问
string name = "chen";
int age =24;
protected: //被保护的成员变量
int score = 0;
};
void student::getScore() {
score = 0;
cout << score<<endl;
};
void student::getAge(int age1) { //定义类中的成员函数
age = age1;
cout << age << endl;
};
void test01() {
student s0;
student s1 = student(10);
student s2(s1);
}
//主函数
void main() {
student s0;
test01();
s0.getAge(33);
s0.getScore();
};
通过反复排查问题,发现是因为类名和其他源文件中的类名重复了
通过查找替换新的类名再次运行,就解决报错了。