C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

question about using template class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
thinktwice
Guest





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: question about using template class Reply with quote



can't compile the file below, i have remarked the error line, if i
delete that line , it just pass throgh the compile, can anyone tell me
why?

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

#define pTC static_cast<TClass*>(this);

template<class TClass>
class Base
{
public :
void Test(){
pTC->Hello();
}
void Test(int i){
cout<<"base";
}
};


template<class TClass>
class Derived: public Base<TClass>
{
public :
void Test(){
pTC->Hello(); //error C2143: syntax error : missing ';' before '->'
cout<<"Derived";
}
virtual void Hello(){
cout<<"";
}
};

class Final : public Derived<Final>
{
public:
void Hello(){
cout<< "final";
}
};

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
Final f;
std::vector<int> vec;
f.Test();
return 0;
}
Back to top
Ian Collins
Guest





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: Re: question about using template class Reply with quote



thinktwice wrote:
Quote:
can't compile the file below, i have remarked the error line, if i
delete that line , it just pass throgh the compile, can anyone tell me
why?

#include "stdafx.h"
#include <iostream
#include <vector

using namespace std;

#define pTC static_cast<TClass*>(this);

Why are you dong this?


--
Ian Collins.
Back to top
Guest






PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: Re: question about using template class Reply with quote



thinktwice wrote:
Quote:
can't compile the file below, i have remarked the error line, if i
delete that line , it just pass throgh the compile, can anyone tell me
why?

Yes - it doesn't make sense at all.

Quote:
#include "stdafx.h"
#include <iostream
#include <vector

using namespace std;

#define pTC static_cast<TClass*>(this);

Avoid macros. They just add confusion, and in this case you were so
confused
you didn't see the error.

Quote:
template<class TClass
class Base
{
public :
void Test(){
pTC->Hello();
}
void Test(int i){
cout<<"base";
}
};

Wrong - you can't cast a Base<TClass>* to TClass*. You will see this
error if you try to instantiate Base<TClass>::Test()

Quote:
template<class TClass
class Derived: public Base<TClass
{
public :
void Test(){
pTC->Hello(); //error C2143: syntax error : missing ';' before '->'
cout<<"Derived";
}
virtual void Hello(){
cout<<"";
}
};

Same error here.

Quote:
class Final : public Derived<Final
{
public:
void Hello(){
cout<< "final";
}
};

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
Final f;
std::vector<int> vec;
f.Test();
return 0;
}

f.Test means f.Derived<Final>::Test(), as Final has no Test function.
So
Derived<Final>::Test is found and instantiated. This will give an
error.
Base<Final>::Test is not instantiated.

HTH,
Michiel Salters
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.