Pupusa
Pupusa

Reputation: 197

I have no idea what this error means

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.

Upvotes: 0

Views: 87

Answers (1)

Tyler McHenry
Tyler McHenry

Reputation: 76630

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.

Upvotes: 2

Related Questions