2014-10-19 90 views
0

這裏是我的一段代碼變量不是在這個範圍內聲明

#include <stdio.h> 
#include "W.h" 
#include <QtGui/QApplication> 
#include <QtGui/QPushButton> 
#include <QtGui/QFont> 
#include <QtGui/QWidget> 
#include <QtGui/QLabel> 
#include <QtGui/QImage> 
#include <QtGui/QPainter> 
#include <opencv/highgui.h> 
#include <opencv/cv.h> 
#include <opencv/cxcore.h> 
#include <QTimer> 
#include <QImage> 
#include <string> 
#include <vector> 


void PainterWidget::paintEvent(QPaintEvent* event) { 
     vector <const char*> p; 
     p.push_back("01.jpg"); 
     p.push_back("02.jpg"); 
     p.push_back("03.jpg"); 
     p.push_back("04.jpg"); 
     p.push_back("05.jpg"); 
     counter=counter%5; 
     QImage img= QImage(p[counter]); 
     QPainter painter(this); 
     painter.drawImage(0,0,img); 
     counter++ 
     p.clear(); 

} 

我得到錯誤「錯誤:‘向量’在這個範圍內沒有宣佈」 我不明白,我已經包含矢量庫,爲什麼錯誤仍然出現?

謝謝大家的幫助!

+4

改爲使用'std :: vector'。 – Gluttton 2014-10-19 18:42:03

回答

3

地址:

using namespace std; 

或前綴vectorstd::

通過方式,你可以通過創建和優化你的代碼只填充一次矢量。對於每個繪畫事件都沒有意義。另外,完成後不需要明確地清除你的向量,它的析構函數(在你的情況下自動調用)就可以完成。

static std::vector<std::string>& images() 
{ 
    static std::vector<std::string> imagesVect; 
    if (imagesVect.empty()) 
    { 
     imagesVect.push_back("01.jpg"); 
     imagesVect.push_back("02.jpg"); 
     imagesVect.push_back("03.jpg"); 
     imagesVect.push_back("04.jpg"); 
     imagesVect.push_back("05.jpg"); 
    } 
    return imagesVect; 
} 

void PainterWidget::paintEvent(QPaintEvent* event) { 
     counter=counter%5; 
     QImage img= QImage(images().[counter].c_str()); 
     QPainter painter(this); 
     painter.drawImage(0,0,img); 
     counter++;  
} 
+0

這不是清除矢量的標準庫。它是它自己的析構函數,釋放它分配的內存。 – 2014-10-19 20:09:41

+0

你說得對,我的意思是STL代碼.... – jpo38 2014-10-19 20:10:33

1

使用

std::vector<const char *> 

是使用空間std聲明的類矢量的限定名..