Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanna overload '+' operator and add the elements of two series by using vector. But anytime I run the code , the first elements of my vectors get 'inf' . I can't understand why I get this error. I checked my code several time whether I had done mistake while filling vector but I coulnt find anything to correct. Anyone has any idea about this mistake? here is the compiled code and results -> http://ideone.com/CA5Q6n[^]

C++
#include <cstdlib>
#include <iostream>
#include <vector>
#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 <<"\nAdded :\n" ;
    for(unsigned int i=0;i<a.GetVect().size(); ++i)
    cout <<"Res : " << a.GetVect()[i] << endl ;   
    cout << "first element : "<< a.GetVect()[0] << endl ; 
    return 0;
}


Overload::Overload() {
    k=10 ;
    cout << "\nFrist Started\n" ;
    for(unsigned int i=0;i<k ; ++i){
        double res = 1/pow(i,2) ;
        vect.push_back(res);
        cout << vect[i] << " " ;
    }
    cout << "VEctor size : " << vect.size() << endl ;
    cout <<"\nfirst ended\n"<< endl ;
}

Overload::Overload(int k) {
    double res=0 ;
    cout << "\nsecond started : \n" ;
    for(unsigned int i=0;i<k ; ++i){
        res = 1/pow(i,2) ;
        vect.push_back(res);
        cout << vect[i] << " " ;
    }
    cout << "\nsecond ended : \n" ;
}

Overload Overload::operator+(Overload &a){
    double res=0 ;
    Overload x ;
    for(unsigned int i=0 ;i<vect.size();i++) {
        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;
}
Posted
Updated 9-Nov-12 21:52pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Apr-13 16:09pm    
Please don't post non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership. And the fact you even self-accepted on formally is just outrageous, a sure way for a ban. I hope you won't do it after this warning.

Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

1 solution

Both in ::Overload() and ::Overload(int k) the first iteration of the for loop calculates 1/pow(i,2) for i=0. Which means you end up dividing by zero which is represented by the Inf value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900