English 中文(简体)
使用类参数编辑错误
原标题:compile error with class param
  • 时间:2012-05-22 23:49:20
  •  标签:
  • c++
  • g++

为什么我无法在 StateA 中制作 l-gt;set(新状态B); ? (此行在下面评论)

上面写道:

main.cpp: In member function ‘virtual void StateA::writeName(Lap*, char*)’:
main.cpp:19:4: error: invalid use of incomplete type ‘struct Lap’
main.cpp:3:7: error: forward declaration of ‘struct Lap’

但我不能解决这一点:()()

#include <stdio.h>

class Lap;
class State;
class StateB;
class StateA;

class State { public:
    virtual void writeName(Lap *l, char *str) = 0;
};
class StateB : public State { public:
    void writeName(Lap *l, char *str) {
        printf("%s B
", str);
    }
};
class StateA : public State { public:
    void writeName(Lap *l, char *str) {
        printf("%s A
", str);
        //l->set(new StateB);
    }
};
class Lap { public:
    State *ss;
    Lap(){
        printf("[Lap]
");
        set(new StateA);
    }

    void set(State *s){
        ss = s;
    }

    void writeName(char *str){
        ss->writeName(this, str);
    }
};

int main()
{
    printf("

");

    Lap lap;
    lap.writeName((char*)"Fulano");
    lap.writeName((char*)"Fulano");

    printf("

");
    return 0;
}
最佳回答

问题是,前期宣言

class Lap;

只告诉汇编者该类的存在, 并允许您向 < code> Lap 声明指针, 但它没有给汇编者足够的信息来处理任何 < code> Lap 方法调用 。

因此,在尝试使用该方法之前,您需要先声明 Lap < strong > > 。

您指定的代码无法在一个文件中完成, 因为 Lap does new StateA , 而 StateA 则在 Lap -- -- 循环依赖上使用一种方法。

您需要将至少一个( 更好的是, 全部) 的 < 坚固 > 声明 < / 坚固 > 移至页眉文件, 并在必要时包含页眉。 然后, 编译者将知道各个类的全部接口细节, 然后再在两个班级 的 < 坚固 > 定义 < / 坚固 > 尝试在其他班级使用方法之前 。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签