0

I'm writing an example program to help build an understanding of templates in C++. I am trying to use a template class to have more than one functionality.

Below is the following code I have written up.

// Example program
#include <iostream>
#include <string>
using namespace std;

template<class test>
test addstuff(test a, test b){
    return a+b;
}
test multiplystuff(test a,test b){
    return a*b;
}

int main()
{
  double a,b,c;
  cout << "Enter a value for a." << endl;
  cin >> a;
  cout << "Enter a value for a." << endl;
  cin >> b;
  c = addstuff(a,b);
  cout << c << endl;
  c = multiplystuff(a,b);
  cout << c << endl;

}

The error I am getting is in the test multiplystuff in my function, it is not within scope is the error I receive. I expect the template to be able to handle more than one functionality, What could the problem be?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
BChiu
  • 69
  • 1
  • 10

3 Answers3

4

This:

// ...

test multiplystuff(test a,test b){
    return a*b;
}

// ...

Does this look like a function template? To the compiler, it doesn't. Even for humans, If I see that I would expect it not to be a function template.

Now let's add the context again:

template<class test> // has template parameters
test addstuff(test a, test b) {
    return a + b;
}

// no template parameters
test multiplystuff(test a,test b) { // cannot access test?
    return a * b;
}

One function is a template, but the second clearly isn't.

Expecting test to be available in the second function is like expecting argument to be accessible to other function:

// has int parameter
void func1(int a) { /* ... */ }

// no int parameter
void func2() {
    // cannot access a
}

In this example, a is out of scope in func2.

The same thing happen with your function template. The template argument is not available outside the function.

Obviously, the solution is to add the missing argument to the second function.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
2

You dont actually have a templated class at all. You have 2 unrelated free functions addstuff and multiplystuff, and template<class test> only applies to the first one. Either actually use a class or add another template<class test> like this:

template<class test>
test addstuff(test a, test b)
{
    return a + b;
}

template<class test> 
test multiplystuff(test a,test b)
{
    return a * b;
}

Also, dont using namespace std;

DeviatioN
  • 340
  • 1
  • 3
  • 8
0

template<class test> is not a template declaration. Nor does it declare a class (or class template). It forms part of a template declaration (in this case. It can also form part of a definition).

Instead

template<class test>
test addstuff(test a, test b){
    return a+b;
}

is a template declaration, as is template<class test> test addstuff(test a, test b);.

If you want both addstuff and multiplystuff to be templates, you have to declare them both to be templates. However I would just use + and *.

Caleth
  • 52,200
  • 2
  • 44
  • 75