C ++向量:为什么这段代码不起作用? [英] C++ Vectors: Why is this piece of code not working?

查看:50
本文介绍了C ++向量:为什么这段代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vector< vector< vector<int> > > myArray(5, vector< vector<int> >(4));
vector<int> testArray();
myArray[0][0].push_back(testArray);

我不明白.我只是想在其中添加一个新元素.

I don't understand. I'm just trying to append a new element to it.

第二行是错误的,但这仍然无法编译.

Second line was wrong but this still doesn't compile.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector< vector< vector<int> > > myArray(5, vector< vector<int> >(4));
    vector<int> testArray;
    myArray[0][0].push_back(testArray);
    return 0;
}

编译错误:

pnt.cpp:在"int main()"函数中:pnt.cpp:8:错误:没有匹配的函数可以调用"std :: vector> :: push_back(std :: vector>&)"/usr/include/c++/4.4/bits/stl_vector.h:733:注意:候选对象为:void std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp = int,_Alloc = std::allocator]

pnt.cpp: In function ‘int main()’: pnt.cpp:8: error: no matching function for call to ‘std::vector >::push_back(std::vector >&)’ /usr/include/c++/4.4/bits/stl_vector.h:733: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator]

推荐答案

vector<int> testArray();

应该是:

vector<int> testArray;

vector< int>testArray(); 是名为 testArray 的函数的前向声明,该函数返回 vector< int> .

vector<int> testArray(); is a forward declaration of a function called testArray which returns vector<int>.

您的间接访问级别也太多了:

You also have one level of indirection too much:

myArray[0].push_back(testArray);

myArray[0][0] = testArray;

这篇关于C ++向量:为什么这段代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆