错误帮助:ISO C++ 禁止声明没有类型的“向量”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1552170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:24:40  来源:igfitidea点击:

Help with error: ISO C++ forbids declaration of 'vector' with no type

c++vector

提问by Scott

As the title states, I'm not sure why I'm getting this error. I've put together a test.cpp that's similar to this structure, and it works fine. Also, other than the vector problem, there's the other problem about 'protected', which isn't even in the code. I think 'protected' is a macro, so no telling what's there. I'm new to QT, so I'm likely "doing it wrong." That's certainly what the compiler's suggesting.

正如标题所述,我不确定为什么会收到此错误。我已经把一个类似于这个结构的 test.cpp 放在一起,它工作正常。此外,除了向量问题之外,还有另一个关于“受保护”的问题,它甚至不在代码中。我认为“受保护”是一个宏,所以不知道那里有什么。我是 QT 的新手,所以我很可能“做错了”。这当然是编译器的建议。

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.

Here's the code. I've numbered the lines noted in the error.

这是代码。我已经对错误中指出的行进行了编号。

#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

回答by MichaelM

Vector resides in the std namespace. You have to do one of the following:

Vector 驻留在 std 命名空间中。您必须执行以下操作之一:

Prepend the type with the namespace:

在类型前面加上命名空间:

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

Tell the compiler you are using vector from the std namespace

告诉编译器您正在使用 std 命名空间中的向量

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

Or, tell the compiler you are using the std namespace, which will bring in everything (not recommended, see comments)

或者,告诉编译器您正在使用 std 命名空间,它将引入所有内容(不推荐,请参阅注释)

using namespace std;

回答by Oren S

every symbol declared in C++ standard library is part of the std namespace. In order to use these declarations you have to refer it by its full name. namely std::.
As MichaelM answered you should use std::vector instead of vector. You can, however, use the following "using declarations":
1. using std::vector;
2. using namespace std; // using namespace ...; is mostly discouraged as it causes a mass-import of symbols into the global namespace

C++ 标准库中声明的每个符号都是 std 命名空间的一部分。为了使用这些声明,您必须以其全名来引用它。即 std::.
正如 MichaelM 回答的那样,您应该使用 std::vector 而不是 vector。但是,您可以使用以下“使用声明”:
1. using std::vector;
2.using namespace std; // using namespace ...; is mostly discouraged as it causes a mass-import of symbols into the global namespace

On any case, most of the time you should avoid using declaration in header files as it pollutes the global namespace for every user of your header.

在任何情况下,大多数情况下你应该避免在头文件中使用声明,因为它会污染你头文件的每个用户的全局命名空间。

good luck

祝你好运