【问题标题】:Error: Expected a Type Specifier. C++ Inheritence错误:需要类型说明符。 C++ 继承
【发布时间】: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


【解决方案1】:

错误的原因是包含windows.h 引起的某种名称冲突。删除线

#include <Windows.h>

一切都会编译。

编辑: 为避免这种冲突,您可以将类放在命名空间中。在标题中写下类似的内容:

命名空间 Foo { 类矩形 { ... }; }

【讨论】:

  • 名称冲突?真可惜,我想使用该库中的睡眠功能。感谢您的帮助,效果很好。由于我没有 15 个代表,有人为我投票吗?
  • 您可以将所有代码包装在一个命名空间中(无论如何都是一个好习惯),或者您可以将 Rectangle 重命名为其他名称(MyRectangle?)。
  • 我把它改成了 Rectangles.h,它似乎工作了^.^。将我的所有代码包装在命名空间中是什么意思,例如 Rectangles::Rectangles(1, 2, 3, 4, Blue);? --编辑:看起来托马斯编辑了他的答案^.^,再次感谢
  • 命名空间有助于防止名称冲突。当您的符号位于全局命名空间中时,它更有可能发生名称冲突。就像标准库中的符号在 std 命名空间中一样,您可以将文件放在它们自己的命名空间中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多