注册 登录
编程论坛 C++教室

类类型定义。。。做了个类类型的小程序,不知道错哪里请大家帮我看看

a632034079 发布于 2009-10-06 16:36, 467 次点击
看例题做的。。。还不是很明白类类型的意思。。。做了个程序错误很多,自己能改的,都改过来了。。。还是错了不少....大侠帮我看看先谢过了。错误报告在下面....
#include <iostream>
#include <string>
using namespace std;
class studentClass
{
private:
    string strName;
    string strClass;
    int number;
    string homeAddress;
    int chinese;
    int math;
    int english;
public:
    string getstrName(){return strName;};
    string gethomeAddress(){return homeAddress;};
};
int main()
{
    studentClass student[30];
    student[0].strName="LIUCE";
    student[0].strClass="Class 2";
    student[0].number=12366;
    student[0].homeAddress="HuangYan road";
    student[0].chinese =100;
    student[0].math =100;
    student[0].english =100;
    cout<<student[0].strName
        <<"live in"
        <<student[0].homeAddress
        <<".His nuber is"
        <<student[0].number<<endl;
    cin.get();
    return 0;
}


错误报告;
Compiling...
jhg.cpp
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(21) : error C2248: 'strName' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(7) : see declaration of 'strName'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(22) : error C2248: 'strClass' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(8) : see declaration of 'strClass'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(23) : error C2248: 'number' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(9) : see declaration of 'number'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(24) : error C2248: 'homeAddress' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(10) : see declaration of 'homeAddress'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(25) : error C2248: 'chinese' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(11) : see declaration of 'chinese'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(26) : error C2248: 'math' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(12) : see declaration of 'math'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(27) : error C2248: 'english' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(13) : see declaration of 'english'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(29) : error C2248: 'strName' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(7) : see declaration of 'strName'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(31) : error C2248: 'homeAddress' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(10) : see declaration of 'homeAddress'
E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(32) : error C2248: 'number' : cannot access private member declared in class 'studentClass'
        E:\C++\MSDev98\MyProjects\dfvg\jhg.cpp(9) : see declaration of 'number'
执行 cl.exe 时出错.

dfvg.exe - 1 error(s), 0 warning(s)
我郁闷错了那么多..............
2 回复
#2
flyingcloude2009-10-06 16:54
private:
    string strName;
    string strClass;
    int number;
    string homeAddress;
    int chinese;
    int math;
    int english;
你这些都是private啊,只有类本省的方法才能访问到,在main函数中是访问不到的啊,所以就会报错了
要么把private改成public
#3
a6320340792009-10-06 20:25
.................没人会吗???
1