c++ - 单继承 C++ 和头文件

标签 c++ inheritance

我得到一个

expected class-name before '{' token

在下面的代码中

saltwaterfish.h 头文件:

#ifndef SALTWATERFISH_H
#define SALTWATERFISH_H


class saltwaterfish: public fish
{
public:
    saltwaterfish();
    float GetUnitPrice();
    float GetWeight();
};

#endif // SALTWATERFISH_H

这是定义头文件的正确方法吗

“咸水鱼是鱼的一个亚类”?

我在 saltwaterfish.cpp 中唯一的东西是

#include "fish.h"
#include "saltwaterfish.h"

fish.cpp 类:

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

#include "freshwaterfish.h"
#include "saltwaterfish.h"

using namespace std;

fish::fish()
{
};

float fish::CalculatePrice()
{
    return GetUnitPrice()*GetWeight();
}

float fish::GetUnitPrice()
{
    return 1.0;
}

float fish::GetWeight()
{
    return 1.0;
}

最佳答案

在saltwaterfish.h中包含fish.h头文件

//saltwaterfish.h
#include"fish.h"
#ifndef SALTWATERFISH_H
#define SALTWATERFISH_H


class saltwaterfish: public fish
{
public:
    saltwaterfish();
    float GetUnitPrice();
    float GetWeight();
};
#endif // SALTWATERFISH_H

编译器看不到 Fish 是一个类,因此出现错误。

关于c++ - 单继承 C++ 和头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22496468/

相关文章:

c++ - 在 winform 中使用 DrawImage

Delphi接口(interface)继承: Why can't I access ancestor interface's members?

c++ - 如何使用 condition_variable 来真正 wait_for 不超过一定的持续时间

c++ - 使用 C++ 打印所有目录

c++ - 当我制作一个父类的 vector 时,如果我把一个 child 放在那里, child 的成员会丢失吗?

python - 如何使用继承在python中定义父对象?

c++ - 从派生的复制构造函数调用基的移动构造函数

java - 使用仅调用重写的父类(super class)方法的子类方法有好处吗?

c++ - 使用毕达哥拉斯定理的碰撞检测不可靠?

c++ - 如何自定义监 window 口格式