請幫忙解決關於C++和g ++編譯和鏈接的noob問題。基本上,我有2個不同的文件中的2個類,並且可以編譯它們,但是當我嘗試鏈接時,一個類無法看到另一個類的方法,即使我正在鏈接它們。目標文件的順序在這種情況下不起作用。g ++ undefined參考非常簡單的例子
該問題似乎與採用參數的非默認構造函數有關。
我已經蒸餾,轉載於下面簡單的代碼的問題:
文件:a.cpp:
#include <iostream>
class A
{
public:
int my_int;
A(int i) {
my_int = i;
std::cout << "A";
}
};
文件:a.hpp:
#ifndef __A_H_
#define __A_H_
class A
{
public:
A(int i);
};
#endif
文件b.cpp:
#include <iostream>
using namespace std;
#include <a.hpp>
class B
{
public:
int my_int;
B(int i) {
my_int = i;
A a(i);
cout << "B\n";
}
};
int main(int argc, char* argv[])
{
B b(5);
cout << "hello world: ";
cout.flush();
return 0;
}
命令我用它來構建:
g++ -c -I. a.cpp
g++ -c -I. b.cpp
g++ -o c_test a.o b.o
或者,我已經嘗試每一種:
g++ -o c_test b.o a.o
g++ -I. -o c_test a.cpp b.cpp
g++ -I. -o c_test b.cpp a.cpp
錯誤,我得到以上任何環節場景:
b.o: In function `B::B(int)':
b.cpp:(.text._ZN1BC1Ei[B::B(int)]+0x1c): undefined reference to `A::A(int)'
collect2: ld returned 1 exit status
在此先感謝您的任何見解。
(對不起,如果這是一個重職 - 我想我張貼,並沒有看到它...)