向 NS2 添加向量;编译器错误

Adding vector to NS2; Compiler error

本文关键字:编译器 错误 向量 NS2 添加      更新时间:2023-10-16

我试图将# include <vector>添加到NS2模拟器文件中,但我遇到了问题。我收到此错误(我使用了另一台装有g ++ 4.6的计算机,而不是旧的g ++ 4.4)。为什么只包含<vector>会导致错误?

`g++ -c -Wall -Wno-write-strings  -DTCP_DELAY_BIND_ALL -DNO_TK -DTCLCL_CLASSINSTVAR  -DNDEBUG -DLINUX_TCP_HEADER -DUSE_SHM -DHAVE_LIBTCLCL -DHAVE_TCLCL_H -DHAVE_LIBOTCL1_14 -DHAVE_OTCL_H -DHAVE_LIBTK8_5 -DHAVE_TK_H -DHAVE_LIBTCL8_5 -DHAVE_TCLINT_H -DHAVE_TCL_H  -DHAVE_CONFIG_H -DNS_DIFFUSION -DSMAC_NO_SYNC -DCPP_NAMESPACE=std -DUSE_SINGLE_ADDRESS_SPACE -Drng_test  -I. -I. -I/home/ns235/ns-allinone-2.35/tclcl-1.20 -I/home/ns235/ns-allinone-2.35/otcl -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/usr/include/pcap -I./tcp -I./sctp -I./common -I./link -I./queue -I./adc -I./apps -I./mac -I./mobile -I./trace -I./routing -I./tools -I./classifier -I./mcast -I./diffusion3/lib/main -I./diffusion3/lib -I./diffusion3/lib/nr -I./diffusion3/ns -I./diffusion3/filter_core -I./asim/ -I./qs -I./diffserv -I./satellite -I./wpan -o tcp/tcp-fack.o tcp/tcp-fack.cc
In file included from /usr/include/c++/4.6/vector:61:0,
             from tcp/tcp-fack.cc:28:
 /usr/include/c++/4.6/bits/stl_algobase.h: In function ‘const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = TracedInt]:
tcp/tcp-fack.cc:87:37:   instantiated from here
  /usr/include/c++/4.6/bits/stl_algobase.h:215:7: error: no match for ‘operator<’ in ‘__a  < __b’
 /usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: candidates are:
 /usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: operator<(int, int) <built-in>
 /usr/include/c++/4.6/bits/stl_algobase.h:215:7: note:   no known conversion for argument 2 from ‘const TracedInt’ to ‘int’
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
 /usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
 /usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const   std::reverse_iterator<_IteratorR>&)
 /usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc>   bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
make: *** [tcp/tcp-fack.o] Error 1

以下是 stl_algobase.h 行 (197-218) 的片段。 在 tcp-fack.cc 中使用templete是否会导致错误?(http://www.cs.rit.edu/usr/local/pub/swm/gcc-4.7.2/include/c++/4.7.2/bits/stl_algobase.h)

/**
*  @brief This does what you think it does.
*  @ingroup sorting_algorithms
*  @param  __a  A thing of arbitrary type.
*  @param  __b  Another thing of arbitrary type.
*  @return   The greater of the parameters.
*
*  This is the simple classic generic implementation.  It will work on
*  temporary expressions, since they are only evaluated once, unlike a
*  preprocessor macro.
*/
template<typename _Tp>
inline const _Tp&
max(const _Tp& __a, const _Tp& __b)
{
  // concept requirements
  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  //return  __a < __b ? __b : __a;
  if (__a < __b)
return __b;
  return __a;
}

以下是 tcp-fack.cc 片段。这是带有行号 https://github.com/hbatmit/ns2.35/blob/master/tcp/tcp-fack.cc 的原始文件(我只在第 28 行添加了<vector>

#ifndef lint
static const char rcsid[] =
"@(#) /home/ctk21/cvsroot//hssstcp/ns/ns-2.1b9/tcp/tcp-fack.cc,v 1.2 2002/08/12 10:43:58     ctk21 Exp (PSC)";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <vector>   //line 28
#include "ip.h"
#include "tcp.h"
#include "flags.h"
#include "scoreboard.h"
#include "random.h"
#include "tcp-fack.h"
#include "template.h"
static class FackTcpClass : public TclClass {
public:
FackTcpClass() : TclClass("Agent/TCP/Fack") {}
TclObject* create(int, const char*const*) {
    return (new FackTcpAgent());
    }
 } class_fack;

/*
 * Process a dupack.
 */
void FackTcpAgent::oldack(Packet* pkt)
{
hdr_tcp *tcph = hdr_tcp::access(pkt);
last_ack_ = tcph->seqno();
highest_ack_ = last_ack_;
fack_ = max(fack_,highest_ack_);
/* 
 * There are conditions under which certain versions of TCP (e.g., tcp-fs)
 * retract maxseq_. The following line of code helps in those cases. For
 * versions of TCP, it is a NOP.
     */
maxseq_ = max(maxseq_, highest_ack_);// This is line 87
if (t_seqno_ < last_ack_ + 1)
    t_seqno_ = last_ack_ + 1; // This is line 89 
newtimer(pkt);
if (rtt_active_ && tcph->seqno() >= rtt_seq_) { 
    rtt_active_ = 0;
    t_backoff_ = 1;
}
/* with timestamp option */
double tao = Scheduler::instance().clock() - tcph->ts_echo();
rtt_update(tao);
if (ts_resetRTO_) {
    // From Andrei Gurtov
    // FACK has not been updated to make sure the ECN works
    //   correctly in this case - Sally.
    t_backoff_ = 1;
}
/* update average window */
awnd_ *= 1.0 - wnd_th_;
awnd_ += wnd_th_ * cwnd_;
/* if the connection is done, call finish() */
if ((highest_ack_ >= curseq_-1) && !closed_) {
    closed_ = 1;
    finish();
}
}
int FackTcpAgent::maxsack(Packet *pkt)
{
hdr_tcp *tcph = hdr_tcp::access(pkt);
int maxsack=-1, sack_index; 
for (sack_index=0; sack_index < tcph->sa_length(); sack_index++) {
    if (tcph->sa_right(sack_index) > maxsack)
        maxsack = tcph->sa_right(sack_index);
}
return (maxsack-1);
}

** 我已将<vector>替换为<map>,它显示以下错误**

g++ -c -Wall -Wno-write-strings  -DTCP_DELAY_BIND_ALL -DNO_TK -DTCLCL_CLASSINSTVAR  -DNDEBUG -DLINUX_TCP_HEADER -DUSE_SHM -DHAVE_LIBTCLCL -DHAVE_TCLCL_H -DHAVE_LIBOTCL1_14 -DHAVE_OTCL_H -DHAVE_LIBTK8_5 -DHAVE_TK_H -DHAVE_LIBTCL8_5 -DHAVE_TCLINT_H -DHAVE_TCL_H  -DHAVE_CONFIG_H -DNS_DIFFUSION -DSMAC_NO_SYNC -DCPP_NAMESPACE=std -DUSE_SINGLE_ADDRESS_SPACE -Drng_test  -I. -I. -I/home/ns235/ns-allinone-2.35/tclcl-1.20 -I/home/ns235/ns-allinone-2.35/otcl -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/home/ns235/ns-allinone-2.35/include -I/usr/include/pcap -I./tcp -I./sctp -I./common -I./link -I./queue -I./adc -I./apps -I./mac -I./mobile -I./trace -I./routing -I./tools -I./classifier -I./mcast -I./diffusion3/lib/main -I./diffusion3/lib -I./diffusion3/lib/nr -I./diffusion3/ns -I./diffusion3/filter_core -I./asim/ -I./qs -I./diffserv -I./satellite -I./wpan -o tcp/tcp-fack.o tcp/tcp-fack.cc
In file included from /usr/include/c++/4.6/bits/stl_tree.h:63:0,
             from /usr/include/c++/4.6/map:60,
             from tcp/tcp-fack.cc:28:
/usr/include/c++/4.6/bits/stl_algobase.h: In function ‘const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = TracedInt]’:
tcp/tcp-fack.cc:87:37:   instantiated from here
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: error: no match for ‘operator<’ in ‘__a < __b’
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: candidates are:
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: operator<(int, int) <built-in>
/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note:   no known conversion for argument 2 from ‘const TracedInt’ to ‘int’
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool  std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
make: *** [tcp/tcp-fack.o] Error 1

我找到了解决方案;NS-2 template.h 具有 swap() 和 max() 函数。swap() 是标准 <vector> 的成员函数。而 max() 是标准 stl_algorithm() 的成员函数。NS-2 god.h 也有一个名为 vector() 的类。这些 NS-2 函数导致与<Vector>库和<algorithm>库发生冲突。

我更改了 NS2 功能的名称,现在一切正常。

谢谢