VC6 compiling problem

G

Gernot Frisch

class Q
{
public:
template<class T>void foo(){}
};

main()
{
Q q;
q.foo<int>(); // error
}

error C2062: unexpected type 'int'

What can I do?
 
W

wittempj

on g++ this compiles and runs fine for me, it is essentially the same
as your code, except for the addition of returntype to main
#include <cstdlib>
#include <iostream>

using namespace std;
class Q
{
public:
template<class T>void foo(T t){ cout << t << endl; }

};

int main()
{
Q q;
q.foo<int>(4); // no error, it prints 4

system("PAUSE");
return 0;
}
 
G

Gernot Frisch

on g++ this compiles and runs fine for me, it is essentially the
same
as your code, except for the addition of returntype to main
#include <cstdlib>
#include <iostream>

using namespace std;
class Q
{
public:
template<class T>void foo(T t){ cout << t << endl; }

};

int main()
{
Q q;
q.foo<int>(4); // no error, it prints 4

system("PAUSE");
return 0;
}

Whatch it! you cnahged my code:

class Q
{
public:
template <class T> good(T*) {}
template <class T> bad () {}
};

main()
{
Q q;
int* i=NULL;
q.good(&i); // ok
q.bad<int>(); // fails
}
 
V

Victor Bazarov

Gernot said:
class Q
{
public:
template<class T>void foo(){}
};

main()
{
Q q;
q.foo<int>(); // error
}

error C2062: unexpected type 'int'

What can I do?

Change your compiler. VC++ v6 is notoriously bad with member
templates.
 
V

Victor Bazarov

Gernot said:
Whatch it! you cnahged my code:

class Q
{
public:
template <class T> good(T*) {}
template <class T> bad () {}
};

main()
{
Q q;
int* i=NULL;
q.good(&i); // ok
q.bad<int>(); // fails
}

Your code deserved it. In the posted form it's ill-formed.
Both member functions do not have a return value type.
'main' have no return value type.
"NULL" used in the main is undefined.

This is the corrected program (which still fails on VC++ v6):

class Q
{
public:
template <class T> void good(T*) {}
template <class T> void bad () {}
};

int main()
{
Q q;
int* i=0;
q.good(&i); // ok
q.bad<int>(); // fails
}

V
 
R

Ron Natalie

Victor said:
Change your compiler. VC++ v6 is notoriously bad with member
templates.
Actually, the problem is VC++ 6 is notoriously bad with template
function names. It relies on the normal function arg decoration
to disambiguate instances of the same template. When you have a
template that none of the args depends on the template variable it
gets confused. Even more stupid is in the case of non-member
templated functions, it links just fine, but all refer to the same
function (i.e., it uses only one instance of the template).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top