从书中复制的代码无法编译。已弃用或打字错误?
Code copied from a book won't compile. Deprecated or typo?
这是一本免费的 c++ 电子书中的程序,但无法编译。
起初我将它打出来,但意识到它无法编译后,我尝试从电子书中直接复制到代码块 13.12 中。
它仍然无法编译。这本电子书是 2010 年的,所以可能代码不符合当前标准,或者某处存在语法错字。
请帮我看看哪里出了问题。
错误是:
error: extra qualification 'Critter::' on member 'operator=' [-fpermissive]|
密码是:
#include <iostream>
using namespace std;
class Critter
{
public:
Critter(const string& name = "", int age = 0);
~Critter(); //destructor prototype
Critter(const Critter& c); //copy constructor prototype
Critter& Critter::operator=(const Critter& c);
op
void Greet() const;
private:
string* m_pName;
int m_Age;
};
Critter::Critter(const string& name, int age)
{
cout << "Constructor called\n";
m_pName = new string(name);
m_Age = age;
}
Critter::~Critter() //destructor definition
{
cout << "Destructor called\n";
delete m_pName;
}
Critter::Critter(const Critter& c) //copy constructor definition
{
cout << "Copy Constructor called\n";
m_pName = new string(*(c.m_pName));
m_Age = c.m_Age;
}
Critter& Critter::operator=(const Critter& c)
{
cout << "Overloaded Assignment Operator called\n";
if (this != &c)
{
delete m_pName;
m_pName = new string(*(c.m_pName));
m_Age = c.m_Age;
}
return *this;
}
void Critter::Greet() const
{
cout << "I’m " << *m_pName << " and I’m " << m_Age << " years old.\n";
cout << "&m_pName: " << cout << &m_pName << endl;
}
void testDestructor();
void testCopyConstructor(Critter aCopy);
void testAssignmentOp();
int main()
{
testDestructor();
cout << endl;
Critter crit("Poochie", 5);
crit.Greet();
testCopyConstructor(crit);
crit.Greet();
cout << endl;
testAssignmentOp();
return 0;
}
void testDestructor()
{
Critter toDestroy("Rover", 3);
toDestroy.Greet();
}
void testCopyConstructor(Critter aCopy)
{
aCopy.Greet();
}
void testAssignmentOp()
{
Critter crit1("crit1", 7);
Critter crit2("crit2", 9);
crit1 = crit2;
crit1.Greet();
crit2.Greet();
cout << endl;
Critter crit3("crit", 11);
crit3 = crit3;
crit3.Greet();
}
如消息所说,
中多了一个Critter::
Critter& Critter::operator=(const Critter& c);
因为它在 class 声明中,所以它必须是 class 的成员并且不需要以 class 名称作为前缀。
Critter& operator=(const Critter& c);
是正确的形式。
class 名称前缀仅在成员定义在外部 class 时使用,如示例后面的代码。
从该运算符原型中删除 Critter::
这是一本免费的 c++ 电子书中的程序,但无法编译。 起初我将它打出来,但意识到它无法编译后,我尝试从电子书中直接复制到代码块 13.12 中。 它仍然无法编译。这本电子书是 2010 年的,所以可能代码不符合当前标准,或者某处存在语法错字。 请帮我看看哪里出了问题。
错误是:
error: extra qualification 'Critter::' on member 'operator=' [-fpermissive]|
密码是:
#include <iostream>
using namespace std;
class Critter
{
public:
Critter(const string& name = "", int age = 0);
~Critter(); //destructor prototype
Critter(const Critter& c); //copy constructor prototype
Critter& Critter::operator=(const Critter& c);
op
void Greet() const;
private:
string* m_pName;
int m_Age;
};
Critter::Critter(const string& name, int age)
{
cout << "Constructor called\n";
m_pName = new string(name);
m_Age = age;
}
Critter::~Critter() //destructor definition
{
cout << "Destructor called\n";
delete m_pName;
}
Critter::Critter(const Critter& c) //copy constructor definition
{
cout << "Copy Constructor called\n";
m_pName = new string(*(c.m_pName));
m_Age = c.m_Age;
}
Critter& Critter::operator=(const Critter& c)
{
cout << "Overloaded Assignment Operator called\n";
if (this != &c)
{
delete m_pName;
m_pName = new string(*(c.m_pName));
m_Age = c.m_Age;
}
return *this;
}
void Critter::Greet() const
{
cout << "I’m " << *m_pName << " and I’m " << m_Age << " years old.\n";
cout << "&m_pName: " << cout << &m_pName << endl;
}
void testDestructor();
void testCopyConstructor(Critter aCopy);
void testAssignmentOp();
int main()
{
testDestructor();
cout << endl;
Critter crit("Poochie", 5);
crit.Greet();
testCopyConstructor(crit);
crit.Greet();
cout << endl;
testAssignmentOp();
return 0;
}
void testDestructor()
{
Critter toDestroy("Rover", 3);
toDestroy.Greet();
}
void testCopyConstructor(Critter aCopy)
{
aCopy.Greet();
}
void testAssignmentOp()
{
Critter crit1("crit1", 7);
Critter crit2("crit2", 9);
crit1 = crit2;
crit1.Greet();
crit2.Greet();
cout << endl;
Critter crit3("crit", 11);
crit3 = crit3;
crit3.Greet();
}
如消息所说,
中多了一个Critter::
Critter& Critter::operator=(const Critter& c);
因为它在 class 声明中,所以它必须是 class 的成员并且不需要以 class 名称作为前缀。
Critter& operator=(const Critter& c);
是正确的形式。
class 名称前缀仅在成员定义在外部 class 时使用,如示例后面的代码。
从该运算符原型中删除 Critter::