0

i have an list with some datas list<string> l; and if i pass the value to an class process it gives me error

Process p; p.getnames(l); 

and i have an header file process.h with

 class Process {
    public:
        void getnames(list<string> ll);

    };

and i have an cpp file process.cpp

void getnames(list<string> ll)
{
// i can use the names here
}

error

undefined reference to `Process::getname(std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)'
newb
  • 41
  • 5
  • Note that you rarely ever want to _copy_ a container parameter. See _[How to pass objects to functions in C++?](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/)_ for a set of rules of thumb regarding this. – sbi Feb 18 '11 at 10:10

3 Answers3

3

The error you're getting is a linker error because you've defined a free function named getname rather than a member function getname. To fix this, change your .cpp file to have

Process::getname(list<string> names)

And you should be good. The error now is that the compiler thinks you're defining some random function with no connection to Process, so when it compiles the code and someone tries to use Process::getname the linker can't find an implementation for it, and it's not smart enough to know that the free function you defined was intended to be a member function, hence the poor diagnostic.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

When separating definition from declaration, in definition you need to include class name as following:

void Process::getnames(list<string> ll)
{
// i can use the names here
}

What you did is that you defined getnames(list ll) function that returns void.

sinek
  • 2,458
  • 3
  • 33
  • 55
2
void Process::getnames(list<string> ll)
{
// i can use the names here
}
Erik
  • 88,732
  • 13
  • 198
  • 189