简体   繁体   English

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

[英]Error: expected type-specifier before '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))              );

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 :无论出于何种原因,当我尝试创建 Rect2f 实例(它确实从Shape类继承指定了shared_ptr模板参数,就像Circle一样)时,我收到以下错误

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

Everything about the Circle shared_ptr is perfectly fine.关于Circle shared_ptr一切都非常好。 There are no problems with it;它没有问题; it's only the Rect2f which is causing issues.只有Rect2f导致了问题。

Furthermore, the values passed into the constructor are valid.此外,传递给构造函数的值是有效的。

So, what could the issue be with this one?那么,这可能是什么问题呢? I have Rect.h included for this.Rect.h包含了Rect.h For brevity's sake (and incase I missed something in that file which may be affecting this), I'll post the following:为简洁起见(以防万一我遗漏了该文件中可能影响此的内容),我将发布以下内容:

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?另外,可以做些什么来纠正这个问题?

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 #include the header that defines it.您忘记#include定义它的标头。
  • 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).您的包含保护 ( #ifndef BLAH_H ) 有缺陷(您的#ifndef BLAH_H与您的#define BALH_H不匹配,因为输入错误或复制+粘贴错误)。
  • Your inclusion guards are accidentally used twice (two separate files both using #define MYHEADER_H , even if they are in separate directories)您的包含守卫被意外使用了两次(两个单独的文件都使用#define MYHEADER_H ,即使它们位于不同的目录中)
  • You forgot that you are using a template (eg. new Vector() should be new Vector<int>() )您忘记了您正在使用模板(例如new Vector()应该是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.编译器认为你指的是一个范围,而实际上你指的是另一个范围(例如,如果你有NamespaceA::NamespaceB和一个<global scope>::NamespaceB ,如果你已经在NamespaceA ,它会在NamespaceA::NamespaceB查找NamespaceA::NamespaceB并且不费心检查<global scope>::NamespaceB ) 除非您显式访问它。
  • You have a name clash (two entities with the same name, such as a class and an enum member).您有名称冲突(两个具有相同名称的实体,例如类和枚举成员)。

To explicitly access something in the global namespace, prefix it with :: , as if the global namespace is a namespace with no name (eg ::MyType or ::MyNamespace::MyType ).要显式访问全局命名空间中的某些内容,请为其添加::前缀,就好像全局命名空间是一个没有名称的命名空间(例如::MyType::MyNamespace::MyType )。

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的文件。

For future readers in which the top answer still doesn't resolve the issue, this issue can also occur when you have an enum and a class in the same namespace and one of your enum entries accidentally has the same name as the class. 对于将来的读者来说,最重要的答案仍然不能解决问题,当您在同一个命名空间中有一个枚举和一个类并且您的一个枚举条目偶然与该类具有相同的名称时,也会发生此问题。

If a source file includes both the class header and the header that has the enum (it can be a singular header that defines both) then this will happen. 如果源文件同时包含类标头和具有枚举的标头(可以是定义两者的单个标头),则将发生这种情况。

From the discussion from the first answer: http://chat.stackoverflow.com/rooms/6681/discussion-between-holland-and-anton-golov 来自第一个答案的讨论: http : //chat.stackoverflow.com/rooms/6681/discussion-between-holland-and-anton-golov

Anton: It gives me the error "cannot allocate an object of abstract type 'Rect2f' because the >following virtual functions are pure within 'Rect2f': virtual bool Shape::Intersects(const >Circle&) const". Anton:它给我一个错误“无法分配抽象类型为'Rect2f'的对象,因为>下列虚拟函数在'Rect2f'中是纯净的:virtual bool Shape :: Intersects(const> Circle&)const”。

It looks like you're missing this function from your Rect2f class: 看来您的Rect2f类缺少此功能:

virtual bool Intersects( const Circle& c ) const;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM