Unresolved static template methods

Nikolas Fraser 60 Reputation points
2023-11-23T14:48:22.7233333+00:00

I'm having some trouble with my static template methods, specifically that the linker can't resolve them. I'm not sure why, because as I understand it, the compiler is supposed to write the methods. I have no problem resolving the class's non template methods, including static methods, which leads me to believe that the problem is specific to the template itself.

I'd appreciate any insight as to what the underlying problem is.

"Object.h" declaration

template <typename T>
static T FindObject(long UUID, std::vector<Object*>& Objs, bool crash = true);
template <typename T>
static T FindObject(std::string name, std::vector<Object*>& Objs, bool crash = true);

"Object.cpp" definition

template <typename T>
T Object::FindObject(long UUID, std::vector<Object*>& Objs, bool crash) { 
	// function body
}

template <typename T>
T Object::FindObject(std::string name, std::vector<Object*>& Objs, bool crash) {
	// function body
}

Example call which fails to resolve:

bool Law::getChoiceActive(std::string name) {
 	return Object::FindObject<Choice*>(name, choice)->getSelected(); 
}
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-11-23T15:28:48.9966667+00:00

    Try to move the template definitions from Object.cpp to Object.h.

    It is probably possible to instantiate these functions in Object.cpp, but only for specific types, like Choice*.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.