这是一个免费的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();
}
答案 0 :(得分:2)
正如消息所述,
中还有一个$('#userTable button').click(function(){
$buttonNo = $(this).val();
...
});
Critter::
由于这是在类声明中,它必须是类的成员,并且不需要以类名作为前缀。
Critter& Critter::operator=(const Critter& c);
是正确的表格。
只有当成员在外部类中定义时才使用类名前缀,就像在示例后面的代码中一样。
答案 1 :(得分:1)
从该运算符原型中删除Critter::