0

我目前正在尝试访问这样定义的向量:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;
template<class T>
class file
{
    public:
        typedef vector<vector<T> > buffer;
};


int main()
{
    file<double> test;
    cout << test.buffer.size() << endl;


    std::vector<pair<string, file<double> > > list_of_files;

    for (const auto& [name, file] : list_of_files)
    {
        cout << file.buffer.size() << endl;
    }

}

我收到的错误消息是buffer我目前正在做的范围界定无效?但为什么它无效?我看不出它应该是什么原因?

我在 for 循环中试图在 的内部和外部向量之间进行迭代buffer,但是由于我无法确定它的范围,所以我无法访问?我如何访问它?

4

1 回答 1

1

错误的原因是代码声明buffervector<vector<T>>. 如果你想buffer成为 的成员file,你可以这样做:

template<class T>
class file
{
public:
    std::vector<std::vector<T>> buffer;
};

更改后,main()应该编译没有错误。

于 2017-11-18T21:59:06.027 回答