我正在

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/

10-11 23:08