In C++, can you refer to a class before it is defined? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

In C++, can you refer to a class before it is defined?

In my code, I create a class which has a member that is an object of another class. However, that class is defined after the first one. So, for example, I would have in the definition of class Alpha a member which is of class Beta, but I define class Beta after class Alpha. Currently, I am getting an error "syntax error: identifier Alpha".

27th Nov 2023, 6:13 AM
Toby Ani
Toby Ani - avatar
1 Antwort
+ 2
You might put a forward declaration on top of Alpha: class Beta; https://gieseanw.wordpress.com/2018/02/25/the-joys-of-forward-declarations-results-from-the-real-world But you should also think about avoiding circular dependencies, if that is the actual case here. Not possible to tell without seeing your code. https://pvigier.github.io/2018/02/09/dependency-graph.html
27th Nov 2023, 6:41 AM
Tibor Santa
Tibor Santa - avatar