0

Simple program I do not figure out what did I do wrong, probably it's just a syntax related problem.

The goal of my programm is to make a function which returns a value + 5 but the value will be unknown so I tried using templates. This is what I tried to do:

#include<iostream>

template <class value>
auto addition(value number);

int main(){

    auto number1=76;
    auto result=addition(number1);
    std::cout<<"The result is "<<result;

}


template <class value>
auto addition(value number){
    
    return number+5;
}

I hope you can help me make this simple programm work, and if possible give me some information about the usage of auto and templates.

Thanks in advance.

zavkk
  • 1
  • 1
  • 3
    You can't forward declare templates. Remove the declaration and replace it with the definition. See also [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). When the code uses the template (in `main`) it needs to be able to instantiate it (replace all the parameters) and generate the code. It can't do this with only a forward declaration. – Richard Critten Mar 07 '23 at 21:51
  • 2
    As your code stands, how would the compiler know what the deduced return type will be when it gets to the `auto result=addition(number1);` line? You need to put the **whole definition** of the templated function *before* its use. – Adrian Mole Mar 07 '23 at 21:52

0 Answers0