overloaded operators and namespaces compile problems

Discussion in 'C++' started by George Economos, Aug 23, 2005.

  1. Hi all,

    I am using msvc 7.1 and have encountered the following code:

    -----------
    A.hpp
    -----------

    class A
    {
    public:

    class Vector : public std::vector< std::pair< int, bool > >
    {
    friend std::istream & operator>>(
    const std::istream & is, Vector & v );

    friend std:eek:stream & operator<< (
    const std::eek:stream & os, const Vector & v );
    };

    const Vector & GetVector() const { return v; }

    private:

    Vector v;
    };

    -----------
    B.hpp
    -----------

    namspace BLib
    {
    class B
    {
    void SomeFunc();
    };
    }

    -----------
    B.cpp
    -----------
    #include "B.hpp"
    #include "A.hpp"

    using namespace BLib;

    void B::SomeFunc()
    {
    A a;

    std::strstream ss;
    ss << a;
    }


    When I try and compile B.cpp the compiler complains that it cannot
    find a suitable overload for operator << that takes a right hand
    argument of Vector (more specifically vector< pair< int, bool > > )
    (C2679). If I remove class B from namespace BLib it compiles fine.
    What am I doing wrong?

    Thanks,
    George Economos
     
    George Economos, Aug 23, 2005
    #1
    1. Advertising

  2. George Economos wrote:
    > I am using msvc 7.1 and have encountered the following code:
    >
    > -----------
    > A.hpp
    > -----------
    >
    > class A
    > {
    > public:
    >
    > class Vector : public std::vector< std::pair< int, bool > >
    > {
    > friend std::istream & operator>>(
    > const std::istream & is, Vector & v );
    >
    > friend std:eek:stream & operator<< (
    > const std::eek:stream & os, const Vector & v );
    > };


    I really wish nobody attempted to _write_ code directly in their newsgroup
    reader program. Can't you copy-and-paste?

    Anyway, syntax errors aside, you define operators for the 'Vector' here.

    >
    > const Vector & GetVector() const { return v; }
    >
    > private:
    >
    > Vector v;
    > };
    >
    > [...]
    > void B::SomeFunc()
    > {
    > A a;
    >
    > std::strstream ss;
    > ss << a;


    And here you're trying to output an object of type A, for which no
    operator<< is defined. What do you expect?

    > }
    >
    >
    > When I try and compile B.cpp the compiler complains that it cannot
    > find a suitable overload for operator << that takes a right hand
    > argument of Vector (more specifically vector< pair< int, bool > > )
    > (C2679). If I remove class B from namespace BLib it compiles fine.
    > What am I doing wrong?


    Everything. Try it again. And this time post _real_ code. See FAQ 5.8.

    V
     
    Victor Bazarov, Aug 23, 2005
    #2
    1. Advertising

  3. Thanks Victor for your help. Unfortunately I can't directly paste the
    code as it is confidential.

    Your right about SomeFunc(), it should have been defined as:

    void B:SomeFunc()
    {
    A a;
    std::strstream ss;
    ss << a.GetVector();
    }

    If you have anything more constructive to say I would really
    appreciate it.

    Thanks Again,
    -george

    On Tue, 23 Aug 2005 10:20:51 -0400, Victor Bazarov
    <> wrote:

    >George Economos wrote:
    >> I am using msvc 7.1 and have encountered the following code:
    >>
    >> -----------
    >> A.hpp
    >> -----------
    >>
    >> class A
    >> {
    >> public:
    >>
    >> class Vector : public std::vector< std::pair< int, bool > >
    >> {
    >> friend std::istream & operator>>(
    >> const std::istream & is, Vector & v );
    >>
    >> friend std:eek:stream & operator<< (
    >> const std::eek:stream & os, const Vector & v );
    >> };

    >
    >I really wish nobody attempted to _write_ code directly in their newsgroup
    >reader program. Can't you copy-and-paste?
    >
    >Anyway, syntax errors aside, you define operators for the 'Vector' here.
    >
    >>
    >> const Vector & GetVector() const { return v; }
    >>
    >> private:
    >>
    >> Vector v;
    >> };
    >>
    >> [...]
    >> void B::SomeFunc()
    >> {
    >> A a;
    >>
    >> std::strstream ss;
    >> ss << a;

    >
    >And here you're trying to output an object of type A, for which no
    >operator<< is defined. What do you expect?
    >
    >> }
    >>
    >>
    >> When I try and compile B.cpp the compiler complains that it cannot
    >> find a suitable overload for operator << that takes a right hand
    >> argument of Vector (more specifically vector< pair< int, bool > > )
    >> (C2679). If I remove class B from namespace BLib it compiles fine.
    >> What am I doing wrong?

    >
    >Everything. Try it again. And this time post _real_ code. See FAQ 5.8.
    >
    >V
     
    George Economos, Aug 23, 2005
    #3
  4. George Economos

    Alipha Guest

    George Economos wrote:
    > Thanks Victor for your help. Unfortunately I can't directly paste the
    > code as it is confidential.
    >
    > [snip]


    write a test case then. what you pasted was already a test case, you
    just didn't try compiling it first. Please post code that produces the
    same error, and only the same error, as your actual code.
     
    Alipha, Aug 23, 2005
    #4
  5. George Economos wrote:
    > Thanks Victor for your help. Unfortunately I can't directly paste the
    > code as it is confidential.
    >
    > Your right about SomeFunc(), it should have been defined as:
    >
    > void B:SomeFunc()
    > {
    > A a;
    > std::strstream ss;
    > ss << a.GetVector();
    > }
    >
    > If you have anything more constructive to say I would really
    > appreciate it.


    "More constructive"? Here you go...

    (a) Don't top-post.

    (b) Read the FAQ (http://www.parashift.com/c -faq-lite/).

    (c) This code:
    -----------------------------------------------------------
    #include <strstream>
    #include <vector>
    #include <utility>

    class A
    {
    public:
    class Vector : public std::vector<std::pair<int,bool> >
    {
    /* notice that streams cannot be 'const' */
    friend std::istream&
    operator >>(std::istream &, A::Vector &);
    friend std::eek:stream&
    operator <<(std::eek:stream &, const A::Vector &);
    };

    const Vector& GetVector() const { return v; }

    private:
    Vector v;
    };

    namespace BLib
    {
    class B
    {
    void SomeFunc();
    };
    }

    using namespace BLib;

    void B::SomeFunc()
    {
    A a;
    std::strstream ss;
    ss << a.GetVector();
    }
    -----------------------------------------------------------
    *compiles* without an error using VC++ v7.1.

    So, I don't know what you do wrong since you didn't post the _real_ code
    that gives you the error you claim to have seen.

    >
    > Thanks Again,
    > -george
    >
    > On Tue, 23 Aug 2005 10:20:51 -0400, Victor Bazarov
    > <> wrote:
    >
    >
    >>George Economos wrote:
    >>
    >>>I am using msvc 7.1 and have encountered the following code:
    >>>
    >>>-----------
    >>>A.hpp
    >>>-----------
    >>>
    >>>class A
    >>>{
    >>>public:
    >>>
    >>> class Vector : public std::vector< std::pair< int, bool > >
    >>> {
    >>> friend std::istream & operator>>(
    >>> const std::istream & is, Vector & v );
    >>>
    >>> friend std:eek:stream & operator<< (
    >>> const std::eek:stream & os, const Vector & v );
    >>> };

    >>
    >>I really wish nobody attempted to _write_ code directly in their newsgroup
    >>reader program. Can't you copy-and-paste?
    >>
    >>Anyway, syntax errors aside, you define operators for the 'Vector' here.
    >>
    >>
    >>> const Vector & GetVector() const { return v; }
    >>>
    >>>private:
    >>>
    >>> Vector v;
    >>>};
    >>>
    >>>[...]
    >>>void B::SomeFunc()
    >>>{
    >>> A a;
    >>>
    >>> std::strstream ss;
    >>> ss << a;

    >>
    >>And here you're trying to output an object of type A, for which no
    >>operator<< is defined. What do you expect?
    >>
    >>
    >>>}
    >>>
    >>>
    >>>When I try and compile B.cpp the compiler complains that it cannot
    >>>find a suitable overload for operator << that takes a right hand
    >>>argument of Vector (more specifically vector< pair< int, bool > > )
    >>>(C2679). If I remove class B from namespace BLib it compiles fine.
    >>>What am I doing wrong?

    >>
    >>Everything. Try it again. And this time post _real_ code. See FAQ 5.8.
    >>
    >>V


    V
     
    Victor Bazarov, Aug 23, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -Steve-
    Replies:
    2
    Views:
    372
    - Steve -
    Jul 28, 2003
  2. Replies:
    1
    Views:
    533
    Victor Bazarov
    Feb 7, 2005
  3. Riku Jarvinen
    Replies:
    7
    Views:
    512
    Riku Jarvinen
    Oct 24, 2005
  4. Riku Jarvinen
    Replies:
    5
    Views:
    421
    Thomas J. Gritzan
    Oct 26, 2005
  5. iluvatar
    Replies:
    3
    Views:
    332
    Sylvester Hesp
    Feb 12, 2007
Loading...

Share This Page