2009-10-12 122 views
11

正如标题所述,我不确定为什么我会收到此错误。我已经把一个类似于这个结构的test.cpp放在一起,它工作正常。此外,除矢量问题之外,还有另一个关于'protected'的问题,甚至在代码中也没有。我认为'保护'是一个宏,所以不知道有什么。我是QT新手,所以我很可能“做错了”。这当然是编译器的建议。帮助错误:ISO C++禁止声明'vector'没有类型

In file included from DrvCrystalfontz.cpp:8: 
LCDText.h:28: error: ISO C++ forbids declaration of 'vector' with no type 
LCDText.h:28: error: expected ';' before '<' token 
LCDText.h:30: error: ISO C++ forbids declaration of 'vector' with no type 
LCDText.h:30: error: expected ',' or '...' before '<' token 
LCDText.h:46: error: expected ':' before 'protected' 
LCDText.h: In constructor 'LCDText::LCDText(int, int, int, int, int, int, int, QObject*)': 
LCDText.h:33: error: expected '{' at end of input 
scons: *** [DrvCrystalfontz.o] Error 1 
scons: building terminated because of errors. 

下面的代码。我已经编号了错误中提到的行。

#ifndef __LCD_TEXT__ 
#define __LCD_TEXT__ 

#include <vector> 
#include <QObject> 

#include "LCDBase.h" 
#include "WidgetText.h" 
#include "WidgetBar.h" 
#include "WidgetHistogram.h" 
#include "WidgetIcon.h" 
#include "WidgetBignums.h" 
#include "WidgetGif.h" 

class LCDText: public LCDBase, public virtual QObject { 
    Q_OBJECT 
    protected: 
     char *LayoutFB; 
     char *DisplayFB; 
     int GOTO_COST; 
     int CHARS; 
     int CHAR0; 
     int LROWS; 
     int LCOLS; 
     int DROWS; 
     int DCOLS; 
     vector<vector<char *> > chars; // Line 28 
     void (*TextRealWrite) (const int row, const int col, const char *data, const int len); 
     void (*TextRealDefchar) (const int ascii, const vector<char *> matrix); // Line 30 
    public: 
     LCDText(int rows, int cols, int xres, int yres, int _goto, int chars, 
      int char0, QObject *parent) : LCDBase(xres, yres), QObject(parent); // Line 33 
     ~LCDText(); 
     void TextInit(int rows, int cols); 
     void TextBlit(int row, int col, int height, int width); 
     void TextClear(); 
     void TextClearChars(); 
     void TextGreet(); 
     void TextDraw(WidgetText widget); 
     void TextBarDraw(WidgetBar widget); 
     void TextHistogramDraw(WidgetHistogram widget); 
     void TextIconDraw(WidgetIcon widget); 
     void TextBignumsDraw(WidgetBignums widget); 
     void TextGifDraw(WidgetGif widget); 
    public signals: // Line 46 
     void SpecialCharChanged(int ch); 
    public slots: 
     void TextSpecialCharChanged(int ch); 
}; 

#endif 

回答

31

Vector位于std命名空间中。你必须做下列操作之一:

前面加上命名空间类型:

std::vector<std::vector<char *> > chars; 

告诉您正在使用矢量从std命名空间编译器

using std::vector; 
vector<vector<char *> > chars; 

或者告诉编译器你正在使用标准命名空间,这将带来一切(不推荐,见评论)

using namespace std; 
+20

请为人类的爱,不要在头文件中使用namespace XXX。你会让所有遇到它的程序员都哭泣。 – 2009-10-12 00:57:47

+1

同意,但你可以告诉人们的选项;) – MichaelM 2009-10-12 01:15:06

+1

那么包括这个选项:使用std :: vector;在包含之后,我更喜欢它在.cpp中。保存将std ::绑定到所有位置*和*文件为什么包含头文件(它并不总是像向量一样明显)。 – 2009-10-12 01:55:53

1

在C++标准库中声明的每个符号都是std名称空间的一部分。为了使用这些声明,你必须用全名引用它。即std ::。
正如MichaelM回答你应该使用std :: vector而不是vector。 你可以,但是,使用下面的“使用声明”:

2. using namespace std; // using namespace ...; is mostly discouraged as it causes a mass-import of symbols into the global namespace

在任何情况下,大部分的时间,你应该避免在头文件中使用的声明,因为它污染了全局命名空间为您的标题的每个用户。

祝你好运