You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++类继承代码编译链接错误求助,附代码及报错信息

C++ 类继承项目编译报错求助

问题背景

正在学习C++类继承,复制讲师代码后无法编译,项目包含2个.cpp和2个.h文件,以下是所有代码及报错信息:

代码文件

Main.cpp

#include  <iostream>
#include "player.h"

int main(){
    Player p1("Basketball");
    std::cout <<"player : " << p1 <<std::endl;

    return 0;
}

person.h

#include <iostream>
#include <string>
#include <string_view>

class Person {
    friend std::ostream& operator << (std::ostream& out, const Person& person);
    public:
    Person();
    Person(std::string& first_name_param, std::string& last_name_param);
    ~Person();

    std::string get_first_name() const{
        return first_name;
    }

    std::string get_last_name() const{
        return last_name;
    }

    private:
    std::string first_name{"Mysterious"};
    std::string last_name{"Person"};
};

player.h

#include <iostream>
#include <string>
#include <string_view>
#include "person.h"

class Player : public Person{
    friend std::ostream& operator << (std::ostream& out, const Player& player);
public:
    Player() = default;
    Player(std::string_view game_param);
    
private:
    std::string m_game{"None"};

};

player.cpp

#include "person.h"
#include "player.h"


Player::Player(std::string_view game_param)
: m_game(game_param){

}

std::ostream& operator<<(std::ostream& out, const Player& player){
    out << "Player: [ game: " << player.m_game << " names: " << Player.get_first_name() << " " << Player.get_last_name() << "]";
    return out;
}

person.cpp

#include "person.h"
 
 Person::Person(){}

Person::Person(std::string& first_name_param, std::string& last_name_param)
    : first_name(first_name_param), last_name(last_name_param) {
    }

std::ostream& operator << (std::ostream& out, const Person& person){
    out << "Person [" << person.first_name << " " << person.last_name << "]";
    return out;
}

Person::~Person(){

}

报错信息

Executing task: C/C++: g++.exe build active file 

Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\DustinB\CodingClass\Inheritance\main.cpp -o C:\Users\DustinB\CodingClass\Inheritance\main.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DustinB\AppData\Local\Temp\ccIpfZtM.o:C:\Users\DustinB\CodingClass\Inheritance/main.cpp:11: undefined reference to `Player::Player(std::basic_string_view<char, std::char_traits<char> >)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DustinB\AppData\Local\Temp\ccIpfZtM.o: in function `main':
C:\Users\DustinB\CodingClass\Inheritance/main.cpp:12: undefined reference to `operator<<(std::ostream&, Player const&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DustinB\AppData\Local\Temp\ccIpfZtM.o: in function `Player::~Player()':
C:\Users\DustinB\CodingClass\Inheritance/player.h:6: undefined reference to `Person::~Person()'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

 *  The terminal process terminated with exit code: -1. 

问题解决

1. 编译命令错误(核心原因)

当前编译命令只编译了main.cpp,没有将person.cpp和player.cpp纳入编译链接流程,导致链接器找不到这些文件中实现的函数、构造函数和析构函数。

正确编译命令:

g++.exe -fdiagnostics-color=always -g main.cpp person.cpp player.cpp -o main.exe

2. 代码语法错误

在player.cpp的operator<<重载函数中,错误地使用类名Player调用成员函数,成员函数需要通过对象实例调用,应改为player.get_first_name()和player.get_last_name():

修正后的player.cpp代码片段:

std::ostream& operator<<(std::ostream& out, const Player& player){
    out << "Player: [ game: " << player.m_game << " names: " << player.get_first_name() << " " << player.get_last_name() << "]";
    return out;
}

3. 额外优化建议

  • 头文件包含保护:在所有头文件中添加包含保护,防止重复包含导致的编译错误,例如在person.h中:
    #ifndef PERSON_H
    #define PERSON_H
    // 原头文件内容
    #endif // PERSON_H
    
    player.h同理添加对应保护宏。
  • 构造函数参数优化:将Person构造函数的参数改为const std::string&或std::string_view,这样可以兼容临时字符串,避免不必要的拷贝:
    // 修改person.h中的构造函数声明
    Person(const std::string& first_name_param, const std::string& last_name_param);
    // 或者更高效的版本
    Person(std::string_view first_name_param, std::string_view last_name_param);
    
    同时对应修改person.cpp中的构造函数实现。

内容的提问来源于stack exchange,提问作者Dustin Boatman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 05:31:18