Hi there,

I am trying to write a C++ app using Dev-C++ 4.9.9.2. The app is supposed to read in a txt file and then use templates to read in the files and swap data that gets populated in a ptr array. The errors are:

[Linker error] undefined reference to `SalaryCompare::makeList(std::basic_ifstream<char, std::char_traits<char> >&, voter**)'
[Linker error] undefined reference to `void SalaryCompare::swap<voter>(voter**, int, int)'
ld returned 1 exit status
C:\Users\dwilmes\Desktop\project2\Makefile.win [Build Error] [Project2.exe] Error 1

thanks for any ideas or answers.

Daniel

triple check that your prototypes match your definitions, and that your calls to the functions agree with parameter lists.

ALL your template functions in the cpp file need to be in the .h file.

When you instance a template with a type, the compiler needs all the template code to generate the appropriate type-specific instances of those functions.

in my .h file i have:

template<class T> void swap(T *[], int, int);

my implementation is:
template<class T>
void SalaryCompare::swap(T *array2swap[], int x, int y) {
    T temp = array2swap[x];
    array2swap[x] = array2swap[y];
    array2swap[y] = temp;
}

my call is:

comp_app->swap<voter>(voter_array, 0, (voteCount-1));

They seem to agree. Is there something that isn't correct?

thanks daniel

Like I said, at what point in your compilation steps did the compiler generate the code for swap<voter>

Was it when it compiled salarycompare.cpp - doubtful, it doesn't know what a voter is.
Was it when it compiled main.cpp - again no, the actual template code isn't in view (only the class in the .h file).

got it to work, put the template fun in the .h file. That seemed to at least get it to compile.

thanks daniel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.