Click here to Skip to main content
11,706,007 members (52,813 online)
Rate this: bad
good
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
Edited 9-Nov-12 21:52pm
v2
Comments
Sergey Alexandrovich Kryukov at 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

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
0 OriginalGriff 142
1 Sergey Alexandrovich Kryukov 137
2 ppolymorphe 71
3 Mika Wendelius 70
4 chainerlt 70
0 OriginalGriff 9,050
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,636


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2015
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