我不知道我的简单代码有什么问题 [英] I don't know what's wrong with my very simple code

查看:64
本文介绍了我不知道我的简单代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习用C ++编写代码并编写一些非常简单的代码。我认为它符合每条规则,但总是有编译错误

,我不明白。


该代码包括以下5个文件,由//////分隔:


//////////////// pose.h

#ifndef pose_h

#define pose_h

#include" point.h"

class pose

{

int i;

public:

friend void point :: init(姿势P);

};

#endif


////////////////pose.cpp

#include" pose.h"


////////////////point.h

#ifndef point_h

#define point_h

#include" pose.h"

class point

{

public:

void init(姿势P);

};

#endif


////////////////point.cpp

#include" point.h"

void point :: init(姿势P)

{

Pi = 1;

}


////////////////main.cpp

#include" pose.h"

#include" point.h"

void main()

{

}

基本上是点。 class具有修改pose对象''私有数据i的功能。 VC ++总是给出如下错误消息:

''point'':不是类或命名空间名称

''我'':无法访问类中声明的私有成员''pose''

语法错误:标识符''姿势''

不存在的函数''point :: init''指定为朋友

我无法理解。谁能告诉我他的想法呢?

非常感谢。

解决方案

Sam写道:

我正在学习用C ++编写代码并编写一些非常简单的代码。我认为它符合所有规则,但总是编译错误
我不明白。

代码包括以下5个文件,由/分隔/////:

////////////////pose.h
#ifndef pose_h
#define pose_h #include" point.h"
班级姿势
{
int i;


我默认是私人的。这是编译器告诉你的。好

练习说*应该是*私密的。使用朋友类/功能或使用

访问器来摆弄i的值。对于真正的程序,你应该重新考虑你的整体设计。


你正处于养成一些坏习惯的边缘。

public:
friend void point :: init(姿势P);
};
#endif

//////////// ////pose.cpp
#include" pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include" pose.h"
类点
{
公开:
void init(姿势P);
};
#endif

////////////////point.cpp
#include" point.h" ;
void point :: init(姿势P)

Pi = 1;
}

/////////// /////main.cpp
#include" pose.h"
#include" point.h"
void main()
{
}

基本上是点。 class具有修改pose对象'的私有数据i的功能。 VC ++总是给出如下错误消息:
''point'':不是类或命名空间名称
''我'':无法访问类''pose'中声明的私有成员'语法错误:标识符''pose''
不存在的函数''point :: init''指定为朋友




友元函数不能是类的成员。所以point :: init不能是一个朋友构成的



雅各布


" Sam" < HC *** @ nd.edu>在消息中写道

news:f0 ************************* @ posting.google.co m ... < blockquote class =post_quotes>我正在学习使用C ++编写代码并编写一些非常简单的代码。我认为它符合所有规则,但总是编译错误
我不明白。

代码包括以下5个文件,由/分隔/////:

////////////////pose.h
#ifndef pose_h
#define pose_h #include" point.h"
班级姿势
{i /;
公众:
朋友虚空点:: init(姿势P);
};
#endif

////////////////pose.cpp
#include" pose.h"

////////////////point.h
#ifndef point_h
#define point_h
#include" pose。 h
课程要点
{
公开:
void init(姿势P);
};
#endif

////////////////point.cpp
#include" point.h"
void point :: init(姿势P)
{< br => Pi = 1;
}
////////////////main.cpp
#include" pose.h" ;
#include" point.h"
void main()
{
}

基本上是点&q UOT; class具有修改pose对象'的私有数据i的功能。 VC ++总是给出如下错误消息:
''point'':不是类或命名空间名称
''我'':无法访问类''pose'中声明的私有成员'语法错误:标识符''pose''
不存在的函数''point :: init''指定为朋友

我无法理解它。有人能告诉我他对此的看法吗?
非常感谢。



osmium写道:

Sam写道:

我正在学习用C ++编写代码并编写一些非常简单的代码。我认为它符合所有规则,但总是编译错误
我不明白。

代码包括以下5个文件,由/分隔/////:

////////////////pose.h
#ifndef pose_h
#define pose_h #include" point.h"
类姿势
{i /;



i默认为私有。这是编译器告诉你的。好的
实践说它*应该是*私人的。使用朋友类/功能或使用
访问器来摆弄i的值。对于一个真正的程序,你应该重新考虑你的整体设计。

你即将养成一些坏习惯。



除了设计问题,OP的问题是为什么朋友

声明无法按照他认为的那样工作。他知道我是私人的b $ b,他确实有一个朋友的功能宣告。


对OP来说,你可以通过组合解决你的问题一个

参考参数和一个前瞻性声明:


///////////////////// ////////////////////////////////////////////////// /////

// A.hpp

#ifndef A_HPP_INCLUDED

#define A_HPP_INCLUDED


#include" B.hpp"


A级

{

/ * ... * /

朋友无效B:f(A& a); //一个前瞻性声明不够

//这里;你必须有完整的定义

//的B,因此#include以上。

/ * ... * /

};


#endif //包括后卫


//////////////////// ////////////////////////////////////////////////// //////

// B.hpp

#ifndef B_HPP_INCLUDED

#define B_HPP_INCLUDED


A级; //前方声明


B级

{

/ * ... * /

公众:

void f(A& a); //注意,对于一个引用或指针

//到A,你只需要转发

//声明,而不是完整的类

//定义。


/ * ... * /

};

您之前尝试的原因没有''工作是你的包含

指令会导致循环依赖,你的

包括防范。 (毕竟这就是守卫的用途。)


如果你不能使用参考参数,那么你将不得不尝试

其他东西 - 就像A类中的公共访问器方法一样。但是,

你应该更喜欢在大多数情况下通过引用传递类,因为

传递值调用类的复制构造函数。请注意,在那个

的情况下,该函数只会修改参数的副本,

这几乎肯定不是你想要的。

HTH,


- Adam


-

反向回复域名。


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.

解决方案

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. 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.
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 be
a friend to pose.

Jakob

"Sam" <hc***@nd.edu> wrote in message
news: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. 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. 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 friend
declaration doesn''t work as he thinks it should. He knows that i is
private, and he did have a friend function declared.

To the OP, you could get around your problem by using a combination of a
reference 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_INCLUDED

class A; // forward declaration

class 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 include
directives would have resulted in a circular dependency, which your
include guard prevented. (That''s what include guards are for, after all.)

If you can''t use a reference parameter, then you will have to try
something else -- like a public accessor method in class A. However,
you should prefer to pass classes by reference in most situations, as
passing by value invokes the class'' copy constructor. Note that in that
case, 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.


这篇关于我不知道我的简单代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆