我有五个文件。成功编译后,我收到链接错误。请注意:我不想在这里使用继承。
one.cpp
#include"one.hpp"
#include"two.hpp"
void one::method()
{
ptrTwoObj = new two;
ptrTwoObj->method();
}
one.hpp
class one{
public:
void method();
};
two.cpp
#include<iostream>
#include"two.hpp"
void two::method()
{
std::cout<<"In class two"<<std::endl;
}
two.hpp
class two{
public:
void method();
};
extern two *ptrTwoObj;
的main.cpp
#include<iostream>
#include"bsap.hpp"
int main()
{
one *ptrOneObj = new one;
ptrOneObj->method();
}
我使用以下命令
编译了上述文件
`g++ one.cpp two.cpp main.cpp`
以下是链接错误
`/tmp/ccwE53uC.o: In function `one::method()':
one.cpp:(.text+0x19): undefined reference to `ptrTwoObj'
one.cpp:(.text+0x20): undefined reference to `ptrTwoObj'
collect2: error: ld returned 1 exit status`