• 2 Classes point to each other?
    5 replies, posted
How do you have 2 classes point to each other? I have tried [B]ClassA.h[/B] [cpp]#pragma once #include "ClassB.h" class ClassA { ClassB* B; };[/cpp] [B]ClassB.h[/B] [cpp]#pragma once #include "ClassA.h" class ClassB { [U] ClassA* A;[/U] };[/cpp] But I am getting the error. [quote]Error 1 error C2146: syntax error : missing ';' before identifier 'A' 7[/quote] Because ClassB isn't seeing ClassA. Is this even possible in C++? I would combine them but 1 class is managed and the other is unmanaged and when combining them the unmanaged portion breaks. So I want ClassA (managed) to call a function in ClassB (unmanaged) and then ClassB will call a function in ClassA that will trigger an event.
forward declare one of the classes. [editline]07:12PM[/editline] [B]ClassB.h[/B] [cpp]#pragma once #include "ClassA.h" class ClassA; class ClassB { ClassA* B; };[/cpp]
Isn't this a no-no?
Error 1 error C2079: 'ClassB::A' uses undefined class 'ClassA' 8
[b]ClassA.hpp[/b] [cpp] #ifndef CLASS_A_HPP #define CLASS_A_HPP class ClassB; class ClassA { public: ClassB* B; }; #endif [/cpp] [b]ClassB.hpp[/b] [cpp] #ifndef CLASS_B_HPP #define CLASS_B_HPP #include "ClassA.hpp" class ClassB { public: ClassA* A; }; #endif [/cpp] [b]main.cpp[/b] [cpp] #include "ClassA.hpp" #include "ClassB.hpp" int main (int argc, char *argv[]) { ClassA* a = new ClassA; ClassB* b = new ClassB; a->B = b; b->A = a; delete a; delete b; return 0; } [/cpp] Is how it's done.
-snip-
Sorry, you need to Log In to post a reply to this thread.