Class Template with Specialization code Giving Error

Discussion in 'C++' started by Pallav singh, Feb 21, 2008.

  1. Pallav singh

    Pallav singh Guest

    Hi

    I am getting error in compiling this code .......Unable to find error
    Kindly suggest me something.

    Thanks
    Pallav

    ===================================================================

    #include<iostream.h>

    template<typename T>
    class B
    {
    public :

    T DependentName;
    int basefield;
    typedef int X;
    };


    template<>
    class B<char>
    {

    public :
    enum E{basefield = 1};
    int func()
    { return basefield;}
    };


    template<typename T>
    class D:B<T>
    {
    public :
    int func()
    { basefield = 12;
    return basefield; }
    };


    void call(D<int> & obj_int){
    cout<<"called function :: call(B<int> & obj_int) ::
    "<<obj_int.func()<<endl;}

    void call(D<char> & obj_char){
    cout<<"called function :: call(B<char> & obj_char)::
    "<<obj_char.func()<<endl;}


    int main()
    {

    D<char> obj_char;
    D<int> obj_int;

    call(obj_char);
    call(obj_int);

    return 0;

    }
     
    Pallav singh, Feb 21, 2008
    #1
    1. Advertising

  2. On Feb 21, 3:55 pm, Pallav singh <> wrote:
    > Hi
    >
    > I am getting error in compiling this code .......Unable to find error
    > Kindly suggest me something.
    >
    > Thanks
    > Pallav
    >
    > ===================================================================
    >
    > #include<iostream.h>

    You should use <iostream> in place of iostream.h. For cout, you can
    use
    using std::cout;
    >
    > template<typename T>
    > class B
    > {
    > public :
    >
    > T DependentName;
    > int basefield;
    > typedef int X;
    >
    > };
    >
    > template<>
    > class B<char>
    > {
    >
    > public :
    > enum E{basefield = 1};
    > int func()
    > { return basefield;}
    >
    > };
    >
    > template<typename T>
    > class D:B<T>
    > {
    > public :
    > int func()
    > { basefield = 12;

    Two errors in this step (when instantiation of D<char> is done):-
    1)C++ does not permit implicit conversion from int type to enum type.
    That is you cannot write
    enum R{A,B};
    R x = 1;

    2)basefield is an l-value, that is it is an integral constant, a
    lvalue. So you cannot assign
    any thing to it.
    > return basefield; }
    >
    > };
    >
    > void call(D<int> & obj_int){
    > cout<<"called function :: call(B<int> & obj_int) ::
    > "<<obj_int.func()<<endl;}
    >
    > void call(D<char> & obj_char){
    > cout<<"called function :: call(B<char> & obj_char)::
    > "<<obj_char.func()<<endl;}
    >
    > int main()
    > {
    >
    > D<char> obj_char;
    > D<int> obj_int;
    >
    > call(obj_char);
    > call(obj_int);
    >
    > return 0;
    >
    > }


    Regards,
    Reetesh Mukul
     
    Reetesh Mukul, Feb 21, 2008
    #2
    1. Advertising

  3. Pallav singh

    Pallav singh Guest

    On Feb 21, 4:26 pm, Reetesh Mukul <> wrote:
    > On Feb 21, 3:55 pm, Pallav singh <> wrote:> Hi
    >
    > > I am getting error in compiling this code .......Unable to find error
    > > Kindly suggest me something.

    >
    > > Thanks
    > > Pallav

    >
    > > ===================================================================

    >
    > > #include<iostream.h>

    >
    > You should use <iostream> in place of iostream.h. For cout, you can
    > use
    > using std::cout;
    >
    >
    >
    > > template<typename T>
    > > class B
    > > {
    > > public :

    >
    > > T DependentName;
    > > int basefield;
    > > typedef int X;

    >
    > > };

    >
    > > template<>
    > > class B<char>
    > > {

    >
    > > public :
    > > enum E{basefield = 1};
    > > int func()
    > > { return basefield;}

    >
    > > };

    >
    > > template<typename T>
    > > class D:B<T>
    > > {
    > > public :
    > > int func()
    > > { basefield = 12;

    >
    > Two errors in this step (when instantiation of D<char> is done):-
    > 1)C++ does not permit implicit conversion from int type to enum type.
    > That is you cannot write
    > enum R{A,B};
    > R x = 1;
    >
    > 2)basefield is an l-value, that is it is an integral constant, a
    > lvalue. So you cannot assign
    > any thing to it.
    >
    >
    >
    > > return basefield; }

    >
    > > };

    >
    > > void call(D<int> & obj_int){
    > > cout<<"called function :: call(B<int> & obj_int) ::
    > > "<<obj_int.func()<<endl;}

    >
    > > void call(D<char> & obj_char){
    > > cout<<"called function :: call(B<char> & obj_char)::
    > > "<<obj_char.func()<<endl;}

    >
    > > int main()
    > > {

    >
    > > D<char> obj_char;
    > > D<int> obj_int;

    >
    > > call(obj_char);
    > > call(obj_int);

    >
    > > return 0;

    >
    > > }

    >
    > Regards,
    > Reetesh Mukul


    ---------------------------------------------------------------------------------

    Hi

    as my knowledge with char ...it should look to Base class Specialized
    function
    where i am Not doing any assignment but access

    while it should call generalized Base class for Other cases where i
    did assigment

    Thanks
    Pallav
     
    Pallav singh, Feb 21, 2008
    #3
  4. Pallav singh

    Pallav singh Guest

    On Feb 21, 4:36 pm, Pallav singh <> wrote:
    > On Feb 21, 4:26 pm, Reetesh Mukul <> wrote:
    >
    >
    >
    > > On Feb 21, 3:55 pm, Pallav singh <> wrote:> Hi

    >
    > > > I am getting error in compiling this code .......Unable to find error
    > > > Kindly suggest me something.

    >
    > > > Thanks
    > > > Pallav

    >
    > > > ===================================================================

    >
    > > > #include<iostream.h>

    >
    > > You should use <iostream> in place of iostream.h. For cout, you can
    > > use
    > > using std::cout;

    >
    > > > template<typename T>
    > > > class B
    > > > {
    > > > public :

    >
    > > > T DependentName;
    > > > int basefield;
    > > > typedef int X;

    >
    > > > };

    >
    > > > template<>
    > > > class B<char>
    > > > {

    >
    > > > public :
    > > > enum E{basefield = 1};
    > > > int func()
    > > > { return basefield;}

    >
    > > > };

    >
    > > > template<typename T>
    > > > class D:B<T>
    > > > {
    > > > public :
    > > > int func()
    > > > { basefield = 12;

    >
    > > Two errors in this step (when instantiation of D<char> is done):-
    > > 1)C++ does not permit implicit conversion from int type to enum type.
    > > That is you cannot write
    > > enum R{A,B};
    > > R x = 1;

    >
    > > 2)basefield is an l-value, that is it is an integral constant, a
    > > lvalue. So you cannot assign
    > > any thing to it.

    >
    > > > return basefield; }

    >
    > > > };

    >
    > > > void call(D<int> & obj_int){
    > > > cout<<"called function :: call(B<int> & obj_int) ::
    > > > "<<obj_int.func()<<endl;}

    >
    > > > void call(D<char> & obj_char){
    > > > cout<<"called function :: call(B<char> & obj_char)::
    > > > "<<obj_char.func()<<endl;}

    >
    > > > int main()
    > > > {

    >
    > > > D<char> obj_char;
    > > > D<int> obj_int;

    >
    > > > call(obj_char);
    > > > call(obj_int);

    >
    > > > return 0;

    >
    > > > }

    >
    > > Regards,
    > > Reetesh Mukul

    >
    > ---------------------------------------------------------------------------------
    >
    > Hi
    >
    > as my knowledge with char ...it should look to Base class Specialized
    > function
    > where i am Not doing any assignment but access
    >
    > while it should call generalized Base class for Other cases where i
    > did assigment
    >
    > Thanks
    > Pallav


    I am also catching object by reference of derived class ........
     
    Pallav singh, Feb 21, 2008
    #4
  5. On Feb 21, 4:36 pm, Pallav singh <> wrote:
    > On Feb 21, 4:26 pm, Reetesh Mukul <> wrote:
    >
    >
    >
    > > On Feb 21, 3:55 pm, Pallav singh <> wrote:> Hi

    >
    > > > I am getting error in compiling this code .......Unable to find error
    > > > Kindly suggest me something.

    >
    > > > Thanks
    > > > Pallav

    >
    > > > ===================================================================

    >
    > > > #include<iostream.h>

    >
    > > You should use <iostream> in place of iostream.h. For cout, you can
    > > use
    > > using std::cout;

    >
    > > > template<typename T>
    > > > class B
    > > > {
    > > > public :

    >
    > > > T DependentName;
    > > > int basefield;
    > > > typedef int X;

    >
    > > > };

    >
    > > > template<>
    > > > class B<char>
    > > > {

    >
    > > > public :
    > > > enum E{basefield = 1};
    > > > int func()
    > > > { return basefield;}

    >
    > > > };

    >
    > > > template<typename T>
    > > > class D:B<T>
    > > > {
    > > > public :
    > > > int func()
    > > > { basefield = 12;

    >
    > > Two errors in this step (when instantiation of D<char> is done):-
    > > 1)C++ does not permit implicit conversion from int type to enum type.
    > > That is you cannot write
    > > enum R{A,B};
    > > R x = 1;

    >
    > > 2)basefield is an l-value, that is it is an integral constant, a
    > > lvalue. So you cannot assign
    > > any thing to it.

    >
    > > > return basefield; }

    >
    > > > };

    >
    > > > void call(D<int> & obj_int){
    > > > cout<<"called function :: call(B<int> & obj_int) ::
    > > > "<<obj_int.func()<<endl;}

    >
    > > > void call(D<char> & obj_char){
    > > > cout<<"called function :: call(B<char> & obj_char)::
    > > > "<<obj_char.func()<<endl;}

    >
    > > > int main()
    > > > {

    >
    > > > D<char> obj_char;
    > > > D<int> obj_int;

    >
    > > > call(obj_char);
    > > > call(obj_int);

    >
    > > > return 0;

    >
    > > > }

    >
    > > Regards,
    > > Reetesh Mukul

    >
    > ---------------------------------------------------------------------------------
    >
    > Hi
    >
    > as my knowledge with char ...it should look to Base class Specialized
    > function
    > where i am Not doing any assignment but access

    The what about the assignment in D<char>::func() ...
    basefield = 1;
    >
    > while it should call generalized Base class for Other cases where i
    > did assigment
    >
    > Thanks
    > Pallav


    I am not able to get your point, can you explain.

    Regards,
    Reetesh Mukul
     
    Reetesh Mukul, Feb 21, 2008
    #5
  6. Pallav singh

    Pallav singh Guest

    On Feb 21, 4:41 pm, Reetesh Mukul <> wrote:
    > On Feb 21, 4:36 pm, Pallav singh <> wrote:
    >
    > > On Feb 21, 4:26 pm, Reetesh Mukul <> wrote:

    >
    > > > On Feb 21, 3:55 pm, Pallav singh <> wrote:> Hi

    >
    > > > > I am getting error in compiling this code .......Unable to find error
    > > > > Kindly suggest me something.

    >
    > > > > Thanks
    > > > > Pallav

    >
    > > > > ===================================================================

    >
    > > > > #include<iostream.h>

    >
    > > > You should use <iostream> in place of iostream.h. For cout, you can
    > > > use
    > > > using std::cout;

    >
    > > > > template<typename T>
    > > > > class B
    > > > > {
    > > > > public :

    >
    > > > > T DependentName;
    > > > > int basefield;
    > > > > typedef int X;

    >
    > > > > };

    >
    > > > > template<>
    > > > > class B<char>
    > > > > {

    >
    > > > > public :
    > > > > enum E{basefield = 1};
    > > > > int func()
    > > > > { return basefield;}

    >
    > > > > };

    >
    > > > > template<typename T>
    > > > > class D:B<T>
    > > > > {
    > > > > public :
    > > > > int func()
    > > > > { basefield = 12;

    >
    > > > Two errors in this step (when instantiation of D<char> is done):-
    > > > 1)C++ does not permit implicit conversion from int type to enum type.
    > > > That is you cannot write
    > > > enum R{A,B};
    > > > R x = 1;

    >
    > > > 2)basefield is an l-value, that is it is an integral constant, a
    > > > lvalue. So you cannot assign
    > > > any thing to it.

    >
    > > > > return basefield; }

    >
    > > > > };

    >
    > > > > void call(D<int> & obj_int){
    > > > > cout<<"called function :: call(B<int> & obj_int) ::
    > > > > "<<obj_int.func()<<endl;}

    >
    > > > > void call(D<char> & obj_char){
    > > > > cout<<"called function :: call(B<char> & obj_char)::
    > > > > "<<obj_char.func()<<endl;}

    >
    > > > > int main()
    > > > > {

    >
    > > > > D<char> obj_char;
    > > > > D<int> obj_int;

    >
    > > > > call(obj_char);
    > > > > call(obj_int);

    >
    > > > > return 0;

    >
    > > > > }

    >
    > > > Regards,
    > > > Reetesh Mukul

    >
    > > ---------------------------------------------------------------------------------

    >
    > > Hi

    >
    > > as my knowledge with char ...it should look to Base class Specialized
    > > function
    > > where i am Not doing any assignment but access

    >
    > The what about the assignment in D<char>::func() ...
    > basefield = 1;
    >
    >
    >
    > > while it should call generalized Base class for Other cases where i
    > > did assigment

    >
    > > Thanks
    > > Pallav

    >
    > I am not able to get your point, can you explain.
    >
    > Regards,
    > Reetesh Mukul


    --------------------------------------------------------------------------

    thats beauty of Specialization when i call B<char> it will call
    specialized base class for char
    Variable / functionality of specialized class has No relation with
    Generalized Base Class

    when i call B<int> , B<bool> ---------It call generalized Base Class
    while when i call B<char> ----------- It should call my Specialized
    Base Class

    U can see Above in the Code ........ I Have written two class B

    Thanks
    pallav
     
    Pallav singh, Feb 21, 2008
    #6
  7. On Feb 21, 4:47 pm, Pallav singh <> wrote:
    > On Feb 21, 4:41 pm, Reetesh Mukul <> wrote:
    >
    >
    >
    > > On Feb 21, 4:36 pm, Pallav singh <> wrote:

    >
    > > > On Feb 21, 4:26 pm, Reetesh Mukul <> wrote:

    >
    > > > > On Feb 21, 3:55 pm, Pallav singh <> wrote:> Hi

    >
    > > > > > I am getting error in compiling this code .......Unable to find error
    > > > > > Kindly suggest me something.

    >
    > > > > > Thanks
    > > > > > Pallav

    >
    > > > > > ===================================================================

    >
    > > > > > #include<iostream.h>

    >
    > > > > You should use <iostream> in place of iostream.h. For cout, you can
    > > > > use
    > > > > using std::cout;

    >
    > > > > > template<typename T>
    > > > > > class B
    > > > > > {
    > > > > > public :

    >
    > > > > > T DependentName;
    > > > > > int basefield;
    > > > > > typedef int X;

    >
    > > > > > };

    >
    > > > > > template<>
    > > > > > class B<char>
    > > > > > {

    >
    > > > > > public :
    > > > > > enum E{basefield = 1};
    > > > > > int func()
    > > > > > { return basefield;}

    >
    > > > > > };

    >
    > > > > > template<typename T>
    > > > > > class D:B<T>
    > > > > > {
    > > > > > public :
    > > > > > int func()
    > > > > > { basefield = 12;

    >
    > > > > Two errors in this step (when instantiation of D<char> is done):-
    > > > > 1)C++ does not permit implicit conversion from int type to enum type.
    > > > > That is you cannot write
    > > > > enum R{A,B};
    > > > > R x = 1;

    >
    > > > > 2)basefield is an l-value, that is it is an integral constant, a
    > > > > lvalue. So you cannot assign
    > > > > any thing to it.

    >
    > > > > > return basefield; }

    >
    > > > > > };

    >
    > > > > > void call(D<int> & obj_int){
    > > > > > cout<<"called function :: call(B<int> & obj_int) ::
    > > > > > "<<obj_int.func()<<endl;}

    >
    > > > > > void call(D<char> & obj_char){
    > > > > > cout<<"called function :: call(B<char> & obj_char)::
    > > > > > "<<obj_char.func()<<endl;}

    >
    > > > > > int main()
    > > > > > {

    >
    > > > > > D<char> obj_char;
    > > > > > D<int> obj_int;

    >
    > > > > > call(obj_char);
    > > > > > call(obj_int);

    >
    > > > > > return 0;

    >
    > > > > > }

    >
    > > > > Regards,
    > > > > Reetesh Mukul

    >
    > > > ---------------------------------------------------------------------------------

    >
    > > > Hi

    >
    > > > as my knowledge with char ...it should look to Base class Specialized
    > > > function
    > > > where i am Not doing any assignment but access

    >
    > > The what about the assignment in D<char>::func() ...
    > > basefield = 1;

    >
    > > > while it should call generalized Base class for Other cases where i
    > > > did assigment

    >
    > > > Thanks
    > > > Pallav

    >
    > > I am not able to get your point, can you explain.

    >
    > > Regards,
    > > Reetesh Mukul

    >
    > --------------------------------------------------------------------------
    >
    > thats beauty of Specialization when i call B<char> it will call
    > specialized base class for char
    > Variable / functionality of specialized class has No relation with
    > Generalized Base Class
    >
    > when i call B<int> , B<bool> ---------It call generalized Base Class
    > while when i call B<char> ----------- It should call my Specialized
    > Base Class
    >
    > U can see Above in the Code ........ I Have written two class B
    >
    > Thanks
    > pallav


    Have you read my replies ? I think you are not seeing the D<char>
    thing that I have written above.

    Ofcourse that is why it is called specialization.

    Regards,
    Reetesh mukul
     
    Reetesh Mukul, Feb 21, 2008
    #7
  8. Pallav singh

    Pallav singh Guest

    On Feb 21, 4:50 pm, Reetesh Mukul <> wrote:
    > On Feb 21, 4:47 pm, Pallav singh <> wrote:
    >
    >
    >
    > > On Feb 21, 4:41 pm, Reetesh Mukul <> wrote:

    >
    > > > On Feb 21, 4:36 pm, Pallav singh <> wrote:

    >
    > > > > On Feb 21, 4:26 pm, Reetesh Mukul <> wrote:

    >
    > > > > > On Feb 21, 3:55 pm, Pallav singh <> wrote:> Hi

    >
    > > > > > > I am getting error in compiling this code .......Unable to find error
    > > > > > > Kindly suggest me something.

    >
    > > > > > > Thanks
    > > > > > > Pallav

    >
    > > > > > > ===================================================================

    >
    > > > > > > #include<iostream.h>

    >
    > > > > > You should use <iostream> in place of iostream.h. For cout, you can
    > > > > > use
    > > > > > using std::cout;

    >
    > > > > > > template<typename T>
    > > > > > > class B
    > > > > > > {
    > > > > > > public :

    >
    > > > > > > T DependentName;
    > > > > > > int basefield;
    > > > > > > typedef int X;

    >
    > > > > > > };

    >
    > > > > > > template<>
    > > > > > > class B<char>
    > > > > > > {

    >
    > > > > > > public :
    > > > > > > enum E{basefield = 1};
    > > > > > > int func()
    > > > > > > { return basefield;}

    >
    > > > > > > };

    >
    > > > > > > template<typename T>
    > > > > > > class D:B<T>
    > > > > > > {
    > > > > > > public :
    > > > > > > int func()
    > > > > > > { basefield = 12;

    >
    > > > > > Two errors in this step (when instantiation of D<char> is done):-
    > > > > > 1)C++ does not permit implicit conversion from int type to enum type.
    > > > > > That is you cannot write
    > > > > > enum R{A,B};
    > > > > > R x = 1;

    >
    > > > > > 2)basefield is an l-value, that is it is an integral constant, a
    > > > > > lvalue. So you cannot assign
    > > > > > any thing to it.

    >
    > > > > > > return basefield; }

    >
    > > > > > > };

    >
    > > > > > > void call(D<int> & obj_int){
    > > > > > > cout<<"called function :: call(B<int> & obj_int) ::
    > > > > > > "<<obj_int.func()<<endl;}

    >
    > > > > > > void call(D<char> & obj_char){
    > > > > > > cout<<"called function :: call(B<char> & obj_char)::
    > > > > > > "<<obj_char.func()<<endl;}

    >
    > > > > > > int main()
    > > > > > > {

    >
    > > > > > > D<char> obj_char;
    > > > > > > D<int> obj_int;

    >
    > > > > > > call(obj_char);
    > > > > > > call(obj_int);

    >
    > > > > > > return 0;

    >
    > > > > > > }

    >
    > > > > > Regards,
    > > > > > Reetesh Mukul

    >
    > > > > ---------------------------------------------------------------------------------

    >
    > > > > Hi

    >
    > > > > as my knowledge with char ...it should look to Base class Specialized
    > > > > function
    > > > > where i am Not doing any assignment but access

    >
    > > > The what about the assignment in D<char>::func() ...
    > > > basefield = 1;

    >
    > > > > while it should call generalized Base class for Other cases where i
    > > > > did assigment

    >
    > > > > Thanks
    > > > > Pallav

    >
    > > > I am not able to get your point, can you explain.

    >
    > > > Regards,
    > > > Reetesh Mukul

    >
    > > --------------------------------------------------------------------------

    >
    > > thats beauty of Specialization when i call B<char> it will call
    > > specialized base class for char
    > > Variable / functionality of specialized class has No relation with
    > > Generalized Base Class

    >
    > > when i call B<int> , B<bool> ---------It call generalized Base Class
    > > while when i call B<char> ----------- It should call my Specialized
    > > Base Class

    >
    > > U can see Above in the Code ........ I Have written two class B

    >
    > > Thanks
    > > pallav

    >
    > Have you read my replies ? I think you are not seeing the D<char>
    > thing that I have written above.
    >
    > Ofcourse that is why it is called specialization.
    >
    > Regards,
    > Reetesh mukul


    -----------------------------------------------------------------------

    Thanks

    I got while D<char> .........the Func() get Over-Ridding and thats
    Creating Problem

    Any Solution how to handle it in derived

    #include<iostream.h>


    template<typename T>
    class B
    {
    public :

    T DependentName;
    int basefield;
    typedef int X;
    };


    template<>
    class B<char>
    {

    public :
    enum E{basefield = 1};
    int func()
    { return basefield;}
    };

    template<typename T>
    class D1:B<char>
    {
    public :
    int func()
    {B<char>::func();}
    };


    template<typename T>
    class D:B<T>
    {
    public :
    int func()
    { basefield = 12;
    return basefield; }
    };


    void call(D<int> & obj_int){
    cout<<"called function :: call(B<int> & obj_int) ::
    "<<obj_int.func()<<endl;}

    void call(D1<char> & obj_char){
    cout<<"called function :: call(B<char> & obj_char)::
    "<<obj_char.func()<<endl;}


    int main()
    {

    D1<char> obj_char;
    D<int> obj_int;

    call(obj_char);
    call(obj_int);

    return 0;

    }
     
    Pallav singh, Feb 21, 2008
    #8
    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.
Similar Threads
  1. Dave
    Replies:
    4
    Views:
    7,708
    pdixtl
    Jun 4, 2010
  2. pit3k
    Replies:
    8
    Views:
    376
    Victor Bazarov
    Feb 9, 2005
  3. Ruben Campos
    Replies:
    3
    Views:
    6,491
  4. Joseph Turian
    Replies:
    2
    Views:
    480
  5. Pallav singh
    Replies:
    1
    Views:
    317
    Victor Bazarov
    Jan 6, 2012
Loading...

Share This Page