开发者

Undefined reference for function accepting references to user defined classes

开发者 https://www.devze.com 2023-02-20 16:27 出处:网络
I have a problem with a function that accepts as input references to two user defined objects. Here\'s what it looks like:

I have a problem with a function that accepts as input references to two user defined objects. Here's what it looks like:

void copy_job_list( vector<Job> &org, vector<Job> &dest)  
{  
      // ....  
}

The function is defined in file functions.cpp, and called in job_random.cpp, both files include the header in which the declaration of this function is given.

Now when in job_random.cpp I call the function with

copy_job_list( Old_best_list.J, Old_best_list_new_times.J) ;  

where Old.best_list.J and Old_best_list_new_times.J are vectors of type Job, I get the following error with g++:

job_random.o:job_random.cpp:(.text+0x1fc): undefined reference to 
`copy_job_list(std::vector<Job, std::allocator<Job> >, 
                           std::vector<Job, std::allocator<Job> >&)'
collect2: ld returned 1 exit status
mingw32-make: *** [random_job] Error 1

This looks like linker is convinced only second argument should be a reference, and first should be a value. And indeed if I change definition and declaration of copy_job_list() so that first argument is passed by value, then program compiles. But why would linker insist I do that?

From what I was able to find on the web it seems like undefined reference messages usually mean I didn't include headers the right way or such, but functions.cpp contains a multitude of other functions that job_random.cpp uses without 开发者_如何学运维a hitch, all of them declared through a common header.


You likely have void copy_job_list( vector<Job> org, vector<Job> &dest) in a header file, or have it forward declared some other way. Also make sure that your dependencies are right - do a clean build.


First, the error message looks like it's coming from the linker, not the compiler. It's just a guess, since you don't show us the code, but I'd guess that the declaration in the header is missing the &. (I'm also curious as to what vector could be. The error message suggests std::vector, but that's a template, and the code you show us doesn't show any use of a template.)

0

精彩评论

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