Home > OS >  How do I structure my templates, if I am passing a container which contains a container
How do I structure my templates, if I am passing a container which contains a container

Time:10-20

Here is my code:

//How do you find the length between each sort of subsequence too?
/*
Up,Down is Map<int,int>
UpV,DownV,UpDown are vector<int>
UpMat, DownMat are Map<int,vector<int>>
*/
template <typename T, typename U, typename V>
auto SubSequenceFrequency(U &&Up, U &&Down, T &&UpV, T &&DownV, T &&UpDown, V &&UpMat, V &&DownMat) -> void {
    int Lptr = 0;
    int Rptr = 0; 
    
    while (Rptr < UpDown.size()) {
        if (UpDown[Rptr] != UpDown[Lptr]) {
            if (UpDown[Lptr] == 0) {
                DownV.push_back(Rptr-Lptr);
                if (Down.find(Rptr-Lptr) == Down.end()) {
                    DownMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
                    Down[Rptr-Lptr] = 1;
                } else {
                    Down[Rptr-Lptr]  = 1;
                    UpMat[Rptr-Lptr].push_back(Lptr);
                }
            } else {
                UpV.push_back(Rptr-Lptr);
                if (Up.find(Rptr-Lptr) == Up.end()) {
                    UpMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
                    Up[Rptr-Lptr] = 1;
                } else {
                    UpMat[Rptr-Lptr].push_back(Lptr);
                    Up[Rptr-Lptr]  = 1;
                }
            }
            Lptr = Rptr;
        }
          Rptr;
    }

    if (UpDown[Lptr] == 0) {
        DownV.push_back(Rptr-Lptr);
        if (Down.find(Rptr-Lptr) == Down.end()) {
            DownMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
            Down[Rptr-Lptr] = 1;
        } else {
            DownMat[Rptr-Lptr].push_back(Lptr);
            Down[Rptr-Lptr]  = 1;
        }
    } else {
        UpV.push_back(Rptr-Lptr);
        if (Up.find(Rptr-Lptr) == Up.end()) {
            UpMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
            Up[Rptr-Lptr] = 1;
        } else {
            UpMat[Rptr-Lptr].push_back(Lptr);
            Up[Rptr-Lptr]  = 1;
        }
    }
}

My Exact Error is:

BinanceData.cpp: In instantiation of ‘void SubSequenceFrequency(U&&, U&&, T&&, T&&, T&&, V&&, V&&) [with T = std::vector<int>&; U = std::map<int, int>&; V = std::map<int, std::vector<int> > (&)()]’:
BinanceData.cpp:168:25:   required from ‘auto SubSequenceDistributionGenerator(T&&, T&&, U&&) [with T = std::__cxx11::basic_string<char>&; U = std::vector<int>&]’
BinanceData.cpp:249:37:   required from ‘auto PriceChangeDataToCSV(T&&, T&&, U&&, const int&) [with T = std::__cxx11::basic_string<char>&; U = std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >&]’
BinanceData.cpp:315:54:   required from here
BinanceData.cpp:60:29: error: request for member ‘insert’ in ‘DownMat’, which is of non-class type ‘std::map<int, std::vector<int> >()’
   60 |                     DownMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
      |                     ~~~~~~~~^~~~~~
BinanceData.cpp:64:26: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   64 |                     UpMat[Rptr-Lptr].push_back(Lptr);
      |                     ~~~~~^
BinanceData.cpp:64:38: error: request for member ‘push_back’ in ‘*((& UpMat)   ((sizetype)(Rptr - Lptr)))’, which is of non-class type ‘std::map<int, std::vector<int> >(
’
   64 |                     UpMat[Rptr-Lptr].push_back(Lptr);
      |                     ~~~~~~~~~~~~~~~~~^~~~~~~~~
BinanceData.cpp:69:27: error: request for member ‘insert’ in ‘UpMat’, which is of non-class type ‘std::map<int, std::vector<int> >()’
   69 |                     UpMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
      |                     ~~~~~~^~~~~~
BinanceData.cpp:72:26: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   72 |                     UpMat[Rptr-Lptr].push_back(Lptr);
      |                     ~~~~~^
BinanceData.cpp:72:38: error: request for member ‘push_back’ in ‘*((& UpMat)   ((sizetype)(Rptr - Lptr)))’, which is of non-class type ‘std::map<int, std::vector<int> >(
’
   72 |                     UpMat[Rptr-Lptr].push_back(Lptr);
      |                     ~~~~~~~~~~~~~~~~~^~~~~~~~~
BinanceData.cpp:84:21: error: request for member ‘insert’ in ‘DownMat’, which is of non-class type ‘std::map<int, std::vector<int> >()’
   84 |             DownMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
      |             ~~~~~~~~^~~~~~
BinanceData.cpp:87:20: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   87 |             DownMat[Rptr-Lptr].push_back(Lptr);
      |             ~~~~~~~^
BinanceData.cpp:87:32: error: request for member ‘push_back’ in ‘*((& DownMat)   ((sizetype)(Rptr - Lptr)))’, which is of non-class type ‘std::map<int, std::vector<int> >()’
   87 |             DownMat[Rptr-Lptr].push_back(Lptr);
      |             ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
BinanceData.cpp:93:19: error: request for member ‘insert’ in ‘UpMat’, which is of non-class type ‘std::map<int, std::vector<int> >()’
   93 |             UpMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
      |             ~~~~~~^~~~~~
BinanceData.cpp:96:18: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   96 |             UpMat[Rptr-Lptr].push_back(Lptr);
      |             ~~~~~^
BinanceData.cpp:96:30: error: request for member ‘push_back’ in ‘*((& UpMat)   ((sizetype)(Rptr - Lptr)))’, which is of non-class type ‘std::map<int, std::vector<int> >(
’
   96 |             UpMat[Rptr-Lptr].push_back(Lptr);
      |             ~~~~~~~~~~~~~~~~~^~~~~~~~~

I have fixed up some stupid mistakes. This occurs during compile time, and it is just the function itself. If anyone has suggestions in what I can do in regards to what seems like a nested template problem that would be great.

Despite changing the code, in regards to the mistake as well, I still get the same error as the previous error too, so it must be an issue with in regards to that as well.

Thank you

CodePudding user response:

Full code here: https://godbolt.org/z/KdKqTo469

The problems are in these two lines in my case:

UpMat[Rptr-Lptr].insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));
UpMat[Rptr-Lptr].insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));

Access the value of UpMat:

UpMat[Rptr-Lptr] => access to the value of UpMat and the value is of type std::vector<int>

Insert in the std::vector<int> with:

std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr})

doesn't make sense.

CodePudding user response:

the problem is with the statement UpMat[Rptr-Lptr].insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));

UpMat is a std::map<int,std::vector<int>> so UpMat[Rptr-Lptr] is std::vector and you tried to insert a std::pair<int,std::vector<int>> to it.

I think you meant to insert the pair to the map (eg.: UpMat.insert(std::pair<int, std::vector<int>>(Rptr-Lptr, {Lptr}));)

you can't insert a pair<int,vector<int>> to a vector<int> but you can insert it to a std::map<int,std::vector<int>>

  • Related