[英]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)) );
我試圖理解為什么上面不能編譯。 無論出於何種原因,當我嘗試創建 Rect2f 實例(它確實從Shape
類繼承指定了shared_ptr
模板參數,就像Circle
一樣)時,我收到以下錯誤:
error: expected type-specifier before 'Rect2f'
error: expected ')' before 'Rect2f'
關於Circle
shared_ptr
一切都非常好。 它沒有問題; 只有Rect2f
導致了問題。
此外,傳遞給構造函數的值是有效的。
那么,這可能是什么問題呢? 我Rect.h
包含了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();
}
問題
從提供的錯誤來看,我在這里遺漏了什么? 另外,可以做些什么來糾正這個問題?
對於未來遇到類似問題的人來說,情況是編譯器根本找不到您正在使用的類型(即使您的 Intelisense 可以找到它)。
這可以通過多種方式引起:
#include
定義它的標頭。#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
查找NamespaceA::NamespaceB
並且不費心檢查<global scope>::NamespaceB
) 除非您顯式訪問它。 要顯式訪問全局命名空間中的某些內容,請為其添加::
前綴,就好像全局命名空間是一個沒有名稱的命名空間(例如::MyType
或::MyNamespace::MyType
)。
首先,讓我們試着讓你的代碼更簡單一點:
// 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
的文件。
對於將來的讀者來說,最重要的答案仍然不能解決問題,當您在同一個命名空間中有一個枚舉和一個類並且您的一個枚舉條目偶然與該類具有相同的名稱時,也會發生此問題。
如果源文件同時包含類標頭和具有枚舉的標頭(可以是定義兩者的單個標頭),則將發生這種情況。
來自第一個答案的討論: http : //chat.stackoverflow.com/rooms/6681/discussion-between-holland-and-anton-golov
Anton:它給我一個錯誤“無法分配抽象類型為'Rect2f'的對象,因為>下列虛擬函數在'Rect2f'中是純凈的:virtual bool Shape :: Intersects(const> Circle&)const”。
看來您的Rect2f
類缺少此功能:
virtual bool Intersects( const Circle& c ) const;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.