2010-05-05 80 views
17

我正在使用Cygwin和GCC,並且最終我想在字符的文件讀入的字符的向量,和使用此代碼C++ vector.push_back錯誤:請求成員'push_back'...,它是非類類型'vector(char,allocator(char))()()'

#include <fstream> 
#include <vector> 
#include <stdlib.h> 

using namespace std; 

int main (int argc, char *argv[]) 
{ 
    vector<char> string1(); 
    string1.push_back('a'); 

    return 0; 
} 

生成此編譯時間錯誤:

main.cpp: In function int main(int, char**)': main.cpp:46: error: request for member push_back' in string1', which is of non -class type std::vector >()()'

我試圖與整數和字符串的向量以及他們也有同樣的問題。

回答

42

不要使用括號來調用默認的構造函數:

vector<char> string1; 

否則,該聲明的函數string1,它沒有argumentes並返回一個vector<char>

6

刪除聲明vector聲明中的零件 - 它們使它成爲函數聲明而不是向量聲明。

相關問題