1

Sorry if this question maybe asked before but I couldn't find a proper answer for it. I am trying to use my own class file inside another class which was already written by different people. The whole program is based on Omnet++. Before building everything seems to be alright but as soon as I build the program gives me an error of

undefined reference to `RatePrediction::RatePrediction(double, double, double)

I am trying to use RatePrediction in CentralEntity and this is what I've done:

In RatePrediction.cc :

#include "CentralEntity.h"
RatePrediction::RatePrediction(double gs,double gi, double dataRate):
            gs(gs), gi_int(gi), reqDataRate(dataRate), result(0), error(0){
    //some codes
}

In CentralEntity.h :

#include "RatePrediction.h"
class CentralEntity : public cAsyncModule
{ 
//friend classes ....
protected:
  virtual void initialize();
}

In CentralEntity.cc:

#include "RatePrediction.h"

void CentralEntity::initialize()
{
RatePrediction RateObj(1.1e-13,1.2-13,1e4);
}

EDIT:

@hildensia Thanks for your comment. I actually did this and now it works. But I am not sure if it can be reliable and stable or not.

I edited the Makefile manually and I added:

OBJS= RatePrediction.o

RatePrediction.o: RatePrediction.cc
    $(CXX) -c $(COPTS) RatePrediction.cc
Shahab
  • 129
  • 1
  • 2
  • 8
  • Is RatePrediction.cc compiled into an object file and linked with the target executable? Is it included in the build? – eci Feb 21 '14 at 09:05
  • Can you show how you compile the program? The problem might lie in the fact, that you miss to link to RatePrediction.o (I was to slow...) – hildensia Feb 21 '14 at 09:07
  • @hildensia I am using Eclipse (GCC C++ compiler). about RatePrediction.o you are right. the file doesn't exist next to the other *.o files – Shahab Feb 21 '14 at 09:10
  • I don't know nothing about what Eclipse does to compile c++. Could you somehow extract the actual gcc calls it creates? The problem seems to be, that RatePrediction.cc is not compiled at all and thus the constructor can not be found by the linker. – hildensia Feb 21 '14 at 09:13
  • @hildensia Compiler invocation command: `g++` Compiler invocation arguments: `-E -P -v -dD "${plugin_state_location}/specs.cpp"` – Shahab Feb 21 '14 at 09:17

2 Answers2

1

The problem lies within the compilation. RatePrediction.cc seems to be not compiled at all. Thus the linker can't find the Constructor, because there is no object file with it. Your solution in the Makefile seems to be appropriate for now, but will probably be overwritten by your build system. So you should gather information about how you can define which files should be build by Eclipse.

hildensia
  • 1,650
  • 2
  • 12
  • 24
  • This is the correct answer, but some additional info: If you are using the OMNeT++ IDE, then the GUI generates the correct makefile for you automatically on each build. You just have to place your .cc file in any of the source folders and build the project. If you invoke the build process from the command line, you always have to call opp_makemake (with proper arguments) in the root of the source directory. This is the command that creates the proper makefile foe you and picks up new .cc files. (Recreating the makefile is necessary only if you've added new .cc/.h files or directories) – Rudi Jul 31 '14 at 08:59
0

You should define the construction function of Class RatePrediction in RatePrediction.h file. and it may be better to add such lines "#ifndef *_H #define *_H #endif" in every *.h files.