#include <iostream>
#include <stdlib.h>
class B;
class A {
int x;
public:
B it();
friend class B;
};
class B {
int y;
A a;
public:
B(A aa):y(99), a(aa) {};
int get_y() {
return this -> y;
}
};
using namespace std;
int main() {
A md;
cout << md.it().get_y() << endl;
return 0;
}
Ignore encapsulation and other details, my problem is that I want to use "md.it().get_y()", but my compiler gives me this linker error: Undefined symbols for architecture x86_64: "A::it()", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64. How can I solve this ?
EDIT2: After realizing that
B it();
is NOT a function returning B, but an object inside class A: You have a cyclic depenency, where A uses B and B uses A. The compiler is unable to resolve this. Forward declares like
class N;
ONLY work for pointers or references, where the actual object size does not have to be known
EDIT: I just realized I misread the error message. You are missing the function definition for the it() method.
The forward declare propably does not work for
B it();
changing the code to
B& it();
should fix your issue
Note that B is now returned by refernce instead of by value. So you might actually do something like
const B& it() const;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.