这是原题
#include<iostream> using namespace std; class Student{ private: int sno; int age; public: Student(int sno1, int age1); void show(); void setSno(int d); }; void Student::show(Student &stu){ cout<<"\nSno is "<<Stu.sno<<","; cout<<"age is "<<Stu.age<<","<<endl; } void setSno(int s){ Stu.sno = s; } void setAge(int a){ Stu.age = a; } void printStu(){} int main(){ Student stu("20021","20"); Stu.showdata(); return 0; }
这是本人修改过后的
#include<iostream> using namespace std; class Student{ private: int sno; int age; public: Student(int sno1, int age1); void show(); void setSno(int d); }; void Student::show(Student &stu){ cout<<"\nSno is "<<Stu.sno<<","; cout<<"age is "<<Stu.age<<","<<endl; } void setSno(int s){ Stu.sno = s; } void setAge(int a){ Stu.age = a; } void Student::student(){} int main(){ Student stu("20021","20"); Stu.showdata(); return 0; }
本人这样的思路对吗,本人改了一部分还是有错,有谁能详细讲解一下吗?
下面是错误:
(12) : error C2511: “show” : overloaded member function “void (class Student &)” not found in “Student”
(3) : see declaration of “Student”
(17) : error C2065: “Stu” : undeclared identifier
(17) : error C2228: left of “.sno” must have class/struct/union type
(20) : error C2228: left of “.age” must have class/struct/union type
(24) : error C2664: “__thiscall Student::Student(int,int)” : cannot convert parameter 1 from “char [6]” to “int”
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
(25) : error C2228: left of “.showdata” must have class/struct/union type
解决方案
20
好多错误啊。
#include<iostream> using namespace std; class Student{ private: int sno; int age; public: Student(int sno1, int age1):sno(sno1),age(age1){} void show(); void setSno(int d); void setAge(int d); }; void Student::show(){ cout<<"\nSno is "<<this->sno<<","; cout<<"age is "<<this->age<<","<<endl; } void Student::setSno(int s){ this->sno = s; } void Student::setAge(int a){ this->age = a; } int main(){ Student stu(20021,20); stu.show(); return 0; }
20
这样就行了
#include<iostream> using namespace std; class Student{ int sno; int age; public: void printStu(); void setSno(int d); void setAge(int d); }; void Student::printStu(){ cout<<"\nSno is "<<sno<<","; cout<<"age is "<<age<<","<<endl; } void Student::setSno(int s) {sno = s;} void Student::setAge(int a) {age = a;} int main(){ Student lin; lin.setSno(20021); lin.setAge(20); lin.printStu(); }