Connecting Tech Pros Worldwide Help | Site Map

namespace? include errors??

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:55 PM
Francesco Gallarotti
Guest
 
Posts: n/a
Default namespace? include errors??

Can any of you help with this? Pretty much all the errors I have belong to 2
different categories:
1) error C2872: ambiguous symbols
2) error C2662: cannot convert 'this' pointer from 'const class Vertex' to
'class Vertex &'

Let's see them:

f:\my school\cse528\showdxf\vertex.h(9) : error C2872: 'ostream' : ambiguous
symbol
f:\my school\cse528\showdxf\vertex.h(9) : error C2872: 'ostream' : ambiguous
symbol
f:\my school\cse528\showdxf\triangle.h(9) : error C2872: 'ostream' :
ambiguous symbol
f:\my school\cse528\showdxf\triangle.h(9) : error C2872: 'ostream' :
ambiguous symbol
f:\my school\cse528\showdxf\edge.h(12) : error C2872: 'ostream' : ambiguous
symbol
f:\my school\cse528\showdxf\edge.h(12) : error C2872: 'ostream' : ambiguous
symbol
f:\my school\cse528\showdxf\dxfparser.h(34) : error C2872: 'ifstream' :
ambiguous symbol
f:\my school\cse528\showdxf\dxfparser.h(35) : error C2872: 'ifstream' :
ambiguous symbol
f:\my school\cse528\showdxf\dxfparser.h(36) : error C2872: 'ifstream' :
ambiguous symbol
f:\my school\cse528\showdxf\dxfparser.h(37) : error C2872: 'ifstream' :
ambiguous symbol
f:\my school\cse528\showdxf\dxfparser.h(39) : error C2872: 'ifstream' :
ambiguous symbol
f:\my school\cse528\showdxf\drawdxf.cpp(505) : error C2872: 'cout' :
ambiguous symbol
f:\my school\cse528\showdxf\drawdxf.cpp(525) : error C2872: 'cout' :
ambiguous symbol

where for example:

#ifndef FG_VERTEX
#define FG_VERTEX
#include <iostream>
using namespace std;
class Vertex {
friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<< line 9
public:
Vertex(float vx, float vy, float vz);
~Vertex() { }; // default destructor
float getX(void) {return x;}; // returns vertex X coordinate
float getY(void) {return y;}; // returns vertex Y coordinate
float getZ(void) {return z;}; // returns vertex Z coordinate
void set(float newX, float newY, float newZ);
bool operator<(const Vertex& v) const;
bool operator==(const Vertex& v) const;
private:
float x,y,z;
};
#endif

and again:

#ifndef FG_DXF_PARSER
#define FG_DXF_PARSER
#include <fstream.h>
#include "model.h"
class DXFParser {
public:
void read_and_build(char *filename, Model *model);
private:
DXFSection getSection(ifstream is);
void readFace(ifstream is, Model *model);
void readPolyline(ifstream is, Model *model);
float getFloat(ifstream is);
void showError(void);
bool skipToHeader(ifstream is, char *header);
void trim(char *str, char *trimmed);
};
#endif

and let's see an example of the error C2662:
f:\my school\cse528\showdxf\vertex.cpp(17) : error C2662: 'getX' : cannot
convert 'this' pointer from 'const class Vertex' to 'class Vertex &'
Conversion loses qualifiers
f:\my school\cse528\showdxf\vertex.cpp(17) : error C2662: 'getY' : cannot
convert 'this' pointer from 'const class Vertex' to 'class Vertex &'
Conversion loses qualifiers
f:\my school\cse528\showdxf\vertex.cpp(17) : error C2662: 'getZ' : cannot
convert 'this' pointer from 'const class Vertex' to 'class Vertex &'
Conversion loses qualifiers
here is the vertex.cpp code:

#include <iostream>
#include "vertex.h"
Vertex::Vertex(float vx, float vy, float vz) {
x = vx; y = vy; z = vz;
}; // default constructor
void Vertex::set(float newX, float newY, float newZ) {
x = newX; y = newY; z = newZ;
};
ostream &operator<<(ostream& out, const Vertex& vertex) {
out << "(" << vertex.getX() << "," << vertex.getY() << "," <<
vertex.getZ() << ")";
return out;
}; // for printing
bool Vertex::operator <(const Vertex& v) const {
if(x<v.x) return true;
else if(x==v.x && y<v.y) return true;
else if(x==v.x && y==v.y && z<v.z) return true;
return false;
};
bool Vertex::operator ==(const Vertex& v) const {
return (x==v.x && y==v.y && z==v.z);
};

Any idea? I am getting really confused here! The only thing that makes me
happy here is that maybe somebody out there knows the solution to this
frustrating situation....

NOTE: the errors C2662 go away of I remove the const from the operator<<
definition, but i don't even know if this is correct overloading now
(Deitel's book shows "const" in it)



  #2  
Old July 19th, 2005, 06:55 PM
WW
Guest
 
Posts: n/a
Default Re: namespace? include errors??

Francesco Gallarotti wrote:[color=blue]
> class Vertex {
> friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<<[/color]

You need to qualify with std:: here.
friend std::ostream &operator<<(std::ostream&, const Vertex&);
[color=blue]
> line 9 public:
> Vertex(float vx, float vy, float vz);
> ~Vertex() { }; // default destructor
> float getX(void) {return x;}; // returns vertex X coordinate
> float getY(void) {return y;}; // returns vertex Y coordinate
> float getZ(void) {return z;}; // returns vertex Z coordinate[/color]

float getX(void) const {return x;};
float getY(void) const {return y;};
float getZ(void) const {return z;};

Make them const. They don't change the object.


--
WW aka Attila


  #3  
Old July 19th, 2005, 06:55 PM
tom_usenet
Guest
 
Posts: n/a
Default Re: namespace? include errors??

On Mon, 06 Oct 2003 13:57:34 GMT, "Francesco Gallarotti"
<gallarotti@hotmail.com> wrote:
[color=blue]
>Can any of you help with this? Pretty much all the errors I have belong to 2
>different categories:
>1) error C2872: ambiguous symbols
>2) error C2662: cannot convert 'this' pointer from 'const class Vertex' to
>'class Vertex &'
>
>where for example:
>
>#ifndef FG_VERTEX
>#define FG_VERTEX
>#include <iostream>
>using namespace std;[/color]

Delete the above line! Never put "using namespace std" in a header,
since it produces exactly the problems you are seeing.
[color=blue]
>class Vertex {
> friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<< line 9[/color]

friend std::ostream &operator<<(std::ostream&, const Vertex&);

[color=blue]
>public:
> Vertex(float vx, float vy, float vz);
> ~Vertex() { }; // default destructor
> float getX(void) {return x;}; // returns vertex X coordinate
> float getY(void) {return y;}; // returns vertex Y coordinate
> float getZ(void) {return z;}; // returns vertex Z coordinate
> void set(float newX, float newY, float newZ);[/color]

float getX(void) const {return x;}; // returns vertex X coordinate
float getY(void) const {return y;}; // returns vertex Y coordinate
float getZ(void) const {return z;}; // returns vertex Z coordinate

[color=blue]
> bool operator<(const Vertex& v) const;
> bool operator==(const Vertex& v) const;
>private:
> float x,y,z;
>};
>#endif
>
>and again:
>
>#ifndef FG_DXF_PARSER
>#define FG_DXF_PARSER
>#include <fstream.h>[/color]

Why the legacy header?

Tom
  #4  
Old July 19th, 2005, 06:58 PM
Frank Schmitt
Guest
 
Posts: n/a
Default Re: namespace? include errors??

"WW" <wolof@freemail.hu> writes:
[color=blue]
> Francesco Gallarotti wrote:[color=green]
> > class Vertex {
> > friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<<[/color]
>
> You need to qualify with std:: here.
> friend std::ostream &operator<<(std::ostream&, const Vertex&);[/color]

No, he doesn't - he has a

using namespace std;

just before the declaration of class Vertex.
To the OP: NEVER put using directives or declarations in header
files.

kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
  #5  
Old July 19th, 2005, 06:58 PM
Attila Feher
Guest
 
Posts: n/a
Default Re: namespace? include errors??

Frank Schmitt wrote:[color=blue]
> "WW" <wolof@freemail.hu> writes:
>[color=green]
>> Francesco Gallarotti wrote:[color=darkred]
>>> class Vertex {
>>> friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<<[/color]
>>
>> You need to qualify with std:: here.
>> friend std::ostream &operator<<(std::ostream&, const Vertex&);[/color]
>
> No, he doesn't - he has a
>
> using namespace std;
>
> just before the declaration of class Vertex.
> To the OP: NEVER put using directives or declarations in header
> files.[/color]

IIRC a friend declaration does not pick up names from a using directive. I
might be wrong. If you have chapter and verse I can be easily convinced.
:-)

--
Attila aka WW


  #6  
Old July 19th, 2005, 07:00 PM
Frank Schmitt
Guest
 
Posts: n/a
Default Re: namespace? include errors??

"Attila Feher" <attila.feher@lmf.ericsson.se> writes:
[color=blue]
> Frank Schmitt wrote:[color=green]
> > "WW" <wolof@freemail.hu> writes:
> >[color=darkred]
> >> Francesco Gallarotti wrote:
> >>> class Vertex {
> >>> friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<<
> >>
> >> You need to qualify with std:: here.
> >> friend std::ostream &operator<<(std::ostream&, const Vertex&);[/color]
> >
> > No, he doesn't - he has a
> >
> > using namespace std;
> >
> > just before the declaration of class Vertex.
> > To the OP: NEVER put using directives or declarations in header
> > files.[/color]
>
> IIRC a friend declaration does not pick up names from a using directive. I
> might be wrong. If you have chapter and verse I can be easily convinced.
> :-)[/color]

Aehm. You got me here - I honestly don't know whether a friend declaration
is different from a "normal" declaration regarding this ;-)
(damn, I *really* have to get a copy of the standard)

kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
  #7  
Old July 19th, 2005, 07:00 PM
Attila Feher
Guest
 
Posts: n/a
Default Re: namespace? include errors??

Frank Schmitt wrote:
[SNIP][color=blue]
> Aehm. You got me here - I honestly don't know whether a friend
> declaration is different from a "normal" declaration regarding this
> ;-) (damn, I *really* have to get a copy of the standard)[/color]

Ahh. I was hoping you will do the job. :-) I do not remember this exactly
either. I recall someone telling it does not pick up names from other
namespaces. Actually friend declarations are veeery interesting beasts.
Herb Sutter has some pretty damn good presentation(s) about it.

--
Attila aka WW


  #8  
Old July 19th, 2005, 07:00 PM
Attila Feher
Guest
 
Posts: n/a
Default Re: namespace? include errors??

Attila Feher wrote:[color=blue]
> Frank Schmitt wrote:
> [SNIP][color=green]
>> Aehm. You got me here - I honestly don't know whether a friend
>> declaration is different from a "normal" declaration regarding this
>> ;-) (damn, I *really* have to get a copy of the standard)[/color]
>
> Ahh. I was hoping you will do the job. :-) I do not remember this
> exactly either. I recall someone telling it does not pick up names
> from other namespaces.[/color]

I mean from using directives. Using declarations were (IIRC) told to be
different.

--
Attila aka WW


  #9  
Old July 19th, 2005, 07:00 PM
Frank Schmitt
Guest
 
Posts: n/a
Default Re: namespace? include errors??

"Attila Feher" <attila.feher@lmf.ericsson.se> writes:
[color=blue]
> Attila Feher wrote:[color=green]
> > Frank Schmitt wrote:
> > [SNIP][color=darkred]
> >> Aehm. You got me here - I honestly don't know whether a friend
> >> declaration is different from a "normal" declaration regarding this
> >> ;-) (damn, I *really* have to get a copy of the standard)[/color]
> >
> > Ahh. I was hoping you will do the job. :-) I do not remember this
> > exactly either. I recall someone telling it does not pick up names
> > from other namespaces.[/color]
>
> I mean from using directives. Using declarations were (IIRC) told to be
> different.[/color]

Hm. further research turned up that it's considered a defect in the
standard, and there seemed to be some confusion how it should be handled:

http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#138

According to the website, an informal consensus has been reached -
although they don't mention which one???

kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
  #10  
Old July 19th, 2005, 07:05 PM
Attila Feher
Guest
 
Posts: n/a
Default Re: namespace? include errors??

Frank Schmitt wrote:[color=blue]
> "Attila Feher" <attila.feher@lmf.ericsson.se> writes:
>[color=green]
>> Attila Feher wrote:[color=darkred]
>>> Frank Schmitt wrote:
>>> [SNIP]
>>>> Aehm. You got me here - I honestly don't know whether a friend
>>>> declaration is different from a "normal" declaration regarding this
>>>> ;-) (damn, I *really* have to get a copy of the standard)
>>>
>>> Ahh. I was hoping you will do the job. :-) I do not remember this
>>> exactly either. I recall someone telling it does not pick up names
>>> from other namespaces.[/color]
>>
>> I mean from using directives. Using declarations were (IIRC) told
>> to be different.[/color]
>
> Hm. further research turned up that it's considered a defect in the
> standard, and there seemed to be some confusion how it should be
> handled:
>
> http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#138
>
> According to the website, an informal consensus has been reached -
> although they don't mention which one???[/color]

I could not see any consensus either. :-(

--
Attila aka WW


  #11  
Old July 19th, 2005, 07:06 PM
Gavin Deane
Guest
 
Posts: n/a
Default Re: namespace? include errors??

Frank Schmitt <invalid@seesignature.info> wrote in message news:<4cd6d7g3n9.fsf@scxw21.4sc>...[color=blue]
> "Attila Feher" <attila.feher@lmf.ericsson.se> writes:
>[color=green]
> > Attila Feher wrote:[color=darkred]
> > > Frank Schmitt wrote:
> > > [SNIP]
> > >> Aehm. You got me here - I honestly don't know whether a friend
> > >> declaration is different from a "normal" declaration regarding this
> > >> ;-) (damn, I *really* have to get a copy of the standard)
> > >
> > > Ahh. I was hoping you will do the job. :-) I do not remember this
> > > exactly either. I recall someone telling it does not pick up names
> > > from other namespaces.[/color]
> >
> > I mean from using directives. Using declarations were (IIRC) told to be
> > different.[/color]
>
> Hm. further research turned up that it's considered a defect in the
> standard, and there seemed to be some confusion how it should be handled:
>
> http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#138
>
> According to the website, an informal consensus has been reached -
> although they don't mention which one???[/color]

Perhaps they have yet to agree which consensus has been reached :)

GJD
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.