C ++函数XXX中对XXX的未定义引用

时间:2014-01-06 10:44:37

标签: c++ types reference makefile

我在标题中设置了一个自己的类型:

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

现在我打电话给:

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;
....

等。在主要直接。

1 个答案:

答案 0 :(得分: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

之外

或执行Schedule::createOperation as:

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
}