Search code examples
c++classtemplatesvectorstructured-bindings

Why can't I access the vector?


I am currently trying to access the vector defined as such:

#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;
    }

}

the error message I am getting is that scoping the buffer like I am currently doing is invalid?, but why is it invalid? I don't see a reason why it should be?

I am in the for loop trying to iterate between the inner and outer vector of the buffer, but since I can't scope it, I am not able to access? How do i access it?


Solution

  • The reason for the error is because the code declares buffer as a new type for vector<vector<T>>. If you want buffer to be a member of file, you can do so like this:

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

    After changing that, main() should compile without errors.