错误:“ ClassName”之前的预期类型说明符 [英] Error: expected type-specifier before 'ClassName'

查看:204
本文介绍了错误:“ ClassName”之前的预期类型说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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))              );

我想了解为什么以上内容无法编译。无论出于何种原因,当我尝试创建Rect2f实例(确实从 Shape 类继承的实例时,指定了 shared_ptr 模板参数,就像 Circle )一样,出现以下错误

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 Shape class specified the shared_ptr template argument, just like Circle), I get the following errors:

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

关于 shared_ptr 的一切都很好。它没有问题;只是引起问题的 Rect2f

Everything about the Circle shared_ptr is perfectly fine. There are no problems with it; it's only the Rect2f which is causing issues.

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

Furthermore, the values passed into the constructor are valid.

那么,这个问题可能是什么呢?我为此添加了 Rect.h 。为简洁起见(以防万一我错过了该文件中可能影响此文件的某些内容),我将发布以下内容:

So, what could the issue be with this one? I have Rect.h included 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

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

};

错误功能和文件头(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.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();
}

问题

从提供的错误来看,我在这里错过了什么?另外,可以采取什么措施来纠正此问题?

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

推荐答案

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

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).

这可能有多种原因:


  • 您忘了 #include 定义它的标头。

  • 您的包含卫队( #ifndef BLAH_H )有缺陷(您的 #ifndef BLAH_H 与您的 #define BALH_H 由于输入错误或复制+粘贴错误)。

  • 您忘记了使用的模板(例如 new Vector()应该是 new Vector< int>()

  • 编译器认为您实际上是指一个范围(例如,如果您有 NamespaceA :: NamespaceB < global scope> :: NamespaceB ,如果您已经在 NamespaceA 之内,在 NamespaceA :: NamespaceB 中查找,并且不必费心检查< global scope> :: NamespaceB ),除非您显式访问它。

  • 您有一个名称冲突(两个具有相同名称的实体,例如一个类和一个枚举成员)。

  • You forgot to #include the header that defines it.
  • Your inclusion guards (#ifndef BLAH_H) are defective (your #ifndef BLAH_H doesn't match your #define BALH_H due 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::NamespaceB and 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).

要显式访问全局名称空间中的内容,请在其前面加上 :: 前缀,就好像全局名称空间是没有名称的名称空间(例如 :: MyType :: MyNamespace :: MyType )。

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

这篇关于错误:“ ClassName”之前的预期类型说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆