gpt4 book ai didi

c++ - 多重定义

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:08 25 4
gpt4 key购买 nike

我的代码有一个关于未定义 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'

最佳答案

看起来你在头文件中定义了一个函数,所以它在包含它的每个源文件中都被定义了。要么将其声明为 inline(允许多个定义),要么将实现移到源文件中(因此它只定义一次)。 编辑:或将定义移动到类定义内,这也使其内联。 (感谢大卫。)

关于c++ - 多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3720155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com