class template

G

Guest

Hi there,

I defined a class template (MyClass) and some member variables and
functions, as following:

template<class T1, class T2>
class MyClass
{
...
struct m_variable
{
...
};
m_variable* MyFunc(m_variable* pv1, m_variable* pv2);
...
}

template <class T1, class T2>
MyClass<T1, T2>::m_variable* MyClass<T1, T2>::MyFunc(MyClass::m_variable*
pv1, MyClass::m_variable* pv2)
{
...
m_variable* pv;
...
return pv;
}

It compiles with VC++ v6.0, but doesn't compile with VC++ v7.0 (.Net 2003).
Anybody can give me some advice to modify the code to make it compile.

Many thanks

rich
 
C

Carl Daniel [VC++ MVP]

rich said:
Hi there,

I defined a class template (MyClass) and some member variables and
functions, as following:

template<class T1, class T2>
class MyClass
{
...
struct m_variable
{
...
};
m_variable* MyFunc(m_variable* pv1, m_variable* pv2);
...
}

template <class T1, class T2>
MyClass<T1, T2>::m_variable* MyClass<T1,
T2>::MyFunc(MyClass::m_variable* pv1, MyClass::m_variable* pv2)
{
...
m_variable* pv;
...
return pv;
}

It compiles with VC++ v6.0, but doesn't compile with VC++ v7.0 (.Net
2003). Anybody can give me some advice to modify the code to make it
compile.

Please post either or both of: some actual (complete) code that doesn't
compile and the exact error(s) that you're getting from the compiler.

In all likelihood, you need to add the 'typename' keyword in a few places to
make the code legal C++ (VC6 didn't enforce that rule, VC7.1 does).

e.g.

template <class T1, class T2>
typename MyClass<T1, T2>::m_variable* MyClass<T1, T2>::MyFunc(
MyClass::m_variable* pv1,
MyClass::m_variable* pv2
)

-cd
 
G

Guest

Thanks Carl.

MyClass<T1, T2>::m_variable is the type name for return. The following is
the code and error messages.

template<class TYPE, class ARG_TYPE>
class suDynList : public CObject
{
protected:
struct suNode
{
suNode* iNext;
suNode* iPrev;
TYPE data;
};
suNode* NewNode(suNode* prev, suNode* next, ARG_TYPE& newElement);
....


template<class TYPE, class ARG_TYPE>
suDynList<TYPE, ARG_TYPE>::suNode* //line 342
suDynList<TYPE, ARG_TYPE>::NewNode(suDynList::suNode* prev,
suDynList::suNode* next,
ARG_TYPE& newElement) //line 343
{
...

c:\...\sudynlist.h(342) : warning C4346: 'suDynList<TYPE,ARG_TYPE>::suNode'
: dependent name is not a type prefix with 'typename' to indicate a type
c:\...\sudynlist.h(342) : error C2143: syntax error : missing ';' before '*'
c:\...\sudynlist.h(342) : error C2501: 'suDynList<TYPE,ARG_TYPE>::suNode' :
missing storage-class or type specifiers
c:\...\sudynlist.h(343) : error C2065: 'TYPE' : undeclared identifier
c:\...\sudynlist.h(343) : error C2065: 'ARG_TYPE' : undeclared identifier
c:\...\sudynlist.h(343) : error C2955: 'suDynList' : use of class template
requires template argument list
c:\...\sudynlist.h(120) : see declaration of 'suDynList'
c:\...\sudynlist.h(345) : error C2061: syntax error : identifier 'ARG_TYPE'
 
G

Guest

Thanks again Carl. I tried adding keyword typename in front of some of my
type names and it compiles now. Thanks a lot.

rich
 
R

Ronald Laeremans [MSFT]

rich said:
Thanks again Carl. I tried adding keyword typename in front of some of my
type names and it compiles now. Thanks a lot.

rich

:


OK, I do have to ask: Can you suggest how we could make the compiler
error message more clear? We worked very hard here to have the compiler
give a really accurate diagnostic, but in practice it still fails to
help many developers. Any suggestions on how to improve it are very welcome.

Thanks!

Ronald Laeremans
Visual C++ team
 
H

Hendrik Schober

Ronald Laeremans said:
[...][...]

OK, I do have to ask: Can you suggest how we could make the compiler
error message more clear? [...]

I doubt that you could get much better than saying
"prefix with 'typename' to indicate a type". IMHO
the reason this still gets asked here so often is
that many VC-only developers (do as the VC team did
for many years and) don't care much for the C++ std.
They simply never heard of 'typename', they never
bothered to really learn templates, and never read
a general C++ book. It's only 2 years that you have
a more or less decent compiler regarding templates
(and thus show a different attitude towards std C++)
-- it will take a while for your customers to catch
up. (I don't see it as an incident that many of
those who answer such questions here seem to have
regular exposure to other compilers.)
I expect an even bigger amount of FAQs swamping this
newsgroup should you ever get around to implement
std conforming lookup rules. Porting template code
checked using VC7.1 to other compilers (many of
which do implement proper lookup) is a PITA due to
this.
Ronald Laeremans

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

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

Top