dm是私有变量,在main函数中不能直接使用,如果想使用,可以将dm改为public的。
class C {public: int fn1( int x ) {
dm = x ; return 0; }
public: int dm; };
void main ( ) {
C c;
c.fn1(16 );
cout << c.dm << ‘\n’;
}
或者在类成员中,添加一个函数,用来返回dm的值。
class C {public: int fn1( int x ) {
dm = x ; return 0;}
int get(){return dm;}
private: int dm; };
void main ( ) {
C c;
c.fn1(16 );
cout << c.get() << ‘\n’;
}
第二种方法更好一些,因为保护了数据成员。
第一个函数,没有返回值,而你定义的函数应该是有一个int类型的返回值的。
C c = new C(),而不是直接C c
c.dm是不能用的吧,既然这是一个private属性。
感觉是有以上三个问题。