2010-09-15 58 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