2010-11-08 79 views
0

我在因特網上找到了一個arraylist類,在http://code.google.com/p/arraylist/downloads/detail?name=arraylist-v1.zipArrayList C++問題

在使用MS Visual C++的Windows上編譯好,在OS X下使用GCC我會得到大量的錯誤。這裏的錯誤輸出:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf 
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/consolerpg 
mkdir -p build/Debug/GNU-MacOSX 
rm -f build/Debug/GNU-MacOSX/arraylist.operators.o.d 
g++ -fpermissive -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/arraylist.operators.o.d -o build/Debug/GNU-MacOSX/arraylist.operators.o arraylist.operators.cpp 
arraylist.operators.cpp:86: error: redefinition of 'datatype& arraylist<datatype>::operator[](int)' 
arraylist.operators.cpp:86: error: 'datatype& arraylist<datatype>::operator[](int)' previously declared here 
arraylist.operators.cpp:100: error: redefinition of 'const datatype& arraylist<datatype>::operator[](int) const' 
arraylist.operators.cpp:100: error: 'const datatype& arraylist<datatype>::operator[](int) const' previously declared here 
arraylist.operators.cpp:128: error: redefinition of 'const arraylist<datatype>& arraylist<datatype>::operator=(const arraylist<datatype>&)' 
arraylist.operators.cpp:128: error: 'const arraylist<datatype>& arraylist<datatype>::operator=(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:157: error: redefinition of 'arraylist<datatype> arraylist<datatype>::operator+(const arraylist<datatype>&)' 
arraylist.operators.cpp:157: error: 'arraylist<datatype> arraylist<datatype>::operator+(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:186: error: redefinition of 'const arraylist<datatype>& arraylist<datatype>::operator+=(const arraylist<datatype>&)' 
arraylist.operators.cpp:186: error: 'const arraylist<datatype>& arraylist<datatype>::operator+=(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:201: error: redefinition of 'arraylist<datatype> arraylist<datatype>::operator-(const datatype&)' 
arraylist.operators.cpp:201: error: 'arraylist<datatype> arraylist<datatype>::operator-(const datatype&)' previously declared here 
arraylist.operators.cpp:223: error: redefinition of 'const arraylist<datatype>& arraylist<datatype>::operator-=(const datatype&)' 
arraylist.operators.cpp:223: error: 'const arraylist<datatype>& arraylist<datatype>::operator-=(const datatype&)' previously declared here 
arraylist.operators.cpp:256: error: redefinition of 'arraylist<datatype> arraylist<datatype>::operator*(const int&)' 
arraylist.operators.cpp:256: error: 'arraylist<datatype> arraylist<datatype>::operator*(const int&)' previously declared here 
arraylist.operators.cpp:280: error: redefinition of 'const arraylist<datatype>& arraylist<datatype>::operator*=(const int&)' 
arraylist.operators.cpp:280: error: 'const arraylist<datatype>& arraylist<datatype>::operator*=(const int&)' previously declared here 
arraylist.operators.cpp:322: error: redefinition of 'arraylist<datatype> arraylist<datatype>::operator/(const int&)' 
arraylist.operators.cpp:322: error: 'arraylist<datatype> arraylist<datatype>::operator/(const int&)' previously declared here 
arraylist.operators.cpp:347: error: redefinition of 'const arraylist<datatype>& arraylist<datatype>::operator/=(const int&)' 
arraylist.operators.cpp:347: error: 'const arraylist<datatype>& arraylist<datatype>::operator/=(const int&)' previously declared here 
arraylist.operators.cpp:367: error: redefinition of 'bool arraylist<datatype>::operator==(const arraylist<datatype>&)' 
arraylist.operators.cpp:367: error: 'bool arraylist<datatype>::operator==(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:394: error: redefinition of 'bool arraylist<datatype>::operator!=(const arraylist<datatype>&)' 
arraylist.operators.cpp:394: error: 'bool arraylist<datatype>::operator!=(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:418: error: redefinition of 'bool arraylist<datatype>::operator>(const arraylist<datatype>&)' 
arraylist.operators.cpp:418: error: 'bool arraylist<datatype>::operator>(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:425: error: redefinition of 'bool arraylist<datatype>::operator<(const arraylist<datatype>&)' 
arraylist.operators.cpp:425: error: 'bool arraylist<datatype>::operator<(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:432: error: redefinition of 'bool arraylist<datatype>::operator>=(const arraylist<datatype>&)' 
arraylist.operators.cpp:432: error: 'bool arraylist<datatype>::operator>=(const arraylist<datatype>&)' previously declared here 
arraylist.operators.cpp:438: error: redefinition of 'bool arraylist<datatype>::operator<=(const arraylist<datatype>&)' 
arraylist.operators.cpp:438: error: 'bool arraylist<datatype>::operator<=(const arraylist<datatype>&)' previously declared here 
make[2]: *** [build/Debug/GNU-MacOSX/arraylist.operators.o] Error 1 
make[1]: *** [.build-conf] Error 2 
make: *** [.build-impl] Error 2 

BUILD FAILED (exit value 2, total time: 427ms) 

是否有某種VC++兼容性標誌的,甚至更好,你能提供一個樣本修正,這樣我就可以糾正他們呢?

謝謝!

Yvan

+0

你可以發佈你的makefile嗎? - 或者只是檢查Steve M的回答 - 我認爲他已經知道了。 – 2010-11-08 17:12:52

回答

2

沒有兼容性標誌。你看過STL list嗎?它已經實現了visual C++和gcc,並可以做你想做的。

+0

它怎麼叫? – friedkiwi 2010-11-08 18:12:28

+0

哎呀:)。找到它;)對此評論抱歉。 – friedkiwi 2010-11-08 18:12:57

5

我的猜測是,即使arraylist.h包含它們,你也在編譯cpp文件。讓我們來看看代碼...

// arraylist.operators.cpp 

#ifndef __arraylist_CLASS__ 
#include "arraylist.h" 
#endif 

,然後我們有

// arraylist.h 

#include "arraylist.cpp" 
#include "arraylist.operators.cpp" 

所以,如果你編譯arraylist.operators.cpp,它包括arraylist.h,其中包括arraylist.operators .cpp ...

這個班級看起來像一塊垃圾,只是使用std::vector

+0

*永遠不會*包含頭文件中的.cpp文件,但我知道原始代碼已從網上覆制過來。如果您必須包含某些內容但希望將其標記爲非公開內容,則只需以不同的名稱命名,如blah_detail.hpp,_blah.inc等。 – 2010-11-08 17:42:14