2010-11-09 126 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) 
{ 
    ... 
} 

您給出的代碼只包含聲明,那麼定義在哪裏?

相關問題