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

程序编译出现错误,求解!

全世界安静 发布于 2012-06-12 21:57, 586 次点击
#include <iostream.h>
class test
{
    char get()
    {
        return a;
    }
    set(char c)
    {
        a=c;
    }
    private:
       char a;
   
};
void main()
{
    test my_test;
    my_test.set('X');
}
编译出现错误:error C2248: 'set' : cannot access private member declared in class 'test'
求解!
7 回复
#2
mathspanda2012-06-13 08:59
你应该要在get函数之前加个public声明
不声明的话编译器自动默认它为private型的
也就不能调用get和set了
#3
m21wo2012-06-13 12:20
类的默认权限是 私有  结构体的默认权限是 公有
#4
cwj9762779162012-06-14 00:17
字符a是test类的私有成员变量,类的成员函数set不能对其进行赋值。set成员函数的作用域只限于它本身里面。懂了没有?
#5
yang400b2012-06-14 13:41
#include <iostream>
using namespace std;
class test
{ public:
    char get()
    {
        return a;
    }
    void set(char c)
    {
        a=c;
    }
  private:
       char a;  
   
};
void main()
{
    test my_test;
    my_test.set('X');
}
#6
luojunyjh2012-06-16 10:12
回复 4楼 cwj976277916
你果然是新手,不懂的话就不要误导别人
#7
yang400b2012-06-16 10:58
什么啊。我编译对了才发的。  你不也是新手吗。
#8
yueye09192012-06-20 14:25
我是新手
1