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

Код Пожалуйста .... –

+0

, пожалуйста, разместите часть исходного кода, где вы определили функцию. – shuttle87

ответ

7

Похоже, вы определяете функцию в заголовочном файле, поэтому он получает определенный в каждом исходном файле, который включает его. Либо объявите его inline (который допускает несколько определений), либо переместите реализацию в исходный файл (так что он определен только один раз). EDIT: Или переместите определение внутри определения класса, которое также делает его встроенным. (Спасибо David.)

+2

+1 Еще один вариант - это определение метода внутри определения класса, который имеет тот же эффект, что и добавление 'inline' к определению метода за пределами определения класса. –

+0

большое спасибо. Это сработало, и моя программа в порядке. – bahar

Смежные вопросы