zeb*_*und 45 c++ pure-virtual shared-ptr
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)) );
Run Code Online (Sandbox Code Playgroud)
我试图理解为什么以上不会编译.无论出于什么原因,当我尝试创建一个Rect2f实例(其中DOES继承自Shape指定shared_ptr模板参数的类时,就像Circle),我得到以下错误:
error: expected type-specifier before 'Rect2f'
error: expected ')' before 'Rect2f'
Run Code Online (Sandbox Code Playgroud)
关于这一切的一切都Circle shared_ptr很好.它没有问题; 它只是Rect2f导致问题的原因.
此外,传递给构造函数的值是有效的.
那么,这个问题会出现什么问题呢?我已经Rect.h包含了这个.为了简洁起见(并且我错过了该文件中可能影响此内容的内容),我将发布以下内容:
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;
};
Run Code Online (Sandbox Code Playgroud)
错误函数和文件头的来源(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 );
}
Run Code Online (Sandbox Code Playgroud)
主要功能测试(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();
}
Run Code Online (Sandbox Code Playgroud)
题
从提供的错误来看,我在这里缺少什么?还有什么可以解决这个问题呢?
Jam*_*rey 167
对于未来遇到类似问题的人来说,情况是编译器根本无法找到您正在使用的类型(即使您的Intelisense可以找到它).
这可能在很多方面造成:
#include了定义它的标题.#ifndef BLAH_H)是有缺陷的(由于拼写错误或复制+粘贴错误,你#ifndef BLAH_H不符合你的#define BALH_H要求).new Vector(),并且a new Vector<int>(),如果你已经在其中NamespaceA::NamespaceB,那么它会查看<global scope>::NamespaceB并且不打扰检查NamespaceA),除非你明确地访问它.要显式访问全局命名空间中的某些内容,请在其NamespaceA::NamespaceB前面加上,就好像全局命名空间是没有名称的命名空间(例如<global scope>::NamespaceB或::).
首先,让我们尝试使您的代码更简单:
// 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());
Run Code Online (Sandbox Code Playgroud)
好的,现在我们看到括号明显平衡了.还有什么呢?让我们检查以下代码片段的错误:
int main() {
delete new T();
}
Run Code Online (Sandbox Code Playgroud)
这可能看起来像是奇怪的用法,但是我确实讨厌内存泄漏.但是,输出似乎很有用:
In function 'int main()':
Line 2: error: expected type-specifier before 'T'
Run Code Online (Sandbox Code Playgroud)
啊哈!现在我们只剩下关于括号的错误.我找不到导致这种情况的原因; 但是,我认为您忘记包含定义的文件Rect2f.