newb
newb

Reputation: 41

list<string> cannot pass it to an class in c++

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> > > >)'

Upvotes: 0

Views: 150

Answers (3)

sinek
sinek

Reputation: 2488

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.

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 372814

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.

Upvotes: 3

Erik
Erik

Reputation: 91270

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

Upvotes: 2

Related Questions