我的代码有一个关于不定义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 :(得分:7)
看起来您正在头文件中定义一个函数,因此它会在包含它的每个源文件中定义。声明它inline
(允许多个定义),或将实现移动到源文件中(因此它只定义一次)。 编辑:或者在类定义中移动定义,这也使其成为内联。 (谢谢大卫。)