C++ 错误:“ClassName”之前的预期类型说明符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8845117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:13:09  来源:igfitidea点击:

Error: expected type-specifier before 'ClassName'

c++shared-ptrpure-virtual

提问by zeboidlund

shared_ptr<Shape> circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0)));

shared_ptr<Shape> rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0,
                                  Vec3f(1.0f, 1.0f, 0))              );

I'm trying to understand why the above won't compile. For Whatever reason, when I try to create an instance of Rect2f (which DOES inherit from the Shapeclass specified the shared_ptrtemplate argument, just like Circle), I get the following errors:

我试图理解为什么上面不能编译。无论出于何种原因,当我尝试创建 Rect2f 的实例(它确实从Shape指定shared_ptr模板参数的类继承,就像Circle)时,我收到以下错误

error: expected type-specifier before 'Rect2f'
error: expected ')' before 'Rect2f'

Everything about the Circleshared_ptris perfectly fine. There are no problems with it; it's only the Rect2fwhich is causing issues.

关于的一切都Circleshared_ptr很好。它没有问题;这只是Rect2f导致问题的原因。

Furthermore, the values passed into the constructor are valid.

此外,传递给构造函数的值是有效的。

So, what could the issue be with this one? I have Rect.hincluded for this. For brevity's sake (and incase I missed something in that file which may be affecting this), I'll post the following:

那么,这可能是什么问题呢?我已经Rect.h包括在内。为简洁起见(以防万一我遗漏了该文件中可能影响此的内容),我将发布以下内容:

Rect.h

矩形.h

#pragma once

#include <QGLWidget>
#include <GL/glext.h>
#include <cmath>
#include <QDebug>
#include "Shape.h"
#include "Vec3f.h"
#include "rand.h"

const int DEFAULT_SQUARE_WIDTH = 5;
const int DEFAULT_SQUARE_HEIGHT = 5;

typedef enum {
    RC_TOPLEFT,
    RC_TOPRIGHT,
    RC_BOTTOMRIGHT,
    RC_BOTTOMLEFT
} RectangleCorner;

class Rect2f : public Shape
{
public:

    Rect2f(
        Vec2f center = Vec2f(),
        float width = 4,
        float height = 4,
        float radius = 0,
        Vec3f color = Vec3f()
    );

    ~Rect2f();

    inline const float GetWidth( void ) const {
        return mWidth;
    }

    inline const float GetHeight( void ) const {
        return mHeight;
    }

    inline const Vec2f* GetCorner( RectangleCorner rc ) const {
        switch( rc ) {
        case RC_TOPLEFT:
            return mTopLeftCrnr;
            break;

        case RC_TOPRIGHT:
            return mTopRightCrnr;
            break;

        case RC_BOTTOMLEFT:
            return mBottomLeftCrnr;
            break;

        case RC_BOTTOMRIGHT:
            return mBottomRightCrnr;
            break;
        }
    }

    void UpdateCorners();

    virtual void Collide( Shape &s );

    virtual void Collide( Rect2f &r );

    virtual void Collide ( Circle &c );

    virtual bool Intersects( const Shape& s ) const;

    virtual bool Intersects( const Rect2f& s ) const;

    virtual bool IsAlive( void ) const;

    virtual float Mass( void ) const;

protected:

   virtual void Draw( void ) const;
   float mWidth, mHeight;
   Vec3f mColor;

private:
   Vec2f* mTopLeftCrnr;
   Vec2f* mTopRightCrnr;
   Vec2f* mBottomLeftCrnr;
   Vec2f* mBottomRightCrnr;

};

Source of Error Function and File Header (mainwindow.cpp)

错误函数和文件头的来源(mainwindow.cpp)

#include "ui_mainwindow.h"
#include "Vec2f.h"
#include "Rect2f.h"
#include "SharedPtr.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi( this );

    BoxOfShapes* display = new  InteractiveBoxOfShapes( this, 600, 600 );
    ui->mGridLayout->addWidget( display );
    ui->mStatusBar->showMessage( "status ok" );

    QObject::connect( display, SIGNAL( numShapesChanged( int ) ), this, SLOT( setNumShapes(int) ) );
    QObject::connect( ui->mResetButton, SIGNAL( pressed() ), display, SLOT( resetWorld() ) );
    shared_ptr<Shape> circle(
                new Circle(
                    Vec2f( 0, 0 ),
                    0.1,
                    Vec3f( 1, 0, 0 ) ) );
    /*std::shared_ptr<Shape> rect( <---error
                     new Rect2f(
                        Vec2f( 0, 0 ),
                        5.0f,
                        5.0f,
                        0,
                        Vec3f( 1.0f, 1.0f, 0 )
                    )
                );*/
    shared_ptr< Shape > rect( new Rect2f() ); //error - second attempt 
    display->addShape( circle );
    display->addShape( rect );
}

Main Function Test (main.cpp)

主要功能测试(main.cpp)

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "Rect2f.h"

int main(int argc, char *argv[])
{
    Rect2f rec;

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Question

Judging from the errors provided, what am I missing here? Also, what can be done to rectify this issue?

从提供的错误来看,我在这里遗漏了什么?另外,可以做些什么来纠正这个问题?

回答by Jamin Grey

For future people struggling with a similar problem, the situation is that the compiler simply cannot find the type you are using (even if your Intelisense can find it).

对于未来遇到类似问题的人来说,情况是编译器根本找不到您正在使用的类型(即使您的 Intelisense 可以找到它)。

This can be caused in many ways:

这可以通过多种方式引起:

  • You forgot to #includethe header that defines it.
  • Your inclusion guards (#ifndef BLAH_H) are defective (your #ifndef BLAH_Hdoesn't match your #define BALH_Hdue to a typo or copy+paste mistake).
  • You forgot that you are using a template (eg. new Vector()should be new Vector<int>())
  • The compiler is thinking you meant one scope when really you meant another (For example, if you have NamespaceA::NamespaceB, AND a <global scope>::NamespaceB, if you are already within NamespaceA, it'll look in NamespaceA::NamespaceBand not bother checking <global scope>::NamespaceB) unless you explicitly access it.
  • You have a name clash (two entities with the same name, such as a class and an enum member).
  • 你忘记#include了定义它的标题。
  • 您的包含守卫 ( #ifndef BLAH_H) 有缺陷(由于拼写错误或复制+粘贴错误,您的内容#ifndef BLAH_H与您的不匹配#define BALH_H)。
  • 您忘记了您正在使用模板(例如new Vector()应该是new Vector<int>()
  • 编译器认为您指的是一个作用域,而实际上您指的是另一个作用域(例如,如果您有NamespaceA::NamespaceB, 并且 a <global scope>::NamespaceB,如果您已经在 内NamespaceA,它会查看NamespaceA::NamespaceB而不需要检查<global scope>::NamespaceB),除非您明确访问它。
  • 您有名称冲突(两个具有相同名称的实体,例如类和枚举成员)。

To explicitly access something in the global namespace, prefix it with ::, as if the global namespace is a namespace with no name (e.g. ::MyTypeor ::MyNamespace::MyType).

要显式访问全局命名空间中的某些内容,请为其添加前缀::,就好像全局命名空间是一个没有名称的命名空间(例如::MyType::MyNamespace::MyType)。

回答by Anton Golov

First of all, let's try to make your code a little simpler:

首先,让我们试着让你的代码更简单一点:

// No need to create a circle unless it is clearly necessary to
// demonstrate the problem

// Your Rect2f defines a default constructor, so let's use it for simplicity.
shared_ptr<Shape> rect(new Rect2f());

Okay, so now we see that the parentheses are clearly balanced. What else could it be? Let's check the following code snippet's error:

好的,现在我们看到括号是明显平衡的。还能是什么?让我们检查以下代码片段的错误

int main() {
    delete new T();
}

This may seem like weird usage, and it is, but I really hate memory leaks. However, the output does seem useful:

这可能看起来很奇怪,确实如此,但我真的很讨厌内存泄漏。但是,输出似乎很有用:

In function 'int main()':
Line 2: error: expected type-specifier before 'T'

Aha! Now we're just left with the error about the parentheses. I can't find what causes that; however, I think you are forgetting to include the file that defines Rect2f.

啊哈!现在我们只剩下关于括号的错误。我找不到是什么原因造成的;但是,我认为您忘记包含定义Rect2f.