Help me debug error, please!

This is a discussion on Help me debug error, please! within the C++ Programming forums, part of the General Programming Boards category; I have wirten a program to build a class vector execute +.-.*,<<.>>.== With >>,<<, ==,!== operator which very good. But ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Help me debug error, please!

    I have wirten a program to build a class vector execute +.-.*,<<.>>.==
    With >>,<<, ==,!== operator which very good. But +,- operator be error!
    Help me find error, please. Thanhks!



    Code:
    
    #include<iostream>
    #include<conio.h>
    
    using namespace std;
    
    class Vector{
          double *v;
          int size;
          public:
                 Vector(int x=2);
                 Vector(Vector &ob);
                 ~Vector();
                 int getSize();
                 Vector& operator=(Vector &ob);
                 double& operator[](int i);
                 int  operator == (Vector &ob);
                 int  operator != (Vector &ob);
                 friend ostream& operator<<(ostream &os, Vector &ob);
                 friend istream& operator>>(istream &is, Vector &ob);
                 Vector operator + (Vector &) const ;
                 Vector operator - (Vector &) const;
          };
    Vector::Vector(int x){
                       size = x;
                       if(size >=0){
                               v= new double[size];
                               for(int i; i<size;i++)
                                       v[i]=0;
                       }
                       else v=0;
    }
    Vector::~Vector(){
                      if(size>0) delete []v;
    }
    
    Vector::Vector(Vector &ob){
                          if(ob.size > 0){
                                     size = ob.size;
                                     v= new double [size];
                                     for(int i=0; i<size; i++) v[i] = ob.v[i];
                          }
                          else{
                               size = 0;
                               v=0;
                          }
    }
    double &Vector::operator [](int i){
            return v[i];
    }
    Vector& Vector::operator = (Vector &ob){
            if(size>0) delete []v;
            if(ob.size > 0) {
                       size = ob.size;
                       v = new double[size];
                       for(int i=0;i<size;i++) v[i] = ob.v[i];
                       }
            else{
                 size = 0;
                 v = 0;
                 }
    return *this;
    }
    int Vector::operator == (Vector &ob){
         if(size!=ob.size) 
         return 0;
         else {
              for(int i=0;i<size;i++)
                      if(v[i]!=ob.v[i])
                            return 0;
          
    return 1;
              }
    }
    int Vector::operator != (Vector &ob){
        return (*this == ob);
    }
    
    int Vector::getSize(){
        return size;
    }
    ostream& operator << (ostream& os, Vector &ob){
             if(ob.size>0){
                           for(int i=0;i<ob.size;i++) 
                                   os<<ob.v[i]<<" ";
                           }
             else {
                  os<<"Vector is empty!"<<endl;
             }
    return os;
    }
    
    istream& operator >> (istream& is,Vector &ob){
             if(ob.size>0){
                           for(int i=0;i<ob.size;i++){
                                                  cout<<"v["<<i<<"] :";
                                                  is>>ob.v[i];
                                                  }
                           }
    return is;
    }
      
      
    Vector Vector::operator + (const Vector &ob) const{
          Vector x;
          v= new double[size];
          for(int i=0;i<size;i++)
          x.v[i] = v[i] + ob.v[i];
    return x;
     }         
    Vector Vector::operator - (const Vector &ob) const{
           Vector x;
           v= new double[size];
           for(int i=0;i<size;i++)
           x.v[i] = v[i]- ob.v[i];
    return x;
    }
    int main(){
       
         Vector v1(3),v2(3),v3;
         cout<<"\nEnter v1:";
         cin>>v1;
         cout<<"\nDisplay :";
         cout<<v1<<endl;
          cout<<"\nTo query the first element of v1:";
         cout<<v1[0]<<endl;
        cout<<"\n---- Define for v2 ----\n";
         cout<<"\nEnter :";
         cin>>v2;
         cout<<v2<<endl;
         if(v1==v2) cout<<"v1 == v2";
         else cout<<"\nv1 != v2";  
         v3 = v1 + v2;
         cout<<v3;
         v3 = v1 - v2;
         cout<<"\n";
         cout<<v3;
         getch();
        return 0;
         
       }
    Last edited by phuong_bk; 02-26-2009 at 06:49 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
          v= new double[size];
          for(int i=0;i<size;i++)
          x.v[i] = v[i] + ob.v[i];
    Can you explain what this line is doing?

    Also:
    Code:
    int Vector::operator == (Vector &ob){
         if(size!=ob.size) 
         return 0;
         else {
              for(int i=0;i<size;i++)
                      if(v[i]!=ob.v[i])
                            return 0;
          
    return 1;
              }
    }
    int Vector::operator != (Vector &ob){
        return (*this == ob);
    }
    Operator == normally returns bool, not int.
    And your != is wrong returning the opposite.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    oh no,
    v= new double[size];
    for(int i=0;i<size;i++)
    x.v[i] = v[i] + ob.v[i];
    it' mean is add together vector x and ob

    In addition, my opinion Operator == narmally reuturns bool (true, false), thas is true == 1, false == 0

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by phuong_bk View Post
    oh no,
    it' mean is add together vector x and ob

    In addition, my opinion Operator == narmally reuturns bool (true, false), thas is true == 1, false == 0
    But the TYPE returned should be bool, not int, and the values should be true and false, not 0 and 1. Yes, they are equivalent, and bool can be converted to&from int. But it's CORRECT to use bool, incorrect to use int.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 12:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21