Template Example Builds in VC++ 6, Errors in Watcom C++ 1.9

Today I tried to do a template example out of my new C++ book by Siddhartha Rao. OpenWatcom C++ 1.9 gave an error message, "left expression must be integral" for the lines where the type being passed to the template function was string, while there was no error for types int and double. Next I brought up the source in MS Visual C++ 6, the Introductory edition and it built fine.

At them moment all I'm trying to do is master templates, but this difference between my two C++ compilers is a distraction. I would prefer that the OpenWatcom compiler handle everything I do.
Without seeing the code you're trying to compile, it's hard to diagnose the problem.
Try compiling on http://www.ideone.com
Here is the example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<string>
using namespace std;

template <typename Type>
const Type& GetMax(const Type& value1, const Type& value2)
{
	if (value1 > value2)
		return value1;
	else
		return value2;
}

template <typename Type>
void DisplayComparison(const Type& value1, const Type& value2)
{
	cout << "GetMax(" << value1 << ", " << value2 << ") = ";
	cout << GetMax(value1,value2) << endl;
}

int main()
{
	int Int1 = -101, Int2 = 2011;
	DisplayComparison(Int1,Int2);
	
	double d1 = 3.14, d2 = 3.1416;
	DisplayComparison(d1,d2);

	string Name1("Jack"), Name2("John");
	DisplayComparison(Name1,Name2);
	
	return 0;
}


Thank you for getting back to me.
That code is valid. If you have a compiler that rejects it, don't use that compiler.

(I just ran it through gcc, clang, xlc, and sun studio, no complaints)
Works fine in VS, too. He just has to upgrade from ancient VS6.
VS6 is the compiler that built it. It's Watcom that wouldn't.
Topic archived. No new replies allowed.