在主类之外声明多个嵌套类

时间:2015-11-16 08:15:29

标签: c++ class header

我有多个嵌套类,我想在单独的头文件中声明主类之外,因为嵌套类非常长。

这就是我想要做的事情:

class MainClass {
private:
        classA;
        classB;
        classC;
        //and so on
public:
        method(C.var) {//code}

问题是我不断获得Error:incomplete type not allowed.,因为这些课程没有被定义"。除了将所有代码放在MainClass

之外,我还能做些什么

真实代码:

class Graph //fix subclass issues
{
private:
    template <class TYPE> class * linkedList;
    template <class TYPE> class * chainingTable;
    class * minHeap;    
    class * vertex;
    class * edge;

    chainingTable<vertex*> vertexList;

    vertex * findVertex(int addrs) { return vertexList.findVertex(addrs); }
    int weight(vertex *v, vertex *u) { return v->weight + u->weight; }
    void relax(vertex * v, vertex * u,int i, minHeap h)
    {
        if(v->weight > u->weight+weight(v,u))
        {
            v->weight = v->weight + weight(v,u); 
            h.decreaseKey(i, v->weight);    //fixes heap
            v->predecessor = u;
        }
    }
public:
    void addVertex(int cost, int address) 
    { vertexList.insert(new vertex(cost, address)); } //and the class continues

现在有了这些变化,我得到了Error: expected either a definition or tag name

编辑:添加了所请求的实际代码,并添加了更新的错误

1 个答案:

答案 0 :(得分:1)

改为使用指向这些类的指针。

  function apply(func, thisArg, args) {
    switch (args.length) {
      case 0: return func.call(thisArg);
      case 1: return func.call(thisArg, args[0]);
      case 2: return func.call(thisArg, args[0], args[1]);
      case 3: return func.call(thisArg, args[0], args[1], args[2]);
    }
    return func.apply(thisArg, args);
  }

仅在.cpp文件中定义您的方法。

要了解发生了什么,在定义类时,编译器必须知道该类的对象有多大。如果您定义尚未完全定义的成员,则编译器没有关于这些类的大小的信息,因此它将无法计算此类的对象的大小。

但是,指针的大小始终相同(32位为4个字节,64位为8个字节),然后可以计算该类对象的大小。

ps:请注意,您不应在.h文件(头文件)中定义方法。头文件只应该有类的声明,而.cpp文件有它们的实现。

e.g: 部首:

apply()

具有实现的构造函数

classA *objectA;
classB *objectB;
classC *objectC;
相关问题