I\'m trying to access a function of a class(class2) that is declared after the c
ID: 3813339 • Letter: I
Question
I'm trying to access a function of a class(class2) that is declared after the class that i need to use it for(class1) but everytime I try to do it either with friend class and by declaring an object of the second class inside the first class the compiler gives errors since the second class is not being recognised due to only being declared after the line that im using to access it. The simplest way would be to have the function inside class1, but i need access to a vector of class2, which once again if i try to access it in class1 the compiler doesnt know it was declared afterwards(in class2). Either a solution to access the function or simply the vector would be good enough. Language is c++
Explanation / Answer
This can be solved using forward declaration.
Declare your second class before your class1 definition as mentioned below
#include <iostream>
#include<vector>
class class2;
class class1
{
public:
std::vector<class2> vect_class2;
};
class class2
{
};