2019.1.11 c++学习错误总结

  1. 定义类的的方法时,函数名不能跟内容重合。
  2. 在使用函数默认参数时,定义某个默认参数时,之后所有的参数必须都指定默认参数。
  3. 写公有继承的继承类构造函数时,出现错误: 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

猜你喜欢

转载自blog.csdn.net/weixin_43233774/article/details/86318148