2013-04-15 66 views
-3

在下面的代码未定义refernce到

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

产生错误以下行

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

的“未定义提及”编译期间的误差。

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

我没有在整个代码中找到第二个声明。

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; 
}; 
+1

“未定义的引用”是一个链接器错误,而不是编译器错误。似乎你忘了定义一些你提供了声明的函数 –

+3

你能发布完整的错误吗? –

+0

没有成员在评级中名为second – user2214138

回答

2

的错误信息是非常准确:

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

您呼叫的功能updateRating()updateTrust()以内,但是你没有在任何地方定义函数 - 至少,不是链接器能找到它的地方。

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

该评级是定义如下:

即一个声明的updateRating(),而不是一个定义。请注意,该标题中没有功能主体({ ... })。 (另外,而不是在头文件中提供了函数体(或其他定义),而是保留在相应的.cpp文件中是一种很好的做法。首先,它使头文件更容易阅读,即有点自我记录。)

+0

updateRating是在更新信任 – user2214138

+0

下的C++文件中调用的头文件中定义的,它似乎(it-> second)这个东西正在产生错误。 – user2214138

+1

@ user2214138:这是*声明*,而不是*定义*。你告诉编译器在某处会有一个函数'updateRating()'(或者编译器会给你一个错误),但是你不会*定义*那个函数实际上*做了什么*。因此,编译器编译代码时没有错误,希望链接器能够在其他地方找到定义 - 但它不能,因此导致“undefined reference”错误。 – DevSolar