problem of the overloaded operator

This is a discussion on problem of the overloaded operator within the C++ Programming forums, part of the General Programming Boards category; I'm trying to compile a C++program(that ran in Linux before) in VC6. Here has the overloaded operator problem. Code: ---------------------------------------------------------------------- ...

  1. #1
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7

    problem of the overloaded operator

    I'm trying to compile a C++program(that ran in Linux before) in VC6. Here has the overloaded operator problem.
    Code:
    ----------------------------------------------------------------------
    aabb.h
    =======
    struct AABB
    {
      AABB();
      AABB(const Vector3&, const Vector3&);
      ... ...
      Vector3 bmin, bmax;
    };
    ... ...
    ... ...
    ----------------------------------------------------------------------
    
    bound.h
    =======
    class BoundBox : public AABB {
    public:
      ......
      inline BoundBox &operator= (const BoundBox &that)     ;
      inline BoundBox &operator+=(const BoundBox &that)     ;
      inline int       operator==(const BoundBox &that)const;
    };
    
    (the global functions)
    istream &operator>>(istream &buf,      BoundBox &bb);
    ostream &operator<<(ostream &buf,const BoundBox &bb);
    ... ...
    ----------------------------------------------------------------------
    
    bound.cpp
    ========
    ... ...
    istream &operator>>(istream &buf, BoundBox &bb) {
      long orgFlags = buf.setf(ios::skipws);
      eatChar('{',buf);
      buf >> bb.bmin;
      eatChar(',',buf);
      buf >> bb.bmax;
      eatChar('}',buf);
      buf.flags(orgFlags);
      return buf;
    }
    ... ...
    -----------------------------------------------------------------------
    
    primitive.h
    ========
    #include <iostream>
    
    typedef std::ostream myostream;
    typedef std::istream myistream;
    
    (global functions)
    myostream& operator << (myostream&, const Vector3&);
    myistream& operator >> (myistream&, Vector3&);
    ... ...
    ------------------------------------------------------------------------
    primitive.cpp
    =========
    ... ...
    myostream& operator << (myostream& out, const Vector3& v)
    {
       out << v.x << " " << v.y << " " << v.z;
      return out;
    }
    
    myistream& operator >> (myistream& in, Vector3& v)
    {
      in >> v.x >> v.y >> v.z;
      return in;
    }
    ......
    I can't find any problem of such overloading function. But it has compiling error.

    bounds.cpp(78) : error C2678: binary '>>' : no operator defined which takes a left-hand operand of type 'class istream' (or there is no acceptable conversion)
    bounds.cpp(80) : error C2678: binary '>>' : no operator defined which takes a left-hand operand of type 'class istream' (or there is no acceptable conversion)

    That makes me confused for several days. Please help me. Thank you in advance~

    Arwen.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    ok.. i 'll try to take a stab at this..


    your specific error:

    binary '>>' : no operator defined which takes a left-hand operand of type 'class istream'


    so... let's take a look at the overloaded extraction operator function definition:

    Code:
    myistream& operator >> (myistream& in, Vector3& v)
    {
      in >> v.x >> v.y >> v.z;
      return in;
    }
    So you are passing in a reference to 'myistream'... but have you declared myistream as an object of the istream class..?

    Also.. is see the prototype for:
    Code:
    istream &operator>>(istream &buf,      BoundBox &bb);
    But I see no associated function that will accept a 'istream&' argument.
    Last edited by The Brain; 09-13-2004 at 02:52 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    bounds.cpp(78) : error C2678: binary '>>' : no operator defined which takes a left-hand operand of type 'class istream' (or there is no acceptable conversion)

    The error message means that the >> operator isn't overloaded for the class that's being defined in the bounds.cpp file. In this case it's because the >> and << operators are declared outside the class in the bounds.h file. Declare them as friend functions inside the class with public access:
    Code:
    bound.h
    =======
    class BoundBox : public AABB {
    public:
      ......
      inline BoundBox &operator= (const BoundBox &that)	 ;
      inline BoundBox &operator+=(const BoundBox &that)	 ;
      inline int	   operator==(const BoundBox &that)const;
    
    
      friend istream &operator>>(istream &buf,	  BoundBox &bb);
      friend ostream &operator<<(ostream &buf,const BoundBox &bb);
    };
    and I think the error messages will go away.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Making the function a friend will simply allow the function to access non-public members of the class.

    >> (or there is no acceptable conversion)
    "myistream" to "istream" is an acceptable converions.
    Based on what you've posted, I'm guessing that bounds.cpp does not #include primitive.h

    The following code layout compiles as expected under VC++ 6.0
    Code:
    //-------------------------------------------------------------------
    //A.h
    #ifndef A_HEADER
    #define A_HEADER
    
    #include <iostream>
    
    struct A
    {
        int val;
    };
    
    typedef std::istream myistream;
    
    myistream& operator >> (myistream &in, A &a);
    
    #endif //A_HEADER
    
    //-------------------------------------------------------------------
    //A.cpp
    #include "A.h"
    
    myistream& operator >> (myistream &in, A &a)
    {
        in >> a.val;
        return in;
    }
    
    //-------------------------------------------------------------------
    //B.h
    #ifndef B_HEADER
    #define B_HEADER
    
    #include "A.h"
    
    struct B
    {
        A a;
        int val;
    };
    
    myistream& operator >> (myistream &in, B &b);
    
    #endif //B_HEADER
    
    //-------------------------------------------------------------------
    //B.cpp
    
    #include "B.h"
    
    myistream& operator >> (myistream &in, B &b)
    {
        in >> b.a >> b.val;
        return in;
    }
    
    //-------------------------------------------------------------------
    //main.cpp
    #include <iostream>
    #include "B.h"
    
    int main()
    {
        B b;
    
        std::cin >> b;
    
        return 0;
    }//main
    gg

  5. #5
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7
    Quote Originally Posted by The Brain
    So you are passing in a reference to 'myistream'... but have you declared myistream as an object of the istream class..?

    Also.. is see the prototype for:
    Code:
    istream &operator>>(istream &buf,      BoundBox &bb);
    But I see no associated function that will accept a 'istream&' argument.
    Thank you for ur reply~~

    'myistream' has been defined in primitive.h by 'typedef std::istream myistream;'.

  6. #6
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7
    Thank you, elad~~

    I tried this but I think Codeplug said right. That's no work, 555...

  7. #7
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7
    Quote Originally Posted by Codeplug
    Making the function a friend will simply allow the function to access non-public members of the class.

    >> (or there is no acceptable conversion)
    "myistream" to "istream" is an acceptable converions.
    Based on what you've posted, I'm guessing that bounds.cpp does not #include primitive.h
    Thank you~~
    I have tried your code in VC6. Yeah, it works.

    But for my code, primitive.h has been included in bounds.cpp. it hasn't worked,yet. Everything seems ok. The operator "<<" hasn't problem, but ">>" has problem. Confused!

    Do anyone have any idea about it? If anyone is interested in trying to compile it, I can send the code.

    Thanks in advance.

    arwen.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    >> If anyone is interested ... I can send the code.
    Zip up all the code and post it.

    gg

  9. #9
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7
    Here is the code I compiled by VC6. I put it there temporarily because it's too large to upload here.

    http://www.cse.cuhk.edu.hk/~wwu1/www/temp/VCModeDef.zip

    The original code came from 'Game Programming Gem4' book CD. According to authors' description, it has been tested on Red Hat Linux 7.0. I only did some modification to compile it under VC6.

    Before compiling it, please make sure you have 'gl' and 'glut' in your PC because it needs these libraries.

    Thanks in advance for any help.

    arwen.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    "Find in Files"
    Searching for '<iostream'...
    C:\VCModeDef\math\mathprimitives.h(2057):#include <iostream>
    C:\VCModeDef\shared\stdincludes.h(30):#include <iostream>
    C:\VCModeDef\simulation\Simulation.h(36)://#include <iostream.h>
    C:\VCModeDef\simulation\Simulation.h(38):#include <iostream>
    C:\VCModeDef\simulation\bounds.cpp(38):#include <iostream.h>
    C:\VCModeDef\simulation\bounds.h(38):#include <iostream.h>
    You are mixing new and old headers. Convert everthing to use the new headers.

    gg

  11. #11
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7
    Quote Originally Posted by Codeplug
    "Find in Files"

    You are mixing new and old headers. Convert everthing to use the new headers.

    gg
    I convert

    #include <iostream.h>

    to

    #include <iostream>
    using namespace std;


    new error:
    bounds.h(148) : error C2872: 'istream' : ambiguous symbol
    bounds.h(148) : error C2872: 'istream' : ambiguous symbol
    bounds.h(149) : error C2872: 'ostream' : ambiguous symbol
    bounds.h(149) : error C2872: 'ostream' : ambiguous symbol
    bounds.h(290) : error C2872: 'istream' : ambiguous symbol
    ... ...
    -----------------------------------------------------------------------------
    It seems that namespace hasn't worked.
    So I replaced all 'istream/ostream ' by "std::istream/std:: ostream'
    in bounds.h.

    This time errors go away!!

    But I can't explain why. It must use "using namespace std" and "std::" at the same time?!

    Anyway, the code be compiled successfully this time.
    Thank you very much!

    arwen.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    That means you still have some non-standard headers somewhere.
    Searching for '#include'...
    C:\VCModeDef\VCModeDef\math\mathprimitives.cpp(29) :#include <OSTREAM.h>
    C:\VCModeDef\VCModeDef\modal\deform.h(38):#include <fstream.h>
    C:\VCModeDef\VCModeDef\simulation\tetCollision.cpp (27):#include "iostream.h"
    C:\VCModeDef\VCModeDef\shared\smesh.cpp(33):#inclu de <fstream.h>
    C:\VCModeDef\VCModeDef\shared\smesh.h(29):#include <fstream.h>
    I think that's all of em.

    gg

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    You'll also have namespace collisions with min, max, and swap. Just comment them out and use the ones from <algorithm>.

    gg

  14. #14
    Registered User Arwen's Avatar
    Join Date
    Sep 2004
    Posts
    7
    Quote Originally Posted by Codeplug
    You'll also have namespace collisions with min, max, and swap. Just comment them out and use the ones from <algorithm>.

    gg
    Yeah, I comment

    template<class type>
    inline const type& min(const type& a, const type& b) {
    return (a<b?a:b);
    }

    template<class type>
    inline const type& max(const type& a, const type& b) {
    return (a>b?a:b);
    }

    template <class type>
    void swap(type& a, type& b)
    {
    type temp = a;
    a = b;
    b = temp;
    }

    out and use <algorithm> but

    #define min(a,b) (a<b?a:b)
    #define max(a,b) (a>b?a:b)

    still need there.

    For I/O problem, I have corrected them according to your suggestion. Yeah, they are ok

    Thank you very much for your warm help!

    arwen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  4. Operator Overload problem
    By Kasatka in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2004, 09:29 PM
  5. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 06:04 PM

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