-3

在下面的代码中

map<nsaddr_t, Rating*>::iterator it;

为以下行生成错误

(it->second)->updateRating(alpha, beta, TRUST_FADING, 1);

编译过程中出现“未定义引用”的错误。

语法是否正确或需要其他任何东西。

我没有在整个代码中找到 second 的声明。

void TrustManager::updateTrust(nsaddr_t address, double alpha, double beta)
{
    map<nsaddr_t, Rating*>::iterator it;
    it = trust_t.find(address);
    if (it == trust_t.end())
    {
        trust_t[address] = initNewRating(alpha, beta, 1);
    }
    else
    {
        (it->second)->updateRating(alpha, beta, TRUST_FADING, 1);
    }
}
bool TrustManager::isTrustworthy(nsaddr_t from)
{
    map<nsaddr_t, Rating*>::iterator it;
    it = trust_t.find(from);
    if (it != trust_t.end())
    {
        double alpha = (it->second)->getAlpha();
        double beta = (it->second)->getBeta();
        double dev = alpha/(alpha+beta);
        return (dev >= UNTRUST_TOLERANCE) ? false:true;
    }
    else
        return true;
}

完整的错误是

In function `TrustManager::updateTrust(int, double, double)':
:(.text+0xde): undefined reference to `Rating::updateRating(double, double, double, double)'


In function `TrustManager::handleInactivityTimeout()':

(.text+0x241): undefined reference to `Rating::updateRating(double, double, double, double)'
In function `TrustManager::initNewRating(double, double, double)':

trustmanager.cc:(.text+0x306): undefined reference to `Rating::updateRating(double, double, double, double)'

In function `DSRAgent::DSRAgent()':

(.text+0x5b7): undefined reference to `makeRouteCache()'

In function `RouteCache::pre_noticeRouteUsed(Path const&, Path&, double, ID const&)':

(.text+0x54b): undefined reference to `cache_ignore_hints'

routecache.cc:(.text+0x620): undefined reference to `cache_use_overheard_routes'

dsr/routecache.o: In function `RouteCacheClass::create(int, char const* const*)':

(.text._ZN15RouteCacheClass6createEiPKPKc[RouteCacheClass::create(int, char const* 

const*)]+0x7): undefined reference to `makeRouteCache()'

collect2: ld returned 1 exit status

评级定义如下:

class Rating
{
public:
    Rating() { alpha = 1.0; beta = 1.0;}
    Rating(double a, double b) { alpha = a; beta = b; last_t =
        Scheduler::instance().clock();}
    void updateRating(double a, double b, double fading, double weight);
    inline double getAlpha() { return alpha; }
    inline double getBeta() { return beta; }
    inline Time getTime() { return last_t; }
private:
    //the rating of misbehavior
    double alpha;
    //the rating of good behavior
    double beta;
    //last updated time
    Time last_t;
};
class PackData {
public:
    PackData();
    PackData(Packet* packet, Time t);
    Packet* packet;
    Time t;
};
4

1 回答 1

2

错误信息非常准确:

In function `TrustManager::updateTrust(int, double, double)':
:(.text+0xde): undefined reference to `Rating::updateRating(double, double, double, double)'

您正在updateRating()从inside 调用该函数updateTrust(),但您没有在任何地方定义该函数 - 至少不是链接器可以找到它的位置。

大量相同类型的后续错误提示未正确添加到编译器/链接器命令行的翻译单元。updateRating(), makeRouteCache(),cache_ignore_hints等等等等 - 所有这些都必须在某个地方定义。

评级定义如下:

那是 的声明updateRating()而不是定义。请注意,该标头中没有函数体 ( { ... })。

(此外,最好不要在头文件中提供函数体(或其他定义),而是将它们保存在相应的 .cpp 文件中。一方面,它使头文件更易于阅读,即有点自记录.)

于 2013-04-15T11:00:04.120 回答