c++ - 错误 : expected type-specifier before 'ClassName'

标签 c++ shared-ptr pure-virtual

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),我收到以下错误:

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

Circle shared_ptr 的一切都很好。它没有问题;只有 Rect2f 会导致问题。

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

那么,这个问题可能是什么?我为此包含了 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;

};

错误函数来源和文件头(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();
}

问题

从提供的错误来看,我在这里遗漏了什么?另外,有什么办法可以解决这个问题?

最佳答案

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

这可能是由多种原因引起的:

  • 您忘记了 #include定义它的 header 。
  • 您的包含保护 (#ifndef BLAH_H) 有缺陷(您的 #ifndef BLAH_H 与您的 #define BALH_H 不匹配,原因是拼写错误或复制+粘贴错误)。
  • 您的包含保护被意外使用了两次(两个单独的文件都使用 #define MYHEADER_H,即使它们位于不同的目录中)
  • 您忘记了您正在使用模板(例如,new Vector() 应该是 new Vector<int>())
  • 编译器认为你的意思是一个作用域,而实际上你的意思是另一个作用域(例如,如果你有 NamespaceA::NamespaceB<global scope>::NamespaceB ,如果你已经在 NamespaceA 内,它会查看 NamespaceA::NamespaceB并且不要费心检查 <global scope>::NamespaceB ),除非您明确访问它。
  • 您有名称冲突(两个具有相同名称的实体,例如一个类和一个枚举成员)。

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

关于c++ - 错误 : expected type-specifier before 'ClassName' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8845117/

相关文章:

c++ - 使用左右参数覆盖运算符

c++ - Boost 线程串行运行,而不是并行运行

c++ - 基类如何使用另一个父函数来满足父函数的纯虚函数的定义

c++ - 我们可以将 char[undetermined_during_compile_time_size] 保留在 boost::shared_ptr 中吗?

c++ - 使用 std::shared_ptr 时出现 malloc 错误

c++ - 如何删除从基类继承的纯虚函数?

c++ - 从模板类继承

c++ - 我该如何解决这个缓冲区溢出问题?

c++ - 使用 Cython 和 distutilis 方法从 Python 调用 C++ 代码

c++ - 将 shared_ptr 与 const 引用一起使用