C++ 无法访问private 成员
//.CPP#include<iostream>#include<vector>#include"student.h"usingstd::cout;usingstd::e...
//.CPP
#include<iostream>
#include<vector>
#include"student.h"
using std::cout;
using std::endl;
using std::cin;
using std::vector;
extern student input_student();
int main() {
vector<student> students;
student temp("",0,0);
for(int i = 0; i < 5; ++i) {
input_student(temp);
students.push_back(temp);
}
for(vector<student>::iterator itr = students.begin();
itr != students.end(); ++itr)
(*itr).show();
return 0;
}
student input_student(student &stu){
cout<<"Input the student's data:"<<endl;
cout<<"name:";
cin>>stu.name;
cin.clear();
cout<<"age:";
cin>>stu.age;
cin.clear();
cout<<"test score:";
cin>>stu.test_score;
cin.clear();
cout<<"Input finish. ";
return stu;
}
//.H
#include<iostream>
#include<string>
using std::cout;
using std::endl;
using std::cin;
using std::string;
class student {
student set(string, int, int);//给三个属性赋值
int get() const; //获得成绩
student show();
student(string, int, int) {};//这三个变量name有默认构造函数,age和test_score默认初始化为0
string get_name() {return name;}
int get_age() {return age;}
int get_score() {return test_score;}
private:
string name;
int age;
int test_score;
};
student student::set(string tname, int tage, int tscore) {
name = tname;
age = tage;
test_score = tscore;
return *this;
}
int student::get() const{
return test_score;
}
student student::show(){
cout<<name<<" "<<age<<" "<<test_score<<endl;
return *this;
}
/*先不管别的错误,第一个提示错误是: error C2248: “student::student”: 无法访问 private 成员(在“student”类中声明)。我就奇了怪了,我定义类的时候又没有继承哪个类,定义出来一编译为什么是student::student?这货到底错哪了啊?*/ 展开
#include<iostream>
#include<vector>
#include"student.h"
using std::cout;
using std::endl;
using std::cin;
using std::vector;
extern student input_student();
int main() {
vector<student> students;
student temp("",0,0);
for(int i = 0; i < 5; ++i) {
input_student(temp);
students.push_back(temp);
}
for(vector<student>::iterator itr = students.begin();
itr != students.end(); ++itr)
(*itr).show();
return 0;
}
student input_student(student &stu){
cout<<"Input the student's data:"<<endl;
cout<<"name:";
cin>>stu.name;
cin.clear();
cout<<"age:";
cin>>stu.age;
cin.clear();
cout<<"test score:";
cin>>stu.test_score;
cin.clear();
cout<<"Input finish. ";
return stu;
}
//.H
#include<iostream>
#include<string>
using std::cout;
using std::endl;
using std::cin;
using std::string;
class student {
student set(string, int, int);//给三个属性赋值
int get() const; //获得成绩
student show();
student(string, int, int) {};//这三个变量name有默认构造函数,age和test_score默认初始化为0
string get_name() {return name;}
int get_age() {return age;}
int get_score() {return test_score;}
private:
string name;
int age;
int test_score;
};
student student::set(string tname, int tage, int tscore) {
name = tname;
age = tage;
test_score = tscore;
return *this;
}
int student::get() const{
return test_score;
}
student student::show(){
cout<<name<<" "<<age<<" "<<test_score<<endl;
return *this;
}
/*先不管别的错误,第一个提示错误是: error C2248: “student::student”: 无法访问 private 成员(在“student”类中声明)。我就奇了怪了,我定义类的时候又没有继承哪个类,定义出来一编译为什么是student::student?这货到底错哪了啊?*/ 展开
4个回答
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
展开全部
只有同并厅渗绝脊一个类class中的 public类型的成员函数 才能访问 类中private类型成员变量。记住这一点永远不伏亏会出错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在类里面前罩默认袜含的是private。必须显慧好闹示指定为public 看一下的代码
class student {
public:
student set(string, int, int);//给三个属性赋值
int get() const; //获得成绩
student show();
student(string, int, int) {};//这三个变量name有默认构造函数,age和test_score默认初始化为0
string get_name() {return name;}
int get_age() {return age;}
int get_score() {return test_score;}
private:
string name;
int age;
int test_score;
};
class student {
public:
student set(string, int, int);//给三个属性赋值
int get() const; //获得成绩
student show();
student(string, int, int) {};//这三个变量name有默认构造函数,age和test_score默认初始化为0
string get_name() {return name;}
int get_age() {return age;}
int get_score() {return test_score;}
private:
string name;
int age;
int test_score;
};
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class student {
student set(string, int, int);//给三个属性此余销赋值
int get() const; /毁配/获森游得成绩
student show();
student(string, int, int)
student set(string, int, int);//给三个属性此余销赋值
int get() const; /毁配/获森游得成绩
student show();
student(string, int, int)
追问
我把构造函数放前面也提示通不过,未必这个类的函数不能返回这个类的对象?
追答
这个,我只能告诉你,我其实已经忘了c++了
刚刚这个只是感觉,具体的,那啥,你还是问问你的c++老师吧,相信他非常愿意有学生好问好学。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询