2010-09-15 59 views
0

我的代碼有一個關於未定義operator<的錯誤。我通過重載它來糾正了我的代碼中的operator<問題。當我編譯它時,那裏沒有錯誤,但是它有很多關於多重定義的錯誤。我的代碼是:多重定義

int Vector3D::operator < (const Vector 3D &vector) const 
{ 
if(x<vector.x) 
    return 1; 
else 
    return 0; 
} 

下面是一些行:

debug/src/common/propagation-delay-model_1.o: In function `empty': 
/usr/include/c++/4.1.2/limits:1044: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/common/propagation-loss-model_1.o: In function `empty': 
/usr/include/c++/4.1.2/new:94: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/common/jakes-propagation-loss-model_1.o: In function `empty': 
/usr/include/c++/4.1.2/new:94: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/common/cost231-propagation-loss-model_1.o: In function `empty': 
/usr/include/c++/4.1.2/limits:1044: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/common/spectrum-propagation-loss-model_1.o: In function `~BandInfo': 
/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/debug/ns3/type-id.h:392: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/common/friis-spectrum-propagation-loss_1.o: In function `~BandInfo': 
/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/debug/ns3/vector.h:118: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/node/spectrum-phy_1.o: In function `~TypeId': 
/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/debug/ns3/type-id.h:392: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/internet-stack/ipv6-l3-protocol_1.o: In function `new_allocator': 
/usr/include/c++/4.1.2/new:94: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/routing/olsr/olsr-routing-protocol_1.o: In function `~Association': 
/usr/include/c++/4.1.2/new:94: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
debug/src/core/vector_1.o:/home/bahar/Desktop/ns/ns-allinone-3.9/ns-allinone-3.9/ns-3.9/build/../src/core/vector.h:118: first defined here 
debug/src/routing/olsr/test/bug780-test_1.o: In function `new_allocator': 
/usr/include/c++/4.1.2/new:94: multiple definition of `ns3::Vector3D::operator<(ns3::Vector3D const&) const' 
+0

代碼請.... – 2010-09-15 17:32:11

+0

請張貼您定義函數的一些源代碼。 – shuttle87 2010-09-15 17:32:38

回答

7

看起來你是在頭文件中定義的函數,因此它得到了包括它的每一個源文件中定義。要麼聲明它inline(它允許多個定義),要麼將實現移動到源文件中(所以它只定義一次)。 編輯:或者移動類定義中的定義,這也使它內聯。 (謝謝大衛。)

+2

+1另一種選擇實際上是在類定義中定義方法,這與在類定義之外向方法定義添加「inline」具有相同的效果。 – 2010-09-15 17:51:53

+0

非常感謝。它的工作和我的程序現在好了。 – bahar 2010-09-16 04:34:47