我一直在为我的班级创建 Shape 类文件,直到增加了大约 15 行代码,一切都很顺利。当我创建一个“矩形”对象时,我得到了标准的“预期类型说明符”之一。创建其他两个类(三角形和圆形)的对象可以完美地工作。我注意到它在我添加第二个向量(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” _ “”部分。