By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

overloaded operators and namespaces compile problems

P: n/a
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:ostream & operator<< (
const std::ostream & 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
Aug 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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:ostream & operator<< (
const std::ostream & 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
Aug 23 '05 #2

P: n/a
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
<v.********@comAcast.net> 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:ostream & operator<< (
const std::ostream & 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

Aug 23 '05 #3

P: n/a

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.

Aug 23 '05 #4

P: n/a
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::ostream&
operator <<(std::ostream &, 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
<v.********@comAcast.net> 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:ostream & operator<< (
const std::ostream & 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
Aug 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.