question about using template class

Discussion in 'C++' started by thinktwice, Aug 16, 2006.

  1. thinktwice

    thinktwice Guest

    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;
    }
     
    thinktwice, Aug 16, 2006
    #1
    1. Advertising

  2. thinktwice

    Guest

    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
     
    , Aug 16, 2006
    #2
    1. Advertising

  3. thinktwice

    Ian Collins Guest

    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.
     
    Ian Collins, Aug 16, 2006
    #3
  4. thinktwice

    Gary li Guest

    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.
     
    Gary li, Aug 16, 2006
    #4
  5. 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
     
    Frederick Gotham, Aug 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page