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)) );
Tôi đang cố gắng hiểu tại sao những điều trên sẽ không biên dịch. Vì lý do gì, khi tôi cố gắng tạo ra một thể hiện của Rect2f (mà không kế thừa từ lớp Shape
quy định các shared_ptr
mẫu đối số, giống như Circle
), tôi nhận được sau lỗi:Lỗi: dự kiến loại-specifier trước khi 'ClassName'
error: expected type-specifier before 'Rect2f'
error: expected ')' before 'Rect2f'
Tất cả mọi thứ về Circle
shared_ptr
hoàn toàn ổn. Không có vấn đề với nó; nó chỉ là Rect2f
đang gây ra sự cố.
Hơn nữa, các giá trị được truyền vào hàm tạo là hợp lệ.
Vì vậy, vấn đề có thể xảy ra với vấn đề này là gì? Tôi có Rect.h
bao gồm cho việc này. Vì lợi ích ngắn gọn của (và trong trường hợp tôi bị mất một cái gì đó trong tập tin đó mà có thể ảnh hưởng này), tôi sẽ đăng như sau:
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;
};
Nguồn Lỗi Chức năng và tập tin tiêu đề (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();
}
Câu hỏi
Đánh giá từ các lỗi được cung cấp, những gì tôi thiếu ở đây? Ngoài ra, những gì có thể được thực hiện để khắc phục vấn đề này?
Bạn có bao gồm tiêu đề cho Rect2f không? Hay bạn đã làm một tuyên bố về phía trước? –
Có, tôi đã bao gồm tiêu đề cho Rect2f. – zeboidlund