2010-11-09 128 views
1

编译器错误令我非常困惑。我的代码在4-5小时前彻底工作;沿途唯一可行的检查点并未产生任何线索(即,我无法在一个中间步骤中使错误消失)。我看不出编译器错误如何与我所做的任何更改相关联。C++编译器错误:ld:找不到从_main引用的成员函数的符号

g++ -O3 -o a.out -I /Applications/boost_1_42_0/ Host.cpp Simulation.cpp main.cpp Rdraws.cpp SimPars.cpp

编译下面的错误出现

Undefined symbols: 
    "Simulation::runTestEpidSim()", referenced from: 
     _main in ccmcSY5M.o 
ld: symbol(s) not found 
collect2: ld returned 1 exit status 

我创建和操纵main对象Simulation。我对代码的唯一更改是:(1)创建一个新的成员函数Simulation::runTestEpidSim(),在main中调用该函数;(2)编写一些新的全局输入/输出处理函数,这些函数已经解包并直接插入到main为了调试正在发生的事情。

我没有更改任何cpp文件,包括头文件,库或编译器命令。

我不是专业程序员。专业人员将如何去调试这类问题?

不知道如何最好地剪切和粘贴我的代码,但在这里是一个excerpt--

class Simulation 
{ 
public: 
    Simulation(int trt, int sid, SimPars * spPtr ); 
    ~Simulation(); 

    // MEMBER FUNCTION PROTOTYPES 
    void runDemSim(void); 
    void runEpidSim(void); 
    double runTestEpidSim(void); 
... 
} 

int main() { 
for (int trt = 0; trt < NUM_TREATMENTS; trt++) { 

    double treatment = TREATMENTS[ trt ]; 
    .... 
    cout << "Treatment #" << trt + 1 << " of " << NUM_TREATMENTS << ":" << endl; 
    int matchAttempts = 0; 
    double prevError = 1.0 + PREV_ERROR_THOLD; 
    double thisBeta = 0.0; 
    while (matchAttempts < MAX_MATCH_ATTEMPTS && prevError > PREV_ERROR_THOLD) { 
    cout << " Attempt #" << matchAttempts + 1; 
    SimPars thesePars; 
    SimPars * spPtr = &thesePars; 
    Simulation thisSim(trt, 1, spPtr); 
    thisSim.runDemSim(); 
    prevError = thisSim.runTestEpidSim() - TARGET_PREV; 
    cout << ", error=" << prevError << endl; 
    if (prevError > PREV_ERROR_THOLD) { 
    .... 

return 0; 
} 

我以前一直没有问题,执行runEpidSim()

更新 我有完整代码的Simulation::runTestEpidSim() --I执行不知道如何最好地呈现这个!

double Simulation::runTestEpidSim(void) { 
    if (allHosts.size() == 0) { 
    cerr << "No hosts remaining for epidemiological simulation. Cancelling." << endl; 
    assert(false); 
    } 
    cout << " Entering test simulation at t=" << demComplete << "." << endl; 
    double percentDone = 0.0; 

    // Initialize host population with infections 
    demOutputStrobe = t; 
    epidOutputStrobe = t; 
    seedInfections(); 
    EventPQ::iterator eventIter = currentEvents.begin(); 
    double nextTimeStep = t + EPID_DELTA_T; 
    double prevalences[ NUM_TEST_SAMPLES ]; // holds prevalences at strobing periods 
    for (int p = 0; p < NUM_TEST_SAMPLES; p++) { 
    prevalences[ p ] = 0.0; 
    } 
    double prevYear = DEM_SIM_LENGTH + TEST_EPID_SIM_LENGTH - NUM_TEST_SAMPLES; // first simulation year to start sampling 
    int prevSamples = 0; 

    while (t < TEST_EPID_SIM_LENGTH + demComplete) 
    { 

#ifdef DEBUG 
     cout << "time step = " << t << " (" << allHosts.size() << " hosts; " << currentEvents.size() << " events queued)" << endl; 
     assert(currentEvents.size()>0); 
#endif 

     // Calculate new infections for every host and add events to stack 
#ifdef DEBUG 
     cout << "Adding infections for this time step: " << endl; 
#endif 
     calcSI(); 
     eventIter = currentEvents.begin(); 

#ifdef DEBUG 
     cout << "Executing events off stack (currentEvents.size()=" << currentEvents.size() << "): " << endl; 
#endif  

     while ((*eventIter).time < nextTimeStep) { 

    while (demOutputStrobe < t) { 
     writeDemOutput(); 
     demOutputStrobe += STROBE_DEM; 
    } 
    while (epidOutputStrobe < t) { 
     writeEpidOutput(); 
     epidOutputStrobe += STROBE_EPID; 
    } 
    if (prevYear < t) { 
     prevalences[ prevSample ] = calcPrev(); 
     cout << "\tOutputting prevalence sample #" << prevSamples+1 << "; prevalence under 5 is " << prevalences[ prevSample ] << endl; 
     prevSample++; 
    } 
     while (percentDone/100.0 < (t - demComplete)/EPID_SIM_LENGTH ) { 
     cout << "\t" << percentDone << "% of this test component complete." << endl; 
     percentDone += PROGRESS_INTERVAL; 
     } 

    // Execute events off stack 
    Event thisEvent = *eventIter; 
#ifdef DEBUG 
    assert(thisEvent.time >= t); 
    if (thisEvent.time < t) { 
     cout << "Trying to execute event scheduled for time " << thisEvent.time << ", though sim time is " << t << endl; 
     assert(thisEvent.time >= t); 
    } 
#endif 
    t = thisEvent.time; 


#ifdef DEBUG 
    cout << "\tt=" << t << endl; 
    // cout << "\tAbout to execute event ID " << (*eventIter).eventID << " at time " << (*eventIter).time << " for host ID " << (*eventIter).hostID << endl; 
    cout << "\tAbout to execute event ID " << thisEvent.eventID << " at time " << thisEvent.time << " for host ID " << thisEvent.hostID << endl; 
#endif 
    executeEvent(thisEvent); 
    eventCtr++; 
    currentEvents.erase(eventIter); // Check that if event added to top of currentEvents, will not invalidate itr 
    eventIter = currentEvents.begin(); 

#ifdef DEBUG 
    cout << "\tcurrentEvents.size() after pop is " << currentEvents.size() << endl; 
#endif 

     } 

     t = nextTimeStep; 
     nextTimeStep += EPID_DELTA_T; 
    } 

    double meanPrev = 0.0; 
    double sumPrev = 0.0; 
    int totSamples = 0; 
    for ( int p = 0; p < NUM_TEST_SAMPLES; p++) { 
    if (prevalences[ p ] > 0) { // in cae 
     sumPrev += prevalences[ p ]; 
     totSamples++; 
    } 
    } 
    cout << "Counted " << totSamples << " total prevalence samples." << endl; 
    meanPrev = sumPrev/(double)totSamples; 
    return(meanPrev); 
} 
+1

模拟方法的实现(或主体)在哪里? – 2010-11-09 20:21:56

回答

5

How would the pros go about debugging this kind of problem?

你应该和错误消息开始:

Undefined symbols:"Simulation::runTestEpidSim()" 

您已经添加了runTestEpidSim一个雏形,但是你有没有在该功能(大概应该是在模拟的定义。CPP)

这里就是我想要找什么,开始...

  • 是否定义它呢?
  • 您是否将其定义为类Simulation的成员?
  • 你是否用完全相同的参数定义它?
  • 你是否用完全相同的返回类型定义了它?
  • 定义是否被意外评论或#ifdef'd out?

更新 感谢张贴的定义。什么你张贴看起来不错,并通过上面的第一个四个测试 - 但你可以看到一个不匹配的#ifdef DEBUG可能搞乱了你...

此外...

  • 你真的编译并链接到包含该定义的文件吗?
+1

把我的头埋在耻辱中 - 我以为我已经检查了这个,但是我已经确定了#如果已将其删除。感谢你描述你如何解决这个问题。我会尽快接受你的回答。 – Sarah 2010-11-09 20:33:02

+0

+1意外的'#ifdef'。甚至没有想过一次,似乎是正确的解决方案。 – Dirk 2010-11-09 20:45:07

1

链接器错误消息说,你不必为运行* 测试定义* EpidSim,你需要为你调用内部主此功能。你的描述符合这个问题:你已经声明了这个新的方法,但是你没有在任何地方实现它。您需要提供此测试功能的实际源代码。

1

看起来你声明的功能,但没有实现它

1

你在哪里定义runTestEpidSim?您是否在包含main的文件中使用正确的签名来定义它?如果它是在其他文件中定义的:您是否在构建过程中包含该文件?

double 
Simulation::runTestEpidSim(void) 
{ 
    ... 
} 

您给出的代码只包含声明,那么定义在哪里?

相关问题