I wanna define a friend function inside the template not outside but the linker cannot get it :
1 2 3 4 5 6 7
void func();
template<typename T>
class test {
friendvoid func() {
cout << "worked" << endl;
}
};
when I call this function on an ordinary class it works but with templates, it gives me this
error : LNK2019 unresolved external symbol "void __cdecl func(void)" (? func@@YAXXZ) referenced in function _main
in addition, if I defined the function outside the template it works!!
1 2 3 4 5 6 7
void func() {
cout << "worked" << endl;
}
template<typename T>
class test {
friendvoid func();
};
#include <iostream>
class Test {
void memberMethod() { /* do nothing */ }
friendvoid notMemberFunction() { /* do nothing */ }
friendvoid notMemberDefinedOutside(); // prototype only
};
void notMemberFunction() {} // Error! This is not an overloading, this is
// a re-definition.
// notMemberFunction() already defined at line 5
void notMemberDefinedOutside() {} // Fine. This is not a redefinition, since
// so far notMemberDefinedOutside()
// has not been defined, but only declared.
void memberMethod() {} // Ok. Despite its name, this is *not*
// Test::memberMethod()
int main()
{
return 0;
}
2) (only allowed in non-local class definitions) Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline.
A class template by itself is not a type, or an object, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).
#include <iostream>
void func() { std::cout << "func() outside Test\n"; }
template<typename T>
class Test {
friendvoid func() { std::cout << "func() friend of Test\n"; }
};
void waitForEnter();
int main()
{
func(); // The only 'existing' func() is the one outside the template
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
I think the problem is the template class Test simply doen’t exist: no code is generated for it. So there can't be any inline function.
But here, it comes to life (and gives redefinition error):
#include <iostream>
void func() { std::cout << "func() outside Test\n"; }
template<typename T>
class Test {
friendvoid func() { std::cout << "func() friend of Test\n"; }
};
void waitForEnter();
int main()
{
func(); // The only 'existing' func() is the one outside the template
Test<int>().func(); // redefinition of func()
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Compiler errors:
In instantiation of 'class Test<int>':
15:15: required from here
7:17: error: redefinition of 'void func()'
friend void func() { std::cout << "func() friend of Test\n"; }
^~~~
3:6: note: 'void func()' previously defined here
void func() { std::cout << "func() outside Test\n"; }
^~~~
: In function 'int main()':
15:17: error: 'class Test<int>' has no member named 'func'
Test<int>().func(); // redefinition of func()
^~~~