2014-01-06 45 views
0

我提出了在報頭中的自己的類型:C++未定義參照XXX在功能XXX

typedef int OperationId; 

struct operation{ 
    OperationId         id; 
    JobId          assignedJob; 
    MachineType         machineType; 
    double          earning; 
    std::vector<operation>      operationPredecessors_; 
    std::vector<operation>::iterator   oPredecessorsIterator; 
    std::vector<operation>      operationSuccessors_; 
    std::vector<operation>::iterator   oSuccessorsIterator; 
    double          processingTime; 
    std::vector<Machine>      operationMachines_; 
    std::vector<Machine>::iterator    oMachinesIterator; 
}; 

它使用其他的自定義類型。 現在我自己做了一些函數來創建操作,因爲我不希望所有與每一個操作的時間寫很多代碼:

operation createOperation ( operation o, 
           OperationId id, 
           MachineType type, 
           double  earning, 
           double  processingTime); 

,並在在.cpp:

operation createOperation (operation  o, 
          OperationId id, 
          MachineType type, 
          double   earning, 
          double   processingTime){ 
    o.id    = id; 
    o.machineType  = type; 
    o.earning   = earning; 
    o.processingTime = processingTime; 
    return o; 
} 

現在我調用與cunction:

OperationId id; 
operation oSzero; 
id = 0; 
oSzero = createOperation(oSzero,id,INCOMING_GOODS,0,20); 

它給了我下面的錯誤:

undefined reference to Schedule::createOperation(operation, int, MachineType, double, double)' 

有人知道爲什麼嗎?我使用make作爲編譯器。它的功能完美無缺,並且:

oSzero.id = 0; 
oSzero.MachineType = INCOMING_GOODS; 
.... 

等主要直接。

+3

什麼是'Schedule'? – Jarod42

+2

順便提一下,'createOperation'可能是'operation'的構造函數(當你標記你的Q C++時)。 – Jarod42

+0

日程安排是一門課程。然而,我在類之外定義了操作,因爲我在其他類中也使用它,並且希望將它用於全局。然而,我在調用createOperation的函數是Schedule的一部分。 – aratai

回答

0

看來你做這樣的事情:

class Schedule { 
public: 
    operation createOperation(operation o, 
           OperationId id, 
           MachineType type, 
           double  earning, 
           double  processingTime); 
}; 

然後

operation createOperation(operation o, 
          OperationId id, 
          MachineType type, 
          double  earning, 
          double  processingTime) 
{ 
    /* code */ 
} 

所以,你有幾種選擇:

移動聲明createOperation以外安排的

Schedule::createOperation的實現方式如下:

operation Schedule::createOperation(operation o, 
          OperationId id, 
          MachineType type, 
          double  earning, 
          double  processingTime) 
{ 
    /* code */ 
} 

但我建議創建一個構造函數:

struct operation{ 
    operation(OperationId id, MachineType type, double earning, double processingTime); 
    // previous stuff 
}; 

operation::operation(OperationId id, MachineType type, double earning, double processingTime) : 
    id(id), 
    machineType(type), 
    earning(earning), 
    processingTime(processingTime) 
{ 
    // any other initialization 
} 
+0

謝謝!這解決了我的問題。我之前嘗試將它作爲構造函數來實現,這在我的代碼的其他區域中導致了其他問題。所以現在我在Schedule ::中正確實現了createOperation - 再次感謝您的答案! – aratai

相關問題