Error: Expected type-specifier before object

  c++

I’m new to C++. When I go to compile my code in a linux environment, I get the following error:

[email protected]:~/Documents/shortAssign04$ g++ main.cpp Circle.cpp Rectangle.cpp -o out
main.cpp: In function ‘int main()’:
main.cpp:4:18: error: expected type-specifier before ‘Rectangle’
   Shape* s = new Rectangle(10, 10);
                  ^~~~~~~~~
main.cpp:6:11: error: expected type-specifier before ‘Circle’
   s = new Circle(20);
           ^~~~~~

Here is my main.cpp file:

#include "Shape.h"

int main() {
  Shape* s = new Rectangle(10, 10);
  cout << "Rectangle Area: " << s->area() << endl;
  s = new Circle(20);
  cout << "Circle Area: " << s->area() << endl;
  delete s;

  return 0;
}

Here is my Rectangle.cpp file:

#include "Shape.h"

class Rectangle : virtual public Shape {
private:
  double width;
  double height;

public:
  Rectangle(double w, double h) {
    width = w;
    height = h;
  }
  double area() {
    return width * height;
  }
};

And here is my Circle class:

#include "Shape.h"

class Circle : virtual public Shape {
private:
  double radius;

public:
  Circle(double r) {
    radius = r;
  }
  double area() {
    return 3.141 * radius * radius;
  }
};

And here is my Shape header:

#ifndef SHAPE_H_
#define SHAPE_H_

#include <iostream>

using namespace std;

class Shape {
public:
  virtual double area() = 0;
};

#endif

I checked to make sure I’m including the header in all classes, that all pointer objects are referenced using ->, I’m not sure. However, I fixed the problem temporarily by typing #include <Rectangle.cpp> and #include <Circle.cpp> into my main class but I do not want to include .cpp files, only .h files. Does anyone have any suggestions on what I should do?

Source: Windows Questions C++

LEAVE A COMMENT