Question

#ifndef MIGRATINGUSER_H
#define MIGRATINGUSER_H
#include <iostream>
using namespace std;

class MigratingUser
{
        public:
        void populateUidAndGuid(string line);
        private:
        string uid;
        string guid; 
};
#endif

that s header definition and below is cpp definition:

#include "MigratingUser.h"
using namespace std;

void MigratingUser::populateUidAndGuid(string line)
{
  //get the uid and guid
}

and this is the error i am getting. how can i resolve this:

undefined reference to MigratingUser::populateUidAndGuid(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

for completeness this is how i use the class:

MigratingUser muser;

muser.populateUidAndGuid(temp);
Was it helpful?

Solution

First off, I really doubt that's the error you're getting, because it's not formatted correctly. Perhaps you got something that talked about std::basic_string<char, std::char_traits<char>, std::allocator<char> >, but it's unlikely a compiler would spit out what you posted unless you had some ... interesting ... code. In the future, copy and paste exactly because experts can get a surprisingly large amount of information out of details you don't consider important. And you can badly mislead StackOverflow users if you edit incorrectly, making it much harder for them to help you.

Now, the most likely cause of your problem is that you aren't aware of the difference between the compile step and the link step. That "undefined reference" error is a linker error.

This means your headers are all right - your code compiles, but you didn't put all the object files together correctly at the end - so, the output of g+ -c MigratingUser.cpp wasn't included with the output of g++ -c main.cpp (or whatever you named the "usage" code).

You can do the compile and link in one go (for simple projects) like so:

g++ -o my_executable MigratingUser.cpp main.cpp

This will both compile and link the two files together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top