0

I compiled my code and this error message showed up in the terminal that I have no idea what it means. Could someone explain or give me some hints?

/tmp/ccZ95DTV.o: In function `main': homework3_test.cpp:(.text+0x514):
undefined reference to `std::vector<int, std::allocator<int> >&
apply<PowerN>(std::vector<int, std::allocator<int> >&, PowerN)'
collect2: ld returned 1 exit status

Code:

int test_power1, test_power2, test_power3;
PowerN power_three(3);
//test_power will now be 1
power_three(test_power1);
//test_power will now be 3
power_three(test_power2);
//test_power will now be 9
power_three(test_power3);
if (1 == test_power1) {
    std::cout<<"PowerN works for 3**0! +10\n";
    score += 10;
}
else {
    std::cout<<"PowerN failed on 3**0!\n";
}

if (3 == test_power2 and 9 == test_power3) {
    std::cout<<"PowerN works for 3**1 and 3**2! +10\n";
    score += 10;
}
else {
    std::cout<<"PowerN failed for 3**1 and 3**2!\n";
}

std::vector<int> test_power_v(3);
PowerN power_lessfour(-4);
//apply turns the vector into [1, -4, 16]
apply(test_power_v, power_lessfour);
std::vector<int> check_power_v;
check_power_v << 1 << -4 << 16;
if (test_power_v == check_power_v) {
    std::cout<<"Applying PowerN with -4 works! +10\n";
    score += 10;
}
else {
    std::cout<<"Applying PowerN with -4 failed!\n";
}

so what is added is the test code given by my instructor. If you want to see my implementation of the code,let me know.

so these line of code is my header file

template <typename T>
vector<int>& apply(vector<int>& v, T function);

and these are the implementation in the cpp file

template <typename T>
vector<int>& apply(vector<int>& v, T function) {
    for (vector<int>::iterator I = v.begin(); I != v.end(); ++I) {
        function(*I);
    }
    return v;
}

Thanks guys. I solve the issue. The template has to be defined in the header file, not the implementation file.

SirGuy
  • 10,660
  • 2
  • 36
  • 66
Pupusa
  • 167
  • 1
  • 3
  • 14
  • if u could share the code – balaji Mar 26 '14 at 04:24
  • could you show some code please? – Claudiordgz Mar 26 '14 at 04:24
  • You've probably spelled one of your variables/objects wrong when trying to call it. Post code! – domdomcodecode Mar 26 '14 at 04:29
  • You defined some function called `apply` and then implemented it, and the arguments don't match exactly the same. For example `template apply(vector&, T);` and `template apply(vector, T){...}` (Notice the missing ampersand) – AndyG Mar 26 '14 at 04:29
  • 1
    You added more code, but not the code we need. Show the `apply` function definition and implementation. – AndyG Mar 26 '14 at 04:33
  • Yeah, your implementation code is what is important. In particular, we need to see where the body of apply() is, because that's what the compiler is complaining about. – Tyler McHenry Mar 26 '14 at 04:33
  • Based on your edit, the body of apply() seems correct. Is it in the _same cpp file_ as the first block of code you posted? If it's in a different cpp file, are you sure you told your compiler to build _both_ files? – Tyler McHenry Mar 26 '14 at 04:54
  • http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Mar 26 '14 at 04:56
  • @TylerMcHenry the first block of code I posted first was in a file where the main function is. Then the function is declared in a header file and actual implementation of it is in a different cpp file. I am sure while I compile it , I typed in command as "g++ -o o homework3.cpp homework3_test.cpp. Thank you for the help – Pupusa Mar 26 '14 at 13:11
  • possible duplicate of ["Undefined reference to" template class constructor](http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor) – anatolyg Mar 26 '14 at 19:19
  • A related question, with another way to solve the problem: [here](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – anatolyg Mar 26 '14 at 19:20

1 Answers1

2

It means you created a prototype for a function with the signature:

template <typename PowerN> vector<int> apply(vector<int>&, PowerN)

And you called it, but you never actually wrote a body for this function in any of the source that you asked your compiler to build.

Did you:

  • Typo the name of this function when writing the body?
  • Provide a slightly different signature for the function when writing its body (e.g. leave off an & or something)?
  • Write the body in a different source file that you are not building?

Posting your code would help people to diagnose better.

Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166