c++ vector.push_back 错误:请求成员'push_back'...,这是非类类型'vector(char, allocator(char)) ()()'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2770464/
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
c++ vector.push_back error: request for member 'push_back'..., which is of non-class type 'vector(char, allocator(char)) ()()'
提问by Dlongnecker
I'm using Cygwin with GCC, and ultimately I want to read in a file of characters into a vector of characters, and using this code
我在 GCC 中使用 Cygwin,最终我想将一个字符文件读入一个字符向量,并使用此代码
#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;
}
generates this compile time error:
生成此编译时错误:
main.cpp: In function
int main(int, char**)': main.cpp:46: error: request for member
push_back' instring1', which is of non -class type
std::vector > ()()'
main.cpp:在std::vector > ()()' 中的函数
int main(int, char**)': main.cpp:46: error: request for member
push_back' 中string1', which is of non -class type
I tried this with a vector of ints and strings as well and they had the same problem.
我也用整数和字符串的向量尝试过这个,他们有同样的问题。
回答by Georg Fritzsche
Don't use parentheses to invoke the default constructor:
不要使用括号来调用默认构造函数:
vector<char> string1;
Otherwise this declares a function string1
that takes no argumentes and returns a vector<char>
.
否则,这将声明一个string1
不带参数的函数并返回一个vector<char>
.
回答by Eli Bendersky
Remove the parens in the declaration of the vector
- they cause it to be a function declaration and not a vector declaration.
删除声明中的括号vector
- 它们使其成为函数声明而不是向量声明。