C ++前向对象声明

时间:2017-03-25 19:46:04

标签: c++ forward-declaration

我正试图弄清楚如何转发声明新对象存储其他新对象的对象,例如:

一个包含节点的队列,这些节点包含数据项,其中包含2个列表或最好是2个队列。

这是我的Data.h

#ifndef DATA_H_
#define DATA_H_

class List;
class Data {
public:
    Data();
    Data(int aID);
    virtual ~Data();
    virtual int getID();
    virtual void setID(int aID);
private:
    int ID;
    List *listOne;
    List *listTwo;
};

#endif /* DATA_H_ */

和我的Data.c

#include "Data.h"
#include "List.h"

Data::Data() {
    listOne = new List();
    listTwo = new List();
    Data::ID = 0;
}

Data::Data(int aID) {
    Data::listOne = new List();
    Data::listTwo = new List();
    Data::ID = aID;
}

Data::~Data() {
    delete(listOne);
    delete(listTwo);
}

int Data::getID() {
    return Data::ID;
}

void Data::setID(int aID) {
    Data::ID = ID;
}

错误消息:

..\Data.cpp: In constructor 'Data::Data()':
..\Data.cpp:12:23: error: invalid use of incomplete type 'class List'
  listOne = new List();
                       ^
In file included from ..\Data.cpp:8:0:
..\Data.h:11:7: note: forward declaration of 'class List'
 class List;
       ^
..\Data.cpp:13:22: error: invalid use of incomplete type 'class List'
  listTwo = new List();
                      ^
In file included from ..\Data.cpp:8:0:
..\Data.h:11:7: note: forward declaration of 'class List'
 class List;
       ^
..\Data.cpp: In constructor 'Data::Data(int)':
..\Data.cpp:18:29: error: invalid use of incomplete type 'class List'
  Data::listOne = new List();
                             ^
In file included from ..\Data.cpp:8:0:
..\Data.h:11:7: note: forward declaration of 'class List'
 class List;
       ^
..\Data.cpp:19:28: error: invalid use of incomplete type 'class List'
  Data::listTwo = new List();
                            ^
In file included from ..\Data.cpp:8:0:
..\Data.h:11:7: note: forward declaration of 'class List'
 class List;
       ^
..\Data.cpp: In destructor 'virtual Data::~Data()':
..\Data.cpp:24:18: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
  delete(listOne);
                  ^
..\Data.cpp:24:18: warning: invalid use of incomplete type 'class List'
In file included from ..\Data.cpp:8:0:
..\Data.h:11:7: note: forward declaration of 'class List'
 class List;
       ^
..\Data.cpp:24:18: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
  delete(listOne);
                  ^
..\Data.cpp:25:17: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
  delete(listTwo);
                 ^
..\Data.cpp:25:17: warning: invalid use of incomplete type 'class List'
In file included from ..\Data.cpp:8:0:
..\Data.h:11:7: note: forward declaration of 'class List'
 class List;
       ^
..\Data.cpp:25:17: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
  delete(listTwo);

现在,如果我删除Data.h中的前向声明并让Data.h包含List.h,我将得到:并导致循环继承循环

..\Data.h:25:2: error: 'List' does not name a type
  List *cpubursts;
  ^
..\Data.h:26:2: error: 'List' does not name a type
  List *iobursts;

我将如何修复此类设置?

编辑:List.h

#ifndef LIST_H_
#define LIST_H_

#include "Node.h"

namespace std {

class List {
private:
    Node *top;
public:
    List();
    virtual ~List();
    virtual Node* getTop();
    virtual void add(Node * linkedNode);
    virtual void remove(Node * linkedNode);
};

} /* namespace std */

#endif /* LIST_H_ */

Node.h

#ifndef NODE_H_
#define NODE_H_

#include "Data.h"

namespace std {

class Node {
public:
    Node();
    Node(Data * aItem);
    virtual ~Node();
    virtual Node* getNext();
    virtual void setNext(Node * linkedNode);
    virtual Data* getData();
    virtual void setData(Data* item);
private:
    Node *next;
    Data *item;
};

} /* namespace std */

#endif /* NODE_H_ */

2 个答案:

答案 0 :(得分:1)

您在List命名空间内定义std,并在此之外声明它。当你这样做时,你会说“在全局命名空间中有一个名为List的类”,当然没有。

您需要表达的是“List命名空间中有一个名为std的类”,可以按如下方式完成:

namespace std {
    class List;
}

class Data {
    /* your class */
};

我会问你为什么要在命名空间std中定义自己的类,但那就是重点。

答案 1 :(得分:0)

创建对象时,编译器必须知道该类,因此必须在Data.cpp中包含定义它的头。

请注意,建议不要使用raw new和delete,使用直接实例或使用smartpointers。您可以将std :: unique与forward声明一起使用,但是当编译器要为std :: unique_ptr的析构函数发出代码时,请确保已知完整的类定义。