Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

class template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rich - 25 Jul 2005 23:36 GMT
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
Carl Daniel [VC++ MVP] - 26 Jul 2005 00:10 GMT
> Hi there,
>
[quoted text clipped - 26 lines]
> 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
rich - 26 Jul 2005 00:49 GMT
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'

> > Hi there,
> >
[quoted text clipped - 42 lines]
>
> -cd
rich - 26 Jul 2005 00:57 GMT
Thanks again Carl. I tried adding keyword typename in front of some of my
type names and it compiles now. Thanks a lot.

rich

> Thanks Carl.
>
[quoted text clipped - 80 lines]
> >
> > -cd
Ronald Laeremans [MSFT] - 26 Jul 2005 17:55 GMT
> Thanks again Carl. I tried adding keyword typename in front of some of my
> type names and it compiles now. Thanks a lot.
[quoted text clipped - 85 lines]
>>>
>>>-cd

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
Hendrik Schober - 27 Jul 2005 10:01 GMT
> [...]
> >>c:\...\sudynlist.h(342) : warning C4346: 'suDynList<TYPE,ARG_TYPE>::suNode'
[quoted text clipped - 3 lines]
> 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

Signature

SpamTrap@gmx.de 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.