开发者

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

开发者 https://www.devze.com 2023-02-12 01:46 出处:网络
i have an list with some datas list<string> l; and if i pass the value to an class process it gives me error

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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消