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

C++编译报错undefined reference到Fish/Cat类构造函数如何解决

C++链接报错问题分析与修复

UML类图

报错原因

你遇到的是GCC链接阶段的符号找不到错误,核心原因有两个:

  • 你在Cat、Fish类中只声明了四个构造方法(Fish(std::string)、Fish()、Cat(std::string)、Cat())和虚函数eat(),没有给出对应的实现代码,链接器无法找到这些方法的定义,直接抛出undefined reference错误。
  • 代码还存在两处逻辑错误会导致运行结果异常:
    1. Animal基类的构造函数赋值逻辑写反,原本是要把传入的legNumbers赋值给成员变量legs,你写反成了legNumbers = legs;,会导致legs始终是随机脏值
    2. Pet类的getName、setName方法是空实现,没有关联成员变量name,会导致后续输出名字为空

修复方案

你需要补充缺失的方法实现,同时修正逻辑错误,修复后的完整代码如下:

#include <iostream>
using namespace std;
class Animal  // 定义基类
{
protected:
    int legs;        // 基类属性
public:
    Animal(int legNumbers)    // 设置腿的数量
    {
        legs = legNumbers;   // 修正赋值逻辑
    }
    virtual void eat() = 0;  // 基类方法
    virtual void walk() {}; // 基类方法

};
class Pet      // 定义宠物类
{
protected:
    string name;       // 宠物类属性
public: 
    virtual string getName();       // 定义方法
    virtual string setName(string name);    // 设置名称
    virtual void play()          // 定义玩耍方法
    {
        cout << " garfield is playing now." << endl;  // 输出内容
    }
};
class Spider :public Animal   // 子类继承基类
{
public:
    Spider() :Animal(8)   // Spider类继承Animal类
    {
        cout << "animals with " << legs << " legs is walking. " << endl;
    }
    virtual void eat()   // 定义虚方法
    {
        cout << "spider is eating now. " << endl;
    }
};
class Cat :public Pet, public Animal  // Cat类继承两个类
{
public:
    Cat(string name);   // 设置名称方法
    Cat();       
    virtual void play()   // 定义方法
    { 
        cout << name << " is playing now. " << endl;
    }
    virtual void eat();     // 在此定义方法

};
class Fish : public  Pet, public Animal  // Fish类继承两个类
{
public:        // 定义公有成员
    Fish(string name);
    Fish();
    virtual void play()
    {
        cout << name << " is playing now. " << endl;
    }
    virtual void eat();   // 方法在此
    void walk() 
    {
        cout << " Fish cannot walk " << endl;  // 输出内容
    }
};

// 修正Pet类成员方法实现
string Pet::getName()
{
    return name;
}
string Pet::setName(string _name)
{
    name = _name;
    return name;
}

// 补充Cat类缺失的方法实现
Cat::Cat():Animal(4) {
    name = "无名小猫";
}
Cat::Cat(string _name):Animal(4) {
    name = _name;
}
void Cat::eat() {
    cout << name << " is eating fish. " << endl;
}

// 补充Fish类缺失的方法实现
Fish::Fish():Animal(0) {
    name = "无名小鱼";
}
Fish::Fish(string _name):Animal(0) {
    name = _name;
}
void Fish::eat() {
    cout << name << " is eating fish food. " << endl;
}

int main(int argc, char* argv[])   // 定义主方法
{
    Fish* f = new Fish("Jaws");
    Cat* c = new Cat("Tenkir");
    Animal *a = new Fish();
    Animal* e = new Spider();
    Pet* p = new Cat();
    f->play();
    c->play();
    e->eat();
    e->walk();
    a->walk();
    p->play();
    return 0;
}

运行效果

修复后重新编译即可正常运行,输出如下:

animals with 8 legs is walking. 
Jaws is playing now. 
Tenkir is playing now. 
spider is eating now. 
animals with 8 legs is walking. 
 Fish cannot walk 
无名小猫 is playing now. 

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 15:51:01