f:\ my school\cse528\showdxf\dxfparser.h(34):错误C2872:''ifstream'': 模糊符号 f:\ my school\cse528\showdxf\dxfparser.h(35):错误C2872:''ifstream'': 模糊符号 f: \ my school\cse528 \showdxf \dxfparser.h(36):错误C2872:''ifstream'': 模糊符号 f:\ my school\cse528 \showdxf \dxfparser.h(37):错误C2872:''ifstream'': 模糊符号 f:\ my school\ cse528 \ showdxf \ dxfparser.h(39):错误C2872:''ifstream'': 模糊符号 f:\ my school\cse528 \ showdxf\drawdxf.cpp(505):错误C2872:''cout'': 模糊符号 f:\ my school\cse528\showdxf\drawdxf.cpp(525):错误C2872:''cout'': 模糊符号 其中例如: #ifndef FG_VERTEX #define FG_VERTEX #include< iostream> 使用namespace std; class Vertex { friend ostream& operator<<(ostream&,const Vertex&); <<<<<<<<<第9行 public: Vertex(float vx,float vy,float vz); ~Vertex(){}; //默认析构函数 float getX(void){return x;}; //返回顶点X坐标 float getY(void){return y;}; //返回顶点Y坐标 float getZ(void){return z;}; //返回顶点Z坐标 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 又一次: #ifndef FG_DXF_PARSER #定义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是,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 让我们看一个例子错误C2662: f:\ my school\cse528\showdxf\vertex.cpp(17):错误C2662:''getX'':不能 将''this''指针从''const class Vertex''转换为''class Vertex&'' 转换失去限定词 f:\ my school\\ \\ cse528 \ showdxf\vertex.cpp(17):错误C2662:''getY'':不能 将''这个''指针从''const class Vertex''转换为''' class Vertex&'' 转换失去限定符 f:\ my school\cse528\showdxf\vertex.cpp(17):错误C2662:'' getZ'':不能 将''此''指针从''const class Vertex''转换为''class Vertex&'' 转换失去限定词 这里是vertex.cpp代码: #include< iostream> #include" vertex.h" Vertex :: Vertex(float vx,float vy,floa t vz){ x = vx; y = vy; z = vz; }; //默认构造函数 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()<<")" ;; 退出; }; //用于打印 bool Vertex :: operator<(const Vertex& v)const { if(x< vx)return true; else if(x == vx&& y< vy)返回true; else if(x == vx&& y == vy&& z< vz )返回true; 返回false; }; bool Vertex :: operator ==(const Vertex& v)const { return(x == vx&& y == vy&& z == vz); }; 有什么想法吗?我在这里真的很困惑!唯一让我这么开心的事情就是那里的人可能知道解决这个问题的原因...... 令人沮丧的情况.... 注意:错误C2662消失我从运算符<< 定义中删除const,但我甚至不知道这是否正确重载现在 (Deitel的书中显示const) 解决方案 Francesco Gallarotti写道: class Vertex { friend ostream& operator<<(ostream&,const Vertex&); <<<<<<<<< 您需要获得std :: here资格。 朋友std :: ostream& operator<<(std :: ostream&,const Vertex& ;); 第9行public: Vertex(float vx,float vy,float vz); ~Vertex(){}; //默认析构函数 float getX(void){return x;}; //返回顶点X坐标 float getY(void){return y;}; //返回顶点Y坐标 float getZ(void){return z;}; //返回顶点Z坐标 float getX(void)const {return x;}; float getY(void)const {return y;}; float getZ(void)const {return z;}; 将它们设为const。他们没有改变对象。 - WW aka Attila 10月6日星期一2003 13:57:34 GMT,Francesco Gallarotti < ga ******** @ hotmail.com>写道: 你们任何人都可以帮忙吗?几乎所有的错误我都属于2 不同的类别: 1)错误C2872:含糊不清的符号2)错误C2662:无法转换''this''指针来自''const类Vertex''到''类Vertex&'' 其中例如: #ifndef FG_VERTEX #define FG_VERTEX #include< iostream> 使用命名空间std; 删除以上行!永远不要使用using namespace std在标题中, 因为它产生了你所看到的问题。 class Vertex {朋友ostream& operator<<(ostream&,const顶点&安培); <<<<<<<<<第9行 朋友std :: ostream& operator<<(std :: ostream&,const Vertex&); public : Vertex(float vx,float vy,float vz); ~Vertex(){}; //默认析构函数 float getX(void){return x;}; //返回顶点X坐标 float getY(void){return y;}; //返回顶点Y坐标 float getZ(void){return z;}; //返回顶点Z坐标 void set(float newX,float newY,float newZ); float getX(void)const {return x;}; //返回顶点X坐标 float getY(void)const {return y;}; //返回顶点Y坐标 float getZ(void)const {return z;}; //返回顶点Z坐标 bool运算符<(const Vertex& v)const; bool运算符==(const Vertex& v)const; 私有:浮动x,y,z; }; #endif 再次: #ifndef FG_DXF_PARSER #define FG_DXF_PARSER #include< fstream.h> 为什么遗留标题? Tom " WW" < WO *** @ freemail.hu>写道: Francesco Gallarotti写道: class Vertex { friend ostream& operator<<(ostream&,const Vertex&); <<<<<<<<< 你需要获得std :: here资格。朋友std :: ostream& ;运算符<<(std :: ostream&,const Vertex&); 不,他没有 - 他有一个 使用命名空间std; 在类顶点声明之前。 到OP:永远不要在头文件中使用指令或声明 档。 亲切的问候 frank - Frank Schmitt 4SC AG电话:+49 89 700763-0 电子邮件:frankNO DOT SPAMschmitt at 4sc DOT com Can any of you help with this? Pretty much all the errors I have belong to 2different categories:1) error C2872: ambiguous symbols2) 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'' : ambiguoussymbolf:\my school\cse528\showdxf\vertex.h(9) : error C2872: ''ostream'' : ambiguoussymbolf:\my school\cse528\showdxf\triangle.h(9) : error C2872: ''ostream'' :ambiguous symbolf:\my school\cse528\showdxf\triangle.h(9) : error C2872: ''ostream'' :ambiguous symbolf:\my school\cse528\showdxf\edge.h(12) : error C2872: ''ostream'' : ambiguoussymbolf:\my school\cse528\showdxf\edge.h(12) : error C2872: ''ostream'' : ambiguoussymbolf:\my school\cse528\showdxf\dxfparser.h(34) : error C2872: ''ifstream'' :ambiguous symbolf:\my school\cse528\showdxf\dxfparser.h(35) : error C2872: ''ifstream'' :ambiguous symbolf:\my school\cse528\showdxf\dxfparser.h(36) : error C2872: ''ifstream'' :ambiguous symbolf:\my school\cse528\showdxf\dxfparser.h(37) : error C2872: ''ifstream'' :ambiguous symbolf:\my school\cse528\showdxf\dxfparser.h(39) : error C2872: ''ifstream'' :ambiguous symbolf:\my school\cse528\showdxf\drawdxf.cpp(505) : error C2872: ''cout'' :ambiguous symbolf:\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 9public:Vertex(float vx, float vy, float vz);~Vertex() { }; // default destructorfloat getX(void) {return x;}; // returns vertex X coordinatefloat getY(void) {return y;}; // returns vertex Y coordinatefloat getZ(void) {return z;}; // returns vertex Z coordinatevoid 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'' : cannotconvert ''this'' pointer from ''const class Vertex'' to ''class Vertex &''Conversion loses qualifiersf:\my school\cse528\showdxf\vertex.cpp(17) : error C2662: ''getY'' : cannotconvert ''this'' pointer from ''const class Vertex'' to ''class Vertex &''Conversion loses qualifiersf:\my school\cse528\showdxf\vertex.cpp(17) : error C2662: ''getZ'' : cannotconvert ''this'' pointer from ''const class Vertex'' to ''class Vertex &''Conversion loses qualifiershere 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 constructorvoid 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 printingbool 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 mehappy here is that maybe somebody out there knows the solution to thisfrustrating 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) 解决方案 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&); 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.--WW aka AttilaOn Mon, 06 Oct 2003 13:57:34 GMT, "Francesco Gallarotti"<ga********@hotmail.com> wrote: Can any of you help with this? Pretty much all the errors I have belong to 2different categories:1) error C2872: ambiguous symbols2) 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 9friend std::ostream &operator<<(std::ostream&, 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 coordinatefloat getY(void) const {return y;}; // returns vertex Y coordinatefloat 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;};#endifand again:#ifndef FG_DXF_PARSER#define FG_DXF_PARSER#include <fstream.h> Why the legacy header? Tom"WW" <wo***@freemail.hu> writes: 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&); 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 headerfiles. kind regardsfrank --Frank Schmitt4SC AGphone: +49 89 700763-0e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com 这篇关于命名空间?包括错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 18:22