0

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;
}
ahtmatrix
  • 539
  • 1
  • 7
  • 19

3 Answers3

2

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.

AndyG
  • 39,700
  • 8
  • 109
  • 143
balaji
  • 81
  • 6
0

You should put

#include "fish.h"

in the header file itself. (saltwaterfish.h)

Otherwise this code won't have access to fish class.

Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • Doesn't explain the problem though (unless it's in a different source file, not mentioned in the question). – barak manos Mar 19 '14 at 04:57
  • `#ifndef SALTWATERFISH_H` at the top of the code suggests that this code is in saltwaterfish.h – Tanmay Patil Mar 19 '14 at 04:59
  • @TanmayPatil is there a difference between putting `"fish.h"` and `` ? – ahtmatrix Mar 19 '14 at 05:06
  • 1
    Yes. means that the compiler will search in all include directories. On the other hand, "fish.h" will make the compiler search in the sources as well. – Tanmay Patil Mar 19 '14 at 05:10
  • Although this is compiler implementation dependent, more on this at http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename – Tanmay Patil Mar 19 '14 at 05:11
0

The error means "the thing before the { is supposed to be a class name, but it's not." So 'fish' is not a class name. Look for the declaration of the fish class, and make sure it's loaded before this declaration.

John Deters
  • 4,295
  • 25
  • 41