【发布时间】:2013-02-20 22:19:12
【问题描述】:
我一直在为我的班级创建 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 "_"' 部分。
【问题讨论】:
-
供将来参考:请发布完整的错误。许多c++ 常客可以阅读错误的字里行间并找出重要的东西。
-
发布完整错误是什么意思?
-
应该有比“预期类型说明符”更多的细节。通常错误涵盖多行并具有行/列编号。你确定错误没有说“
之前的预期类型说明符”吗? -
您的矩形类不包含 color.h 文件。编译器在编译 rectangle.obj 文件时如何找到
Colors?Colors是包含在Circle类中还是在该类之外? -
它是“Shape.h”父类的一部分,很抱歉造成混淆。我们在课堂上和教授一起写这篇文章,看起来我们都忘了删除那条评论。将修复 ^.^ 还将错误列表复制到此处,希望它更有意义。当我在 VS2012 中输入时,自动完成功能识别出 Rectangle 类存在,但它看起来好像没有看到构造函数。
标签: c++ class inheritance compiler-errors identifier