Search code examples
c++classinheritancecompiler-errorsidentifier

Error: Expected a Type Specifier. C++ Inheritence


I've been working on creating Shape class files for my class, and up until about 15 additional lines of code, everything was going well. I'm getting one of the standard "Expected type specifier" when I create a 'Rectangle' object. Creating objects of the other two classes (Triangle and Circle) work perfectly. I noticed it bugged out as soon as I added the second vector (shapesTest2), so maybe it has something to do with that?

Specifically, the lines in question are:

shapes.push_back(new Rectangle(1, 2, 3, 4, Blue));
shapesTest2.push_back(new Rectangle(11, 22, 33, 44, Black));

The error list says:

    IntelliSense: expected a type specifier     29
    IntelliSense: expected a type specifier     30
Error 1 error C2661: 'std::vector<_Ty>::push_back' : no overloaded function takes 5 arguments   31
Error 2 error C2143: syntax error : missing ';' before ')' 31
Error 3 error C2061: syntax error : identifier 'Rectangle' 31   

Anyways, here's the code in the main.cpp file.:

// main.cpp - Shape class test program
// Written by _______
#include <vector>
#include <Windows.h>
#include "Circle.h"
#include "Triangle.h"
#include "Rectangle.h"


using namespace std;

void main()
{
    // Container of Shapes
    vector<Shape*> shapes;
    vector<Shape*> shapesTest2; // Used for second test case of Move and Scale.

    // Must allocate my object on heap now

    Circle *myCircle = new Circle(10, 10, 100, Red);
    shapes.push_back(myCircle);

    // Create new, unnamed stack-allocated instance of a Circles and push_back() to vector
    shapesTest2.push_back(new Circle(20, 20, 20, Red));

    // Populate the Container with 2 Rectangles

    shapes.push_back(new Rectangle(1, 2, 3, 4, Blue));
    shapesTest2.push_back(new Rectangle(11, 22, 33, 44, Black));

    // Populate the Container with 2 Triangles

    shapes.push_back(new Triangle(3, 4, 5, 7, 15, 4, Black));
    shapesTest2.push_back(new Triangle(6, 7, 9, 8, 43, 15, Green));

// There's more to the file, but this is the only time this pops up, and the rest is
// just messing around with the vector<Shape*>. I figured I'd try and save time and
// space by only posting what's needed, but if you think that the error is caused by
// code below, ask me and I'll upload the rest of this main.cpp file

}

And for reference, here's my Rectangle.h file:

#pragma once

#include <string>
#include "Shape.h"

using namespace std;

// Enum Colors = {Red, Blue, Green, Black, White}; is located in "Shapes.h"

class Rectangle : public Shape
{
public:
Rectangle(int x, int y, int width, int height, Colors color) : Shape(x, y, color)
{
    Width = width;
    Height = height;
}

virtual void Scale(float scaleFactor)
{
    Width = int(Width*scaleFactor);
    Height = int(Height*scaleFactor);
}

virtual void Draw() const // const b/c it doesn't alter Radius, X, Y, nor Color
{
    cout << "Rectangle of width " << Width << " and height " << Height << " with the top left corner at (" << X << ", " << Y << ") and color " << GetColor() << ".\n" << endl;
}

private:
int Width;
int Height;
};

Thanks for all the help guys, I've tried reading through all the other questions and it looked like people had just forgotten the '#include "_"' part of it.


Solution

  • The cause of the error is some sort of name conflict caused by the inclusion of windows.h. Remove the line

    #include <Windows.h>
    

    and everything compiles.

    Edit: To avoid such a conflict you can place your classes in a namespace. In the header write something like:

    namespace Foo { class Rectangle { ... }; }