C++ Vector 不是包含所有内容的 std 成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22468713/
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
Vector is not a member of std with everything included
提问by Scholar
I'm getting a very strange error and something I've never experienced before, I'm trying to initialize a vector at declaration like so:
我遇到了一个非常奇怪的错误和我以前从未经历过的错误,我试图在声明时初始化一个向量,如下所示:
vector <int> myVector (5,4,3,4);
It gives me an error saying that it cannot find a call matching that function, however, if I plug in only 2 numbers it doesn't give me an error.
它给我一个错误,说它找不到匹配该函数的调用,但是,如果我只插入 2 个数字,它不会给我一个错误。
Upon further investigation, even this piece of code throws that vector is not a member of std or that myVector is not a type when I try to call
经过进一步调查,当我尝试调用时,即使是这段代码也会抛出该 vector 不是 std 的成员或 myVector 不是一种类型
myVector.push_back(4);
Now here is the code that gives me the vector is not a member of std, not matching function found, of that nature...
现在这是给我的代码,向量不是 std 的成员,没有找到匹配的函数,具有这种性质......
#include <vector>
using std::vector;
const std::vector<int> newvector;
std::newvector.push_back(5);
int main()
{
}
Error given: newvector in namespace std does not name a type
给出错误:命名空间 std 中的 newvector 未命名类型
采纳答案by Cheers and hth. - Alf
Regarding the final code example,
关于最终的代码示例,
you can't have this statement outside a function body:
你不能在函数体之外有这个语句:
std::newvector.push_back(5);
Place it in main
's function body, andremove the std::
prefix.
将其放在main
的函数体中,并删除std::
前缀。
Also note that for this vector modification to be possible, the vector can't be const
.
另请注意,要使此向量修改成为可能,向量不能为const
。
Initialization with round parentheses provides arguments to a constructor for the class. There must be a corresponding constructor. And there is no std::vector
constructor that corresponds to the four arguments in
用圆括号初始化为类的构造函数提供参数。必须有相应的构造函数。并且没有std::vector
对应于中的四个参数的构造函数
vector <int> myVector (5,4,3,4);
Instead do
而是做
vector <int> myVector = {5,4,3,4};
If your compiler supports this C++11 syntax, that is.
如果您的编译器支持这种 C++11 语法,那就是。
If not, then the C++03 way would be
如果没有,那么 C++03 的方式将是
static int const data[] = {5,4,3,4};
static int const n_items = sizeof(data)/sizeof(data[0]);
vector <int> myVector( data, data + n_items );
回答by taocp
std::newvector.push_back(5);
since newvector
is a variable, it is not in namespace std
.
因为newvector
是一个变量,所以它不在 namespace 中std
。
For this case:
对于这种情况:
vector <int> myVector (5,4,3,4);
If you only use two values (5,4)
in this case, it will create a vector with size 5 and each of the elements initialized to 4 (one form of vector construction). If you need to initialize a vector with a list of values, you may try uniform initialization since C++11 as follows:
如果(5,4)
在这种情况下只使用两个值,它将创建一个大小为 5 的向量,并且每个元素都初始化为 4(向量构造的一种形式)。如果您需要使用值列表初始化向量,您可以尝试从 C++11 开始的统一初始化,如下所示:
std::vector <int> myVector{5,4,3,4};
Some example of uniform initialization
can be found here: uniform initialization and other C++11 features
uniform initialization
可以在此处找到一些示例:uniform Initialization and other C++11 features
回答by 4pie0
There are two issues with this code.
这段代码有两个问题。
using std::vector;
brings avector
from namespacestd
but you have to use this as follows:std::vector<int> newvector; newvector.push_back(5);
You can notice that const dissapeared. This is because you want to change vector at last, so it cannot be
const
, it would be compile error to call apush_back
on it otherwise.
using std::vector;
带来一个vector
from 命名空间,std
但您必须按如下方式使用它:std::vector<int> newvector; newvector.push_back(5);
你可以注意到 const 消失了。这是因为你最后想改变vector,所以它不能
const
,push_back
否则调用a会编译错误。
Finally, this should be:
最后,这应该是:
#include <vector>
using std::vector;
int main() {
std::vector<int> newvector;
newvector.push_back(5);
std::vector<int> newvector;
return 0;
}
回答by yizzlez
The second error, you cannot initialize a vector
with its elements in the constructor, you must use its initializer list (C++11 only)
第二个错误,您无法vector
在构造函数中使用其元素初始化 a ,您必须使用其初始化列表(仅限 C++11)
vector <int> myVector = {5,4,3,4};