xBurnsed
xBurnsed

Reputation: 452

C++ vector addition comp error:

So I've this code:

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

vector <int> suma(vector <int> t, vector <int> m){
    int i;
    vector <int>  sumat;
    for(i=0; i<t.size();i++){
        sumat[i]=t[i]+m[i];
    }
    return sumat;
    }       
int main(){
   vector <int> a(4,0);
   a[0]=5;
   a[1]=5;
   a[2]=3;
   a[3]=0;
   vector <int> b(4,0);
   b[0]=5;
   b[1]=5;
   b[2]=3;
   b[3]=0;
   cout<< suma(a,b) <<endl;
} 

And this is the comp error I'm getting in the last cout line:

[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'std::vector')

Why is that?

Upvotes: 0

Views: 194

Answers (4)

Amadeus Sanchez
Amadeus Sanchez

Reputation: 2565

Why don't you print the contents of sumat within the suma function like this

    void suma(vector <int>& t, vector <int>& m, vector<int>& output){
        // Assuming that both vectors t and m are of the same size.
        for(size_t i=0;i<t.size();i++){
            output.push_back([i]+m[i]);
        }
    }       

Please note that I changed the return value of the function from vector to void. Then in your main function you could call the function like this.

vector<int> sumat;
suma(a, b, sumat);

Then you can go ahead and print the output of your sumat vector like this:

for(size_t i = 0; i < sumat.size(); ++i)
{
    cout << sumat[i] << endl;
}

Upvotes: 0

BlackMamba
BlackMamba

Reputation: 10254

you should change your code to this:

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

 vector<int> suma(vector<int> t, vector<int> m){
     int i;
     vector<int>  sumat(4);
     for(i=0; i<t.size();i++){
         sumat[i]=t[i]+m[i];
     }
     return sumat;
     }
 int main(){
    vector<int> a(4,0);
    a[0]=5;
    a[1]=5;
    a[2]=3;
    a[3]=0;
    vector<int> b(4,0);
    b[0]=5;
    b[1]=5;
    b[2]=3;
    b[3]=0;
    vector<int> res = suma(a,b);
    for(size_t i = 0; i<res.size(); i++)
        cout<<res[i]<<endl;
 }

There are some errors in your code: (1) you should reserve capility for vector sumat when you use sumat[i]. vector<int> sumat just decare a vector, if you want insert value to sumat, you can use push_back.

(2) you cann't cout an vector object directly, you can write an override operator for vector object or output the element for vector.

Upvotes: 0

The Dark
The Dark

Reputation: 8514

C++ doesn't define an ostream operator << for vectors, most likely because there are a lot of different ways you could output them. One entry per line, like below, or comma separated, or contained within brackets.

You can define your own << operator for vectors using "operator overloading". This allows the same operator to call different code depending on the calling parameters.

Global operator overload methods have a specific calling convention, the first parameter is a reference to the object on the left of the operator (e.g. the cout in cout << v, the second parameter is the value on the right of the operator (the v). For the << operator, you normally define the parameter as a const reference, as you don't normally change the value when it is output.

The return value for a << operator is normally the first parameter. This allows you to chain calls to << like cout << vec << "Done" << std::endl.

e.g.:

ostream &operator << (ostream &out, const vector<int> &vec)
{
    for (auto &&val : vec)
        out << val << std::endl;
    return out;
}

If you can't use c++11, you can use the older style loop like this:

ostream &operator << (ostream &out, const vector<int> &vec)
{
    for (vector<int>::size_type i = 0; i < vec.size(); i++)
        out << vec[i] << std::endl;
    return out;
}

or other similar loops using iterators.

PS. Unrelated note - you need to initialise the size of sumat in your code, or use push_back. At the moment it will crash.

Upvotes: 2

patros
patros

Reputation: 7819

There's no definition of how to emit a vector to an output stream.

You should probably define a function to turn a vector of ints into a string, and then call

cout<< YourPrintFunction(suma(a,b)) <<endl;

Upvotes: -1

Related Questions