I get a compiling error in the main fuction 'cout << a.GetVect() ; '
I am not sure whether the problem is in the overloading function or in class.
Anyone has any idea why I take this error?
you can check the compiled code and errors in http://ideone.com/ODOJik
Thanks in advance..

#include <cstdlib>
#include <iostream>
#include <vector>
#include "OverLoad.h"
#include <cmath>

using namespace std;

class Overload {
public:
    Overload();
    Overload(int);
    Overload operator+(Overload &) ;
    void SetK(int k);
    int GetK() const;
    void SetVect(vector<double> vect);
    vector<double> GetVect() const;


private:
    vector<double> vect ;
    int k ;


};

int main(int argc, char** argv) {

    Overload a,b(10),c(10) ;
    a = b+c ;

    cout << a.GetVect() ;   
    return 0;
}


Overload::Overload() {
    k=10 ;
    for(unsigned int i=0;i<k ; ++i){
        double res = 1/pow(i,2) ;
        vect.push_back(res);
    }
}

Overload::Overload(int k) {

    for(unsigned int i=0;i<k ; ++i){
        double res = 1/pow(i,2) ;
        vect.push_back(res);
    }
}

Overload Overload::operator+(Overload &a){

    Overload x ;
    for(unsigned int i=0 ;i<vect.size();i++) {
        double res = vect[i] + a.vect[i];
         x.vect.push_back(res) ;
    }
    return x ;
}

void Overload::SetK(int k) {
    this->k = k;
}

int Overload::GetK() const {
    return k;
}

void Overload::SetVect(vector<double> vect) {
    this->vect = vect;
}

vector<double> Overload::GetVect() const {
    return vect;
}

Dani AI

Generated

Good catch from — the immediate compile error came from trying to stream a std::vector (there’s no built‑in operator<< for vectors). ’s loop to print elements fixes that for output, but there are a few additional, practical improvements and pitfalls worth addressing so the class behaves correctly and efficiently.

First, operator+ should take a const reference and be const; right now it modifies/uses internals in a way that produces the wrong result because the default-constructed temporary already contains data. Clear or resize the result vector before pushing, or construct the result with the correct size and write into it. Also handle mismatched sizes (use the smaller size, throw, or document the requirement). Example pattern:

Overload Overload::operator+(const Overload& rhs) const {
    Overload res;
    res.vect.clear();
    std::size_t n = std::min(vect.size(), rhs.vect.size());
    res.vect.reserve(n);
    for (std::size_t i = 0; i < n; ++i)
        res.vect.push_back(vect[i] + rhs.vect[i]);
    res.k = static_cast<int>(n);
    return res;
}

Second, provide a streaming operator so callers can write cout << a; instead of manually looping every time. Prefer accessing the vector via a const accessor that returns a const reference to avoid copies:

std::ostream& operator<<(std::ostream& os, const Overload& o) {
    const auto& v = o.GetVect(); // ideally GetVect returns const std::vector<double>&
    os << '[';
    for (std::size_t i = 0; i < v.size(); ++i) {
        if (i) os << ", ";
        os << v[i];
    }
    os << ']';
    return os;
}

Finally, watch the constructors: computing 1/pow(i,2) starting at i == 0 gives a division-by-zero (or infinity) — start at 1 or handle the i==0 case specially. Also prefer std::size_t for indices and return a const reference from GetVect() to avoid unnecessary copies. These tweaks make the class safer, faster, and friendlier to stream output.

Recommended Answers

All 2 Replies

The error you are getting is saying that it can’t find an operator << that will take a vector. The vector class does not have built in stream operators so if you want to pass a vector to << than you need to write your own function to do that.

commented: You are saying I should oveload << operator? +0

I found it It must be

for(unsigned int i=0;i <10;++i)
    cout << a.GetVect()[i] ; 

Thats all :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.