Hello everyone..

Would anyone know why I can't do the below. I'm not the best programmer in the world so I'm wondering why the below won't work. I get an error

'A template declaration cannot appear in block scope'

No doubt it's due to my lack of understanding of the topic. Thank you.

#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;

int main()
{
template<typename T>

   T one = "test";
   T two = 255;
   T three = 287.52;
   T four = 'x';

cout << one << " " << two << " " << three << " " << four << endl;

system(pause>nul);
}

declare your template outside the main() function and then try to compile it. hope it helps.! thanks.

You need to do some research on Templates. This clearly would not work in main because there is no way at run-time to determine what types are the variables. (In this example, anyway).

The correct thing would be to do the following:

#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;

template<typename T>
T declareVariable(T theVariable)
{
    T temp = theVariable;

    return temp;
}
int main()
{
    string value = declareVariable<string>("hello");

    cout << value;
}

Notice how in main I can now allow for the variable to be declared so the compiler knows at run time?

I can therefore do this for an interger as well:

int value2 = declareVariable<int>(123);

This should be the solution :)

The construct template<typename T> is used to paramatrize classes, structs, unions, functions or, in C++ 11, type aliases with type parameters (or in some cases other compile-time parameters). So template<typename T> must be in front of the definitions of one of those things (like template<typename T> class Foo {...}). In your case it's inside a function body, which is not allowed.

It's not clear what you want parametrize here. If you wanted to parametrize main, you'd need to write template<typename T> int main(), except that it's not legal for main to be a template function because main is called by the operating system and the OS would not know which type to supply as an argument.

So you'd need to define your own function template<typename T> mymain(), put the code in there and then call that from main with the appropriate type argument. Except that won't work either because:

T one = "test";
T two = 255;
T three = 287.52;
T four = 'x';

Those four variables can't have the same type (unless there's a custom defined class with implicit constructors accepting each of the four types, but in your code there's not).

It's important to understand that in any given instantiation of a template, all occurrences of T refer to the same type. So if call, say mymain<int>() and mymain contains the above code, it will behave like you wrote:

int one = "test";
int two = 255;
int three = 287.52;
int four = 'x';

And this won't compile because you can't assign a string literal (or any other type of pointer) to an integer like that. Likewise mymain<char*> or mymain<string> wouldn't work because the other values can't be assigned to a string.

Thanks for the answers.. I understand now.
phorce, I've used your example without indicating the type next to the function call. By using the function you provided and calling it like the below: It works fine.

cout << myfunction(20) << endl;
cout << myfunction("TestString") << endl;
cout << myfunction(2.1) << endl;
cout << myfunction('x') << endl;

Click Here

This is a great link which will give you idea that how exactly templates work. It will be beneficial for you. hope it helps! thanks. :)

Thanks so much nitin.

@christinetom - That's great! The compiler will specify the type based on the type of variable passed through. Good luck and thanks for marking this as solved :)

As others told, you should define template outside the main function, hope that works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.