56
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:

error: expected type-specifier before 'Rect2f'
error: expected ')' before '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.

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;

};

Source of Error Function and File Header (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)

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

4

2 回答 2

208

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

这可能是由多种方式引起的:

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

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

于 2012-03-22T20:19:57.057 回答
7

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

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

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

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

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

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

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

于 2012-01-13T02:33:45.167 回答