我有這個文件:遞歸內聯函數
//Q2a.h
#ifndef Q2A_H
#define Q2A_H
inline int MyFactorial(int a)
{
if (a < 2)
return 1;
return a*MyFactorial(a-1);
}
int NumPermutations(int b);
#endif
//Q2a.cpp
#include "Q2a.h"
int NumPermutations(int b)
{
return MyFactorial(b);
}
and file with the main- Q2b.cpp
我注意到,編譯器通常會忽略當有遞歸函數內聯decleration。 但我的問題是爲什麼,如果我刪除inline聲明,我可以這樣做:
g++ -Wall -g -c Q2a.cpp -o Q2a.o
g++ -Wall -g -c Q2b.cpp -o Q2b.o
這些都很好,但在聯動階段:
g++ -Wall -g -c Q2a.o Q2b.o -o Q2
我得到一個錯誤:的'多重定義MyFactorial(int)
尾遞歸'inline'是合理的,但這不是這種情況。 – Potatoswatter 2010-09-05 21:06:10