overloaded operators and namespaces compile problems

G

George Economos

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
 
V

Victor Bazarov

George said:
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
 
G

George Economos

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

George said:
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
 
A

Alipha

George said:
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.
 
V

Victor Bazarov

George said:
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

George said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top