单个继承C ++和头文件 [英] Single Inheritance C++ and header files

查看:140
本文介绍了单个继承C ++和头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获得了一个

 预期的类名'{'token 


saltwaterfish.h头文件



  #ifndef SALTWATERFISH_H 
#define SALTWATERFISH_H


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

#endif // SALTWATERFISH_H

这是正确的定义方式头文件



saltwaterfish是鱼的子类?



cpp is

  #includefish.h
#includesaltwaterfish.h

fish.cpp类别:

  #includefish.h
#include< iostream>

#includefreshwaterfish.h
#includesaltwaterfish.h

using namespace std;

fish :: fish()
{
};

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

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

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


解决方案

包括 fish.h saltwaterfish.h中的头文件

  // saltwaterfish.h 
#includefish.h
#ifndef SALTWATERFISH_H
#define SALTWATERFISH_H


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

编译器没有看到Fish是一个类,因此错误。


I'm getting an

expected class-name before '{' token

in the following code

the saltwaterfish.h header file:

#ifndef SALTWATERFISH_H
#define SALTWATERFISH_H


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

#endif // SALTWATERFISH_H

Is this the correct way to define the header file for

"saltwaterfish is a subclass of fish" ?

the only thing I have in saltwaterfish.cpp is

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

the fish.cpp class:

#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;
}

解决方案

Include the fish.h header file in saltwaterfish.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

The compiler does not see that Fish is a class, hence the error.

这篇关于单个继承C ++和头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆