 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
thinktwice Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: question about using template class |
|
|
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
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: question about using template class |
|
|
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
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: question about using template class |
|
|
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 |
|
 |
|
|
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
|
|