相互交叉的C ++ / Classes中的循环依赖 [英] Circular Dependecies in C++ / Classes that dpend each other

查看:173
本文介绍了相互交叉的C ++ / Classes中的循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:



我有一个Pawn,位于字段上。所以我有类Pawn和类字段。我想要从字段访问站在它的Pawn,我想从Pawn到它站在它的字段访问。



所以我有类:

  
class Player;
class Field;
class PlayerList;

类Pawn
{
public:
int intID;
Player * plrPlayer;
Field * fldField;

...

int getPosition()
{
//获取其所在字段的ID
return fldField- > intID; //这里我得到错误
}
}

class Field
{
public:
Pawn * pwnPawn;
int intID;
Field()
{
intID = -1;
}
Field(int intIDParam)
{
intID = intIDParam;
}
};

g ++说

 错误:无效使用不完整类型类字段
错误:类的转发声明字段


b $ b

  main.cpp:在Elementfunktion»int Pawn :: getPosition()«:
main.cpp:42:24:Fehler:falsche Benutzung desunvollständigenTyps»class Field«
main.cpp:10:7:Fehler:Vorwärtsdeklarationvon»class Field«

有没有其他方法声明类外面的东西,或者我真的必须声明所有的方法/成员之外,我需要之前?



我该怎么办?谢谢。



编辑:
谢谢,但我现在尝试将其分为.h和.cpp文件。后来我会为每个类使用.h和.cpp文件,但现在我只需要



header.h

  #ifndef _HEADERS_ 
#define _HEADERS_
#include< iostream>
#include< unistd.h>
#include< stdlib.h>
#include< vector>
using namespace std;

class Pawn;
class Player;
class Field;
class PlayerList;

类Pawn
{
public:
int intID;
Player * plrPlayer;
Field * fldField;

//构造函数....

int getPosition();
...
};
#endif

implements.cpp

  #ifndef _IMPLEMENTATIONS_ 
#define _IMPLEMENTATIONS_
#includeheaders.h
//Pawn.cpp
int Pawn :: getPosition()
{
return fldField-> intID;
}

...

#endif



并且在main.cpp中包含implements.cpp



我得到错误

 在函数Pawn :: getPosition 
中的多个定义Pawn :: getPosition

我做错了什么?

解决方案

定义在 Pawn

  int getPosition 


之后 c $ c>,实现它:

  int Pawn :: getPosition()const 
{
/ /获取它所处的字段的ID
return fldField-> intID;
}

(注意我添加了 const get 建议此方法不应修改其正在工作的实例)



想要将声明和定义分离为头文件和实现文件,另一个答案已经有了一些信息。






创建 pawn.h field.h



您的实现文件 pawn.cpp 应该看起来像这样: / p>

  //注意:没有为
包含保护程序#includepawn.h pawn
#includefield.h//和字段的标题,因为你使用它

int Pawn :: getPosition()
{
return fldField-> intID;
}

field.cpp 。然后你分别编译这些文件,你的 main.cpp 不应该包括 *。cpp 文件。



要编译和链接一切,这应该让你开始:

 将每个源文件编译成一个目标文件
g ++ -Wall -c field.cpp -o field.o
g ++ -Wall -c pawn.cpp -o pawn.o
g ++ -Wall - c main.cpp -o main.o
#将它们链接到一个可执行文件
g ++ field.o pawn.o main.o -o main
#运行可执行文件
。 / main


i have the following problem:

I have a Pawn, that stands on a Field. So I have the Class Pawn and the Class Field. I want to have access from the Field to the Pawn that stands on it, and I want to have access from the Pawn to the Field it stands on.

So i have classes:

class Pawn;
class Player;
class Field;
class PlayerList;

class Pawn
{
    public:
        int intID;
        Player* plrPlayer;
        Field* fldField;

        ...

 int getPosition()
{
    //gets the ID of the Field it stands on 
    return fldField->intID; //Here i get the error
}
}

class Field
{
    public:
    Pawn* pwnPawn;
    int intID;
    Field()
    {
        intID = -1;
    }
    Field(int intIDParam)
    {
        intID = intIDParam;
    }
};

g++ says

Error: invalid use of incomplete type "class Field"
Error: Forward declaration of class Field

or

    main.cpp: In Elementfunktion »int Pawn::getPosition()«:
    main.cpp:42:24: Fehler: falsche Benutzung des unvollständigen Typs »class Field«
    main.cpp:10:7: Fehler: Vorwärtsdeklaration von »class Field«

Is there an other way than declaring things outside the class or do I really have to declare all Methods/Members outside it before I need they?

What can I do? Thank you.

EDIT: Thank you, but i tried now to seperate it into a .h and a .cpp file. Later i will use a .h and a .cpp file for each class, but now i simply have

header.h

#ifndef _HEADERS_
#define _HEADERS_
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <vector>
using namespace std;

class Pawn;
class Player;
class Field;
class PlayerList;

class Pawn
{
    public:
        int intID;
        Player* plrPlayer;
        Field* fldField;

//Constructors....

int getPosition();
...
};
#endif 

implementations.cpp

#ifndef _IMPLEMENTATIONS_
#define _IMPLEMENTATIONS_
#include "headers.h"
//Pawn.cpp
int Pawn::getPosition()
{
    return fldField->intID;
}

...

#endif

and in main.cpp i include "implementations.cpp"

I get the error

In function Pawn::getPosition
multiple definition of Pawn::getPosition

What do I do wrong?

解决方案

Use a declaration instead of a definition in the class Pawn:

int getPosition() const;

and after the class Field, implement it:

int Pawn::getPosition() const
{
    //gets the ID of the Field it stands on 
    return fldField->intID;
}

(note I added const as the get suggest that this method should not modify the instance it is working on)

You will later want to separate declarations and definitions into header and implementation files, the other answer already has some information on that.


Separate the headers, create pawn.h and field.h. I think you know how to do that.

Your implementation file pawn.cpp should look something like this:

// note: no include guards for the 
#include "pawn.h" // you need the header for pawn
#include "field.h" // and the header for field since here you are using it

int Pawn::getPosition()
{
    return fldField->intID;
}

You do the same for field.cpp. Then you compile those files separately, also your main.cpp should not include the *.cpp files.

To compile and link everything together, this should get you started:

# compile each source file into an object file
g++ -Wall -c field.cpp -o field.o
g++ -Wall -c pawn.cpp -o pawn.o
g++ -Wall -c main.cpp -o main.o
# link them together into an executable
g++ field.o pawn.o main.o -o main
# run the executable
./main

这篇关于相互交叉的C ++ / Classes中的循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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