0

As you can see in the following code, i want to allocate the size and elements of the vectors declared in main through Allocate function.

Error Msg

.\MV_Produkt.cpp: In instantiation of 'void Allocation(std::vector<std::vector<T> >&, std::vector<T>&) [with T = double]':

.\MV_Produkt.cpp:44:20:   required from here

.\MV_Produkt.cpp:11:6: error: no match for call to '(std::vector<std::vector<double> >) (int&, std::vector<double>)' -----A(r, std::vector<T>(c));

.\MV_Produkt.cpp:19:6: error: no match for call to '(std::vector<double>) (int&)'-----x(c);
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>

template<typename T>
void Allocation(std::vector<std::vector<T>>& A,std::vector<T>& x){
    std::ifstream inA("A.txt");
    int r, c;
    inA >> r >> c;
    A(r, std::vector<T>(c));
    for(size_t i=0;i<A.size();i++)
        for(size_t j=0;j<A[i].size();j++)
            inA >> A.at(i).at(j);
    inA.close();

    std::ifstream inx("x.txt");
    inx >> c;
    x(c);
    typename std::vector<T>::iterator xi;
    for(xi=x.begin();xi!=x.end();xi++)
        inx >> *xi;
    inx.close();
}

template<typename T>
void MV_Product(const std::vector<std::vector<T>> A,const std::vector<T> x, std::vector<T>& b){
    typename std::vector<std::vector<T>>::const_iterator row;
    typename std::vector<T>::const_iterator colA;
    typename std::vector<T>::const_iterator colx;
    typename std::vector<T>::iterator colb;

    for(row=A.cbegin();row<A.cend();row++)
        for(colA=row->cbegin(),colx=x.cbegin(),colb=b.begin();colA<row->cend(),colx<x.cend(),colb<b.end();colA++,colx++,colb++)
            *colb = *colA * *colx;
}

int main(){
    std::vector<std::vector<double>> A;
    std::vector<double> x;
    std::vector<double> b;
    std::vector<double>::iterator bi;

    Allocation(A, x);
    MV_Product(A,x,b);

    std::ofstream outb("b.txt");
    outb << b.size() << " " << *min(b.begin(),b.end()) << " " << *max(b.begin(),b.end()) << std::endl;
    for(bi=b.begin();bi<b.end();bi++)
        outb << *bi << std::endl;
    outb.close();
}

Thanks for any Help in advance!

P.S. If anyone could also tell me how to better pass vectors to functions and how to use template "typename " for main would be much appreciated.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402

2 Answers2

0

"how to better pass vectors to functions?"

  • it's usually pass the vector by reference, so the function can reflects changes on the vector. ex: void foo(vector<int> &bar);

  • you can also pass a copy of the vector, maybe you want to use/manipulate the contents of this vector, but any change will not be reflected on the passed vector, you just use a "copy". ex: void foo(vector<int> bar);

  • pass a const reference and this is efficient as well as reliable when you don't want function to change the contents of the vector. ex: void foo(vector<int> const &bar);
  • Sure you can pass a pointer to a vector, but unless you know what you're doing and you feel that this is really is the way to go, don't do this. ex: void foo(vector<int> *bar);

  • please check this question and "passing a vector to a function" on Geeks4Geeks

walid barakat
  • 455
  • 1
  • 6
  • 17
0

in your code, you can see in the error messages here

\MV_Produkt.cpp:11:6: error: no match for call to '(std::vector<std::vector<double> >) (int&, std::vector<double>)' -----A(r, std::vector<T>(c));

\MV_Produkt.cpp:19:6: error: no match for call to '(std::vector<double>) (int&)'-----x(c);

that you use for T double in the main function, while you using it as int inside Allocation function, so it's a mismatch between what you defined and what you passed.

walid barakat
  • 455
  • 1
  • 6
  • 17