question about using template class

T

thinktwice

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;
}
 
M

Michiel.Salters

thinktwice said:
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 said:
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
 
I

Ian Collins

thinktwice said:
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?
 
F

Frederick Gotham

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;
}
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top