#include" point.h" 语法错误:标识符''pose'' #include" point.h" 语法错误:标识符''pose'' #include" point.h" HTH, - Adam - 反向回复域名。 Hi I''m learning to code with C++ and wrote some very simple code. Ithink it''s consistent with every rule but always got compiling errorsthat I don''t understand.The code include 5 files as following, delimited by //////:////////////////pose.h#ifndef pose_h#define pose_h#include "point.h"class pose{int i;public:friend void point::init(pose P);};#endif////////////////pose.cpp#include "pose.h"////////////////point.h#ifndef point_h#define point_h#include "pose.h"class point{public:void init(pose P);};#endif////////////////point.cpp#include "point.h"void point::init(pose P){P.i=1;}////////////////main.cpp#include "pose.h"#include "point.h"void main(){}Basically the "point" class has a function to modify the "pose"object''s private data "i". VC++ always gives the error messages like:''point'' : is not a class or namespace name''i'' : cannot access private member declared in class ''pose''syntax error : identifier ''pose''nonexistent function ''point::init'' specified as friendI cannot understand it. Could anybody tell me what he thinks of it?Thanks a lot. 解决方案 Sam writes: Hi I''m learning to code with C++ and wrote some very simple code. I think it''s consistent with every rule but always got compiling errors that I don''t understand. The code include 5 files as following, delimited by //////: ////////////////pose.h #ifndef pose_h #define pose_h #include "point.h" class pose { int i;i is, by default private. Which is what the compiler told you. Goodpractice says it *should be* private. Use a friend class/function or use anaccessor to fiddle with the value of i. For a real program you shouldprobably rethink your overall design.You are on the verge of developing some bad habits. public: friend void point::init(pose P); }; #endif ////////////////pose.cpp #include "pose.h" ////////////////point.h #ifndef point_h #define point_h #include "pose.h" class point { public: void init(pose P); }; #endif ////////////////point.cpp #include "point.h" void point::init(pose P) P.i=1; } ////////////////main.cpp #include "pose.h" #include "point.h" void main() { } Basically the "point" class has a function to modify the "pose" object''s private data "i". VC++ always gives the error messages like: ''point'' : is not a class or namespace name ''i'' : cannot access private member declared in class ''pose'' syntax error : identifier ''pose'' nonexistent function ''point::init'' specified as friend A friend function can not be a member of a class. So point::init can not bea friend to pose.Jakob"Sam" <hc***@nd.edu> wrote in messagenews:f0*************************@posting.google.co m... Hi I''m learning to code with C++ and wrote some very simple code. I think it''s consistent with every rule but always got compiling errors that I don''t understand. The code include 5 files as following, delimited by //////: ////////////////pose.h #ifndef pose_h #define pose_h #include "point.h" class pose { int i; public: friend void point::init(pose P); }; #endif ////////////////pose.cpp #include "pose.h" ////////////////point.h #ifndef point_h #define point_h #include "pose.h" class point { public: void init(pose P); }; #endif ////////////////point.cpp #include "point.h" void point::init(pose P) { P.i=1; } ////////////////main.cpp #include "pose.h" #include "point.h" void main() { } Basically the "point" class has a function to modify the "pose" object''s private data "i". VC++ always gives the error messages like: ''point'' : is not a class or namespace name ''i'' : cannot access private member declared in class ''pose'' syntax error : identifier ''pose'' nonexistent function ''point::init'' specified as friend I cannot understand it. Could anybody tell me what he thinks of it? Thanks a lot.osmium wrote: Sam writes:Hi I''m learning to code with C++ and wrote some very simple code. Ithink it''s consistent with every rule but always got compiling errorsthat I don''t understand.The code include 5 files as following, delimited by //////:////////////////pose.h#ifndef pose_h#define pose_h#include "point.h"class pose{int i; i is, by default private. Which is what the compiler told you. Good practice says it *should be* private. Use a friend class/function or use an accessor to fiddle with the value of i. For a real program you should probably rethink your overall design. You are on the verge of developing some bad habits.The design issue aside, the OP''s question is about why the frienddeclaration doesn''t work as he thinks it should. He knows that i isprivate, and he did have a friend function declared.To the OP, you could get around your problem by using a combination of areference parameter and a forward declaration:////////////////////////////////////////////////////////////////////////////// A.hpp#ifndef A_HPP_INCLUDED#define A_HPP_INCLUDED#include "B.hpp"class A{/* ... */friend void B:f(A& a); // A forward declaration does not suffice// here; you must have the full definition// of B, hence the #include above./* ... */};#endif // include guard////////////////////////////////////////////////////////////////////////////// B.hpp#ifndef B_HPP_INCLUDED#define B_HPP_INCLUDEDclass A; // forward declarationclass B{/* ... */public:void f(A& a); // Note that for a reference or pointer// to A, you only need the forward// declaration, not the full class// definition./* ... */};The reason what you tried earlier didn''t work is that your includedirectives would have resulted in a circular dependency, which yourinclude guard prevented. (That''s what include guards are for, after all.)If you can''t use a reference parameter, then you will have to trysomething else -- like a public accessor method in class A. However,you should prefer to pass classes by reference in most situations, aspassing by value invokes the class'' copy constructor. Note that in thatcase, the function would only modify a copy of the parameter anyway,which is almost certainly not what you want.HTH,- Adam--Reverse domain name to reply. 这篇关于我不知道我的简单代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 18:04
查看更多