1

this is my first post here on stack overflow! I have used this website for years and I appreciate all of the help and time invested by everyone. It has been priceless in helping me write good code.

I get the following error message when I try to compile: "error: expected class name". I have searched the internet and I have been unsuccessful in finding a solution to my problem. I also went to "http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm" and "Single Inheritance C++ and header files" for help with inheritance and using header files. But still my problem persists and I don't know what I'm doing wrong. I have trimmed my code down to the bare bones and presented it below. Your help would be much appreciated.

Compiler

$ make
c++    -c -o RectangleClass.o RectangleClass.cpp
c++    -c -o ShapeClass.o ShapeClass.cpp
In file included from ShapeClass.cpp:1:
In file included from ./ShapeClass.h:4:
./RectangleClass.h:6:26: error: expected class name
class Rectangle : public Shape {
                     ^
1 error generated.
make: *** [ShapeClass.o] Error 1

makefile

lab8: RectangleClass.o ShapeClass.o main.o
$(CXX) $(CXXFLAGS) -o $@ $^ -ansi -pedantic -Wall -Wextra

main.o: main.cpp
RectangleClass.o: RectangleClass.h
ShapeClass.o: ShapeClass.h

.PHONY: clean
clean:
    $(RM) *.o
    $(RM) main RectangleClass ShapeClass lab8
    $(RM) *~

main.cpp

#include "ShapeClass.h"

int main(){
  return 0;
}

ShapeClass.h which is the parent class

#ifndef SHAPECLASS_H
#define SHAPECLASS_H

#include "RectangleClass.h"

class Shape {
 private:
  double area;

 public:
  Shape(double defaultArea = 0);

};

#endif

ShapeClass.cpp

#include "ShapeClass.h"

Shape::Shape (double inputArea) {
  area   = inputArea;
}

RectangleClass.h which is the derived class

#ifndef RECTANGLECLASS_H
#define RECTANGLECLASS_H

#include "ShapeClass.h"

class Rectangle : public Shape {
};

#endif

RectangleClass.cpp

#include "RectangleClass.h"
Community
  • 1
  • 1

1 Answers1

0

You (erroneously) #include "RectangleClass.h" inside ShapeClass.h.

At that point Shape is not defined yet and the RectangleClass class definition tries to inherit from something that is not yet defined. Thus the error.

lothar
  • 19,853
  • 5
  • 45
  • 59