Click here to Skip to main content
12,403,983 members (59,706 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
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[^]

#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 9-Nov-12 21:50pm
Sabri M746
Updated 9-Nov-12 21:52pm
v2
Comments
   
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100