Problem using overloaded << operator

This is a discussion on Problem using overloaded << operator within the C++ Programming forums, part of the General Programming Boards category; Hello, I am currently creating a class to represent a PolyLine. I have all my methods working correctly, but I ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Question Problem using overloaded << operator

    Hello,

    I am currently creating a class to represent a PolyLine. I have all my methods working correctly, but I need to make an overloaded << operator. Here is the method I am using for testing purposes:

    Code:
    void output() {
            int finalSpot = length - 1;
            for (int i = 0; i < finalSpot; i++)
                    cout << "(" << x[i] << "," << y[i] << "):";
                    
            cout << "(" << x[finalSpot] << "," << y[finalSpot] << ")" << endl;
        }
    The x and y arrays are variables of the PolyLine. They are declared as follows in my class:

    Code:
       double *x;
        double *y;
    What would the ostream method equivalent of my output method be? I've tried a number of things and none of them worked. Can someone please help me? Thanks.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Change output() such that
    Code:
    std::ostream & output ( std::ostream &out ) {
            int finalSpot = length - 1;
            for (int i = 0; i < finalSpot; i++)
                    out << "(" << x[i] << "," << y[i] << "):";
                    
            out << "(" << x[finalSpot] << "," << y[finalSpot] << ")" << std::endl;
            return out;
        }
    Then you can specify an operator:
    Code:
    std::ostream & operator <<(std::ostream &out, const CLASSNAME ob)
    {
      return ob.output(out);
    }
    Last edited by Thantos; 09-19-2004 at 02:08 PM.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    I'm still getting errors when I try to compile. Here is my entire code for the class as it stands right now:

    Code:
    #include <iostream>
    using namespace std;
    
    class PolyLine {
        double *x;
        double *y;
        int length;
        
      public:
        PolyLine (double xVals[], double yVals[], int num) {
            length = num;
            x = new double[length];
            y = new double[length];
            
            for (int i = 0; i < length; i++) {
                    x[i] = xVals[i];
                    y[i] = yVals[i];
            }
        }
        
        int points() {
            return length;
        }
        
        double value(double xNum) {
            double val = 0.0;
            double finalSpot = x[length - 1];
    		
            if (xNum < x[0] || xNum > finalSpot) return val;
    		
            for (int i = 0; i < length; i++) {
                if (x[i] == xNum) val = y[i];
                else if (x[i] != xNum && x[i] > xNum) val = xNum;
            }
    		
            return val;
        }
        
        double max() {
            double max = 0.0;
    		
            for (int i = 0; i < length; i++)
                if (y[i] > max) max = y[i];
    		
            return max;
        }
        
        double max(double a, double b) {
            double max = 0.0;
            double finalSpot = x[length - 1];
            int i = 0;
    		
            while (x[i] != a) {
                if (i == length - 1) return max;
                else if (x[i] > a) {
                   if (y[i] < 0) break;
                   else {
                     max = y[i];
                     break;
                   }
                }
                else i++;
            }
    		
            while (x[i] != b) {
                if (i == length - 1 || x[i] > finalSpot || b < x[i]) return max;
                else if (y[i] > max) max = y[i];
                i++;
            }
    		
            return max;	
        }
        
        double integral(double a, double b) {
            double integ = 0.0;
            double trapFormula = 0.0;
            int i = 0;
            int num = 0;
    		
            while (x[i] != a) {
                if (i == length - 1) return integ;
                else i++;
            }
    		
            while (x[i] != b && i != length - 1) {
                trapFormula = ((x[i+1] - x[i]))*((y[i]+y[i+1])/2);
                integ += trapFormula;
                i++;
            }
    		
            return integ;
    		
        }
        
        ostream output(ostream &out) {
            int finalSpot = length - 1;
            for (int i = 0; i < finalSpot; i++)
                    out << "(" << x[i] << "," << y[i] << "):";
                    
            out << "(" << x[finalSpot] << "," << y[finalSpot] << ")" << endl;
            return out;
        }
        
        ostream & operator << (ostream & out, const PolyLine ob) {
            return ob.output(out);
        }
    };
    Can you tell me what I'm doing wrong now? Thanks.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    try this:

    Code:
    #include <iostream>
    using namespace std;
    
    class PolyLine {
        double *x;
        double *y;
        int length;
        
      public:
        PolyLine (double xVals[], double yVals[], int num) {
            length = num;
            x = new double[length];
            y = new double[length];
            
            for (int i = 0; i < length; i++) {
                    x[i] = xVals[i];
                    y[i] = yVals[i];
            }
        }
        
        int points() {
            return length;
        }
        
        double value(double xNum) {
            double val = 0.0;
            double finalSpot = x[length - 1];
    		
            if (xNum < x[0] || xNum > finalSpot) return val;
    		
            for (int i = 0; i < length; i++) {
                if (x[i] == xNum) val = y[i];
                else if (x[i] != xNum && x[i] > xNum) val = xNum;
            }
    		
            return val;
        }
        
        double max() {
            double max = 0.0;
    		
            for (int i = 0; i < length; i++)
                if (y[i] > max) max = y[i];
    		
            return max;
        }
        
        double max(double a, double b) {
            double max = 0.0;
            double finalSpot = x[length - 1];
            int i = 0;
    		
            while (x[i] != a) {
                if (i == length - 1) return max;
                else if (x[i] > a) {
                   if (y[i] < 0) break;
                   else {
                     max = y[i];
                     break;
                   }
                }
                else i++;
            }
    		
            while (x[i] != b) {
                if (i == length - 1 || x[i] > finalSpot || b < x[i]) return max;
                else if (y[i] > max) max = y[i];
                i++;
            }
    		
            return max;	
        }
        
        double integral(double a, double b) {
            double integ = 0.0;
            double trapFormula = 0.0;
            int i = 0;
            int num = 0;
    		
            while (x[i] != a) {
                if (i == length - 1) return integ;
                else i++;
            }
    		
            while (x[i] != b && i != length - 1) {
                trapFormula = ((x[i+1] - x[i]))*((y[i]+y[i+1])/2);
                integ += trapFormula;
                i++;
            }
    		
            return integ;
    		
        }
        
      friend  ostream& operator <<(ostream &out, const PolyLine& ob) {
            int finalSpot = ob.length - 1;
            for (int i = 0; i < finalSpot; i++)
                    out << "(" << ob.x[i] << "," << ob.y[i] << "):";
                    
            out << "(" << ob.x[finalSpot] << "," << ob.y[finalSpot] << ")" << endl;
            return out;
        }
        
    };

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    There we go, that did it! Thanks for the help.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sorry I forgot an & in my code. The return for output should be a reference to an object and not an object itself. Code has been updated. Btw in the future you may want to include the errors instead of just saying you have errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. operator overloaded but not inherited
    By vaibhav in forum C++ Programming
    Replies: 5
    Last Post: 08-07-2006, 06:02 AM
  3. problem with overloader bracker operator
    By cdonlan in forum C++ Programming
    Replies: 8
    Last Post: 04-11-2005, 03:05 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM
  5. overloaded << AP classes
    By lithium in forum C++ Programming
    Replies: 12
    Last Post: 02-11-2003, 01:42 AM

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