错误:预期类型说明符。 C ++继承

时间:2013-02-20 22:19:13

标签: c++ class inheritance compiler-errors identifier

我一直在为我的类创建Shape类文件,直到大约15行代码,一切都进展顺利。当我创建一个'Rectangle'对象时,我得到了一个标准的“Expected type specifier”。创建其他两个类(三角形和圆形)的对象可以完美地工作。一旦我添加了第二个向量(shapesTest2),我就注意到了它,所以它可能与它有关吗?

具体来说,有问题的行是:

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

错误列表显示:

    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   

无论如何,这是main.cpp文件中的代码。:

// 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

}

供参考,这是我的Rectangle.h文件:

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

感谢所有帮助人员,我已经尝试阅读所有其他问题,看起来人们刚刚忘记了'#include' _ “'的一部分。

1 个答案:

答案 0 :(得分:6)

错误的原因是由于包含windows.h而导致的某种名称冲突。删除行

#include <Windows.h>

并且一切都在编译。

修改 为避免此类冲突,您可以将类放在命名空间中。在标题中写下:

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