0

main.cpp

#include <my_func.hpp>

using namespace std;

int main()
{
 MyClass my_class;
 my_class.func("123");
}

my_func.hpp

#include <iostream>

class MyClass{
 void func(auto value);
}

my_func.cpp

#include <my_func.hpp>

void MyClass::func(auto value){
 cout << value << endl;
}

the error occured "undefined reference to function"

but when I write code like this my_func.hpp

#include <iostream>

class MyClass{
 void func(auto value);
}

void MyClass::func(auto value){
 cout << value << endl;
}

It works very well.

I want to divide definition and source. but I dont know how to solve the problem.

I think that cause is "auto". when I change auto to string, It works well. but I have to get multi type parameter and I dont want to overloading because of the code is all same.

help me please.

MinSu Park
  • 11
  • 2
  • Check if you are linking your sources right (see the duplicate link). But also: the "auto" keyword is something evaluated in compile time. While you do not have to state the type with "auto", the type of the variable must still be deductable by the compiler. You do not provide the concrete type here, but your caller does. Your `.cpp` is a separate source, so your implementation and your provider of the type are compiled separately. Templates are [defined in headers](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file), too. – Meph Nov 07 '22 at 10:55
  • thank you for your comment. @Meph I refer to your link. and I use explicit implementation. so I solve the problem. Have a good time. – MinSu Park Nov 07 '22 at 13:32

0 Answers0