question about using template class



 DEVELOP > c-Plus-Plus > question about using template class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "thinktwice"
Date: 16 Aug 2006 03:21:47 AM
Object: 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;
}
.

User: "Frederick Gotham"

Title: Re: question about using template class 16 Aug 2006 09:58:52 AM
thinktwice posted:

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?

The following compiles without error or warning on my system. I didn't
bother to check the quality of the code however:
#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)
{
cout << "Base\n";
}
};
template<class TClass>
class Derived: public Base<TClass> {
public:
void Test()
{
pTC->Hello();
cout << "Derived\n";
}
virtual void Hello()
{
}
};
class Final : public Derived<Final> {
public:
void Hello()
{
cout<< "Final";
}
};
int main()
{
Final f;
std::vector<int> vec;
f.Test();
return 0;
}
--
Frederick Gotham
.

User: "Ian Collins"

Title: Re: question about using template class 16 Aug 2006 03:49:12 AM
thinktwice wrote:

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.
.
User: "Gary li"

Title: Re: question about using template class 16 Aug 2006 04:24:10 AM
should remove ; from the macro.
Ian Collins wrote:

thinktwice wrote:

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.

.


User: ""

Title: Re: question about using template class 16 Aug 2006 03:45:50 AM
thinktwice wrote:

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.

#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.

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()

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.

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER