1

I want to pass any-type parameter to my function func1(). So here is my code: myclass.h :

public:
   myclass();
   template<typename T> void func1(T object);

myclass.cpp :

template<typename T> 
void myclass::func1(T object)
{
    return;
}

main.cpp :

int a=0;
myclass::func1<int>(a);

But I got this error :

error: cannot call member function 'void myclass::func1(T) [with T = int]' without object

Where is my mistake?

lord.h
  • 153
  • 3
  • 14
  • 3
    A member function is still a member function, even if it is a template. That means you need an *object* to call it on. – Some programmer dude Jul 24 '17 at 13:14
  • 1
    And if you haven't used templates before, then you might find [this question and its answers informative](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Some programmer dude Jul 24 '17 at 13:14
  • 1
    Also you should not be specifying the template type. You should let template argument deduction work for you. – NathanOliver Jul 24 '17 at 13:15
  • The instantiated function is not static, so you need an object instance to call it. – Thinkeye Jul 24 '17 at 13:30
  • @Someprogrammerdude OK!... I create an object from myclass and I got this error `error: undefined reference to `void myclass::func1(int)'` – lord.h Jul 24 '17 at 13:31
  • For that see my second comment. – Some programmer dude Jul 24 '17 at 13:32
  • @Someprogrammerdude . I saw that question and answer, but I think that is not my answer, maybe I can't Understand where is the point exactly. would you answer my question with correct sample for `passing any-type parameter to function in c++ using template`. This way I can accept that for other people seeing this question. I prefer to use function without object instance. – lord.h Jul 24 '17 at 13:49
  • @Thinkeye Putting `static` before my function declaration is enough? – lord.h Jul 24 '17 at 13:52
  • 3
    You need to *define* the function in the header file, you can't split templates into header and source files like you normally do. – Some programmer dude Jul 24 '17 at 13:54
  • If you don't want to use an object instance, why define a class in the first place? – melpomene Jul 24 '17 at 14:55

1 Answers1

5

You cannot simply separate declaration and definition in template functions. The simplest thing to do for a template function is to provide the code body in the function declaration in the header file.

If you want to call the function without a class object add static to the function signature.

header.hpp

#include <iostream>

class test_class{
public:
     template<typename T> static void member_function(T t){
        std::cout << "Argument: " << t << std::endl;
    }

};

main.cpp

#include <iostream>

#include "header.hpp"

int main(int argc, char ** argv){

    test_class::member_function(1);
    test_class::member_function("hello");
}
k_kaz
  • 596
  • 1
  • 9
  • 20
  • Thanks! It works but I wonder for declaration and definition separated simple example in template functions. If it's not possible then I will accept your answer. – lord.h Jul 24 '17 at 14:56
  • 1
    @lord.h I don't think it's possible, but i am no expert. You can "trick" the compiler by having declarations and definitions in header and source files, and then include the source file in the end of the header file. I don't think i can explain any better from the already present answers. https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – k_kaz Jul 24 '17 at 15:01