小编bah*_*har的帖子

使用自定义运算符<with std :: less时出错

我试图重载<运算符,但遇到问题.

这是我的实现:

int Vector3D::operator < (const Vector3D &vector)
{
   if(x<vector.x)
       return 1;
   else
       return 0;
}
Run Code Online (Sandbox Code Playgroud)

我用这段代码调用它:

std::map<Vector3D, std::vector<const NeighborTuple *> > position; 
std::set<Vector3D> pos; 
for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
{
    NeighborTuple const  &nb_tuple = *it;

    Vector exposition;
    pos.insert (exposition);
    position[exposition].push_back (&nb_tuple);
}
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

/usr/include/c++/4.1.2/bits/stl_function.h:在成员函数'bool std :: less <_Tp> :: operator()(const _Tp&,const _Tp&)const [with _Tp = ns3 :: Vector3D ]':
/usr/include/c++/4.1.2/bits/stl_map.h:347:从'_Tp&std :: map <_Key,_Tp,_Compare,_Alloc> :: operator [](const _Key&)[与...实例化_Key = ns3 :: Vector3D,_Tp = std …

c++

2
推荐指数
1
解决办法
1253
查看次数

多重定义

我的代码有一个关于不定义的错误operator<.我operator<通过重载它来纠正我的代码中的问题.当我编译它时,没有错误,但它有很多关于多重定义的错误.我的代码是:

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

以下是一些行:

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 …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
2160
查看次数

不知道类型(铸造)

我想在消息头中添加一个变量,所以我用它unit8_t来定义它们但是当我想要读取这个变量时我需要将它转换为double我做了:

hello.positionx = unit8_t (m_ipv4->GetObject<MobilityModel> ()->GetPosition ().x);   
hello.positiony = unit8_t (m_ipv4->GetObject<MobilityModel> ()->GetPosition ().y);   
Run Code Online (Sandbox Code Playgroud)

正如你可以看到这些行类似但是当我运行我的程序时它在第二行显示错误:unit8_t在此范围中未定义
我添加了标题:

 #include"stdint.h"  
Run Code Online (Sandbox Code Playgroud)

我不知道,这个错误是什么意思.如果你帮助我,我将感激不尽.

c++

0
推荐指数
1
解决办法
1285
查看次数

标签 统计

c++ ×3