dimanche 29 janvier 2017

C++ linker error while invoking member function of a class

I am trying to compile and run a simple c++ program, and running into a mysterious linker error.

I have the following three files:

MulticlassDataLoader.h

#ifndef MULTICLASS_DATALOADER
#define MULTICLASS_DATALOADER

#include <iostream>

struct MetaData{
  size_t a;

  MetaData(): a(0){}
  ~MetaData() {}
};

class MulticlassDataLoader{

public:
  MulticlassDataLoader(){}

  ~MulticlassDataLoader(){}

  int dummy(int i);
};

#endif

MulticlassDataLoader.cpp

#include "MulticlassDataLoader.h"
#include<iostream>

int MulticlassDataLoader::dummy(int i){
  return i;
}

main.cpp

#include "MulticlassDataLoader.h"

int main (int argc, char **argv){
  MetaData mdata;
  MulticlassDataLoader loader;
  loader.dummy(10);

  return 0;
}

I get the following error on compiling the code: $ g++ -std=c++11 main.cpp /tmp/cc1nj9Fl.o: In function `main': main.cpp:(.text+0x35): undefined reference to `MulticlassDataLoader::dummy(int)' collect2: error: ld returned 1 exit status

Note that the objects mdata and loader themselves get declared fine, the above error pops up only when I try to invoke the function loader.dummy(..).

What could be the possible reason? I think I am including the header files in the right way. Is there something silly that I am missing? Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire