1
I created a function using feedback as demonstrated in that reply, in this way:
utilits. h
...
template<typename T> bool theres(T a, vector<T> b);
...
cpp utilities.
...
template<typename T> bool theres(T a, vector<T> b)
{
for(T& it : b)
if (it == a)
return 1;
return 0;
}
...
main.cpp
...
vector<string> registro(0);
...
int main ()
{
...
string nick = "far";
...
if(theres(nick, registro)) // <- Erro aqui
...
}
I get the following error:
undefined reference to `bool theres<std::string>(std::string, std::vector<std::string, std::allocator<std::string> >)'
This information may be dated, but C++ handles templates via code substitution. This means that the template code needs to be in the same compilation unit as the type in the template; i.e., implementation in the
.h
– Jefferson Quesado
@Jeffersonquesado, thanks man, it’s working. I didn’t know I could implement the variable in the file
.h
– Felipe Nascimento
@Jeffersonquesado, why did you reply with a comment instead of a reply?
– Kyle A
@Kylea found that there was not enough quality for response. Short and unexplained
– Jefferson Quesado