- 定义类的的方法时,函数名不能跟类成员重合。
- 在使用函数默认参数时,定义某个默认参数时,之后所有的参数必须都指定默认参数。
- 写公有继承的继承类构造函数时,出现错误: undefined reference to `TableTennisPlayer::TableTennisPlayer(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, bool)’|
是因为基类的构造函数忘了写,导致无法调用基类构造函数
#include "tabtenn1.h"
#include <iostream>
TableTennisPlayer::TableTennisPlayer(const string & fn, const string & ln
, bool ht)//基类构造函数
{
firstname = fn;
lastname = ln;
hasTable = ht;
}
void TableTennisPlayer::Name()const
{
std::cout << lastname << ", " << firstname;
}
//RatedPlayer methods
RatedPlayer::RatedPlayer(unsigned int r, const string & fn, const string & ln,
bool ht ) : TableTennisPlayer(fn, ln, ht)//出现问题行
{
rating = r;
}
RatedPlayer::RatedPlayer(unsigned int r, const TableTennisPlayer & tp) : TableTennisPlayer(tp),rating(r)
{
}
问题来自C++primer plus 程序清单13.5