【发布时间】:2013-02-20 06:43:01
【问题描述】:
我已经为我的一个类的指针数据成员编写了复制构造函数
class person
{
public:
string name;
int number;
};
class MyClass {
int x;
char c;
std::string s;
person student;
MyClass::MyClass( const MyClass& other ) :
x( other.x ), c( other.c ), s( other.s ),student(other.student){}
};
但是当我运行这个程序时出现以下错误
错误:成员“MyClass”上的额外限定“MyClass::”[-fpermissive] 我是否正确使用了复制构造函数。
【问题讨论】:
-
正如错误消息所说,
MyClass::是不必要的。 -
(但只是因为您在类的主体中内联定义了构造函数。)
-
在您展示的任何一个类中,您似乎都没有指针数据成员。您的复制 ctor 会按成员进行复制。编译器生成的那个也是一样的。
标签: c++ copy-constructor assignment-operator