namespace? include errors??

  • Thread starter Francesco Gallarotti
  • Start date
F

Francesco Gallarotti

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::eek:perator <(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::eek:perator ==(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)
 
W

WW

Francesco said:
class Vertex {
friend ostream &operator<<(ostream&, const Vertex&); <<<<<<<<<

You need to qualify with std:: here.
friend std::eek:stream &operator<<(std::eek:stream&, 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

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.
 
T

tom_usenet

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;

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

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

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);

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

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>

Why the legacy header?

Tom
 
F

Frank Schmitt

WW said:
You need to qualify with std:: here.
friend std::eek:stream &operator<<(std::eek:stream&, const Vertex&);

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
 
A

Attila Feher

Frank said:
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.

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.
:)
 
F

Frank Schmitt

Attila Feher said:
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.
:)

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
 
A

Attila Feher

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. Actually friend declarations are veeery interesting beasts.
Herb Sutter has some pretty damn good presentation(s) about it.
 
A

Attila Feher

Attila said:
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.

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

Frank Schmitt

Attila Feher said:
Attila said:
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.

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

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/wg21/docs/cwg_active.html#138

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

kind regards
frank
 
A

Attila Feher

Frank said:
Attila Feher said:
Attila said:
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.

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

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/wg21/docs/cwg_active.html#138

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

I could not see any consensus either. :-(
 
G

Gavin Deane

Frank Schmitt said:
Attila Feher said:
Attila said:
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.

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

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/wg21/docs/cwg_active.html#138

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

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

GJD
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top