Use Assignment Operator On Both Types

Discussion in 'C++' started by Immortal Nephi, Aug 24, 2010.

  1. I want to copy data from type 1 to another type 2. I insert Test_B’s
    assignment operator function into Test_A’s class body. The C++
    Compiler is able to compile successfully without errors.
    If I move assignment operator function on the file scope outside
    class body, C++ Compiler complains to report that this function is
    unable to match another one and both of them look identical.

    The error report shows here:

    Compiling...
    Test_MyCode.cpp
    c:\ test_mycode.cpp(167) : error C2244:
    'Test_A<nametype,traits_1>::eek:perator =' : unable to match function
    definition to an existing declaration
    definition
    'Test_A<nametype,traits_1>
    &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'
    existing declarations
    'Test_A<nametype,traits_1>
    &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'

    You may discover that there are two template definitions in one
    function. Look at assignment operator function.
    Do you know how to fix that error?

    enum NameTypes {
    A_Type,
    B_Type
    };

    template< NameTypes >
    struct Traits_1 {
    };

    template<>
    struct Traits_1< A_Type > {
    static const unsigned char name_1;
    static const unsigned char name_2;
    };

    template<>
    struct Traits_1< B_Type > {
    static const unsigned short name_1;
    static const unsigned short name_2;
    };


    template< NameTypes >
    struct Traits_2 {
    };

    template<>
    struct Traits_2< A_Type > {
    static const unsigned long data_1;
    static const unsigned long data_2;
    };

    template<>
    struct Traits_2< B_Type > {
    static const unsigned long long data_1;
    static const unsigned long long data_2;
    };


    template< NameTypes nametype, typename traits_2 >
    class Test_B;


    template< NameTypes nametype, typename traits_1 >
    class Test_A {
    public:
    Test_A();
    ~Test_A();

    void print();


    template< NameTypes nametype, typename traits_2 >
    Test_A& operator =( Test_B< nametype, traits_2 >& test_b ); /* {
    this->x = test_b.x;
    this->y = test_b.y;

    this->u = traits_2::data_1;
    this->v = traits_2::data_2;

    cout << "u: " << hex << "0x" << +u << endl;
    cout << "v: " << hex << "0x" << +v << endl;
    cout << "x: " << hex << "0x" << +x << endl;
    cout << "y: " << hex << "0x" << +y << endl;

    return *this;
    } */


    unsigned long u;
    unsigned long v;
    unsigned long x;
    unsigned long y;
    };


    template< NameTypes nametype, typename traits_2 >
    class Test_B {
    public:
    Test_B();
    ~Test_B();

    void print();

    unsigned long x;
    unsigned long y;
    };



    const unsigned char Traits_1< A_Type >::name_1 = 0x10;
    const unsigned char Traits_1< A_Type >::name_2 = 0x23;

    const unsigned short Traits_1< B_Type >::name_1 = 0x45;
    const unsigned short Traits_1< B_Type >::name_2 = 0x7C;

    const unsigned long Traits_2< A_Type >::data_1 = 0xFA;
    const unsigned long Traits_2< A_Type >::data_2 = 0xEB;

    const unsigned long long Traits_2< B_Type >::data_1 = 0x35;
    const unsigned long long Traits_2< B_Type >::data_2 = 0x92;


    template< NameTypes nametype, typename traits_1 >
    Test_A< nametype, traits_1 >::Test_A() : u( 0 ), v( 0 ), x( 0 ),
    y( 0 ) {
    }

    template< NameTypes nametype, typename traits_1 >
    Test_A< nametype, traits_1 >::~Test_A() {
    }

    template< NameTypes nametype, typename traits_1 >
    void Test_A< nametype, traits_1 >::print() {
    cout << "name_1: " << hex << "0x" << +traits_1::name_1 << endl;
    cout << "name_2: " << hex << "0x" << +traits_1::name_2 << endl;
    }

    // Notice two templates?
    template< NameTypes nametype, typename traits_1 >
    template< NameTypes nametype, typename traits_2 >
    Test_A< nametype, traits_1 >&
    Test_A< nametype, traits_1 >::
    operator =( Test_B< nametype, traits_2 >& test_b ) {
    this->x = test_b.x;
    this->y = test_b.y;

    this->u = traits_2::data_1;
    this->v = traits_2::data_2;

    cout << "u: " << hex << "0x" << +u << endl;
    cout << "v: " << hex << "0x" << +v << endl;
    cout << "x: " << hex << "0x" << +x << endl;
    cout << "y: " << hex << "0x" << +y << endl;

    return *this;
    }


    template< NameTypes nametype, typename traits_2 >
    Test_B< nametype, traits_2 >::Test_B() : x( 0xFE ), y( 0xFC ) {
    }

    template< NameTypes nametype, typename traits_2 >
    Test_B< nametype, traits_2 >::~Test_B() {
    }

    template< NameTypes nametype, typename traits_2 >
    void Test_B< nametype, traits_2 >::print() {
    cout << "name_1: " << hex << "0x" << +traits_2::data_1 << endl;
    cout << "name_2: " << hex << "0x" << +traits_2::data_2 << endl;
    }


    int main() {
    Test_A< A_Type, Traits_1< A_Type > > a;
    Test_B< A_Type, Traits_2< A_Type > > b;

    a.print();
    b.print();

    a = b;

    return 0;
    }
    Immortal Nephi, Aug 24, 2010
    #1
    1. Advertising

  2. Immortal Nephi

    Pavel Guest

    Immortal Nephi wrote:
    > I want to copy data from type 1 to another type 2. I insert Test_B’s
    > assignment operator function into Test_A’s class body. The C++
    > Compiler is able to compile successfully without errors.
    > If I move assignment operator function on the file scope outside
    > class body, C++ Compiler complains to report that this function is
    > unable to match another one and both of them look identical.
    >
    > The error report shows here:
    >
    > Compiling...
    > Test_MyCode.cpp
    > c:\ test_mycode.cpp(167) : error C2244:
    > 'Test_A<nametype,traits_1>::eek:perator =' : unable to match function
    > definition to an existing declaration
    > definition
    > 'Test_A<nametype,traits_1>
    > &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'
    > existing declarations
    > 'Test_A<nametype,traits_1>
    > &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'
    >
    > You may discover that there are two template definitions in one
    > function. Look at assignment operator function.
    > Do you know how to fix that error?
    >
    > enum NameTypes {
    > A_Type,
    > B_Type
    > };
    >
    > template< NameTypes>
    > struct Traits_1 {
    > };
    >
    > template<>
    > struct Traits_1< A_Type> {
    > static const unsigned char name_1;
    > static const unsigned char name_2;
    > };
    >
    > template<>
    > struct Traits_1< B_Type> {
    > static const unsigned short name_1;
    > static const unsigned short name_2;
    > };
    >
    >
    > template< NameTypes>
    > struct Traits_2 {
    > };
    >
    > template<>
    > struct Traits_2< A_Type> {
    > static const unsigned long data_1;
    > static const unsigned long data_2;
    > };
    >
    > template<>
    > struct Traits_2< B_Type> {
    > static const unsigned long long data_1;
    > static const unsigned long long data_2;
    > };
    >
    >
    > template< NameTypes nametype, typename traits_2>
    > class Test_B;
    >
    >
    > template< NameTypes nametype, typename traits_1>
    > class Test_A {
    > public:
    > Test_A();
    > ~Test_A();
    >
    > void print();
    >
    >
    > template< NameTypes nametype, typename traits_2>
    > Test_A& operator =( Test_B< nametype, traits_2>& test_b ); /* {
    > this->x = test_b.x;
    > this->y = test_b.y;
    >
    > this->u = traits_2::data_1;
    > this->v = traits_2::data_2;
    >
    > cout<< "u: "<< hex<< "0x"<< +u<< endl;
    > cout<< "v: "<< hex<< "0x"<< +v<< endl;
    > cout<< "x: "<< hex<< "0x"<< +x<< endl;
    > cout<< "y: "<< hex<< "0x"<< +y<< endl;
    >
    > return *this;
    > } */
    >
    >
    > unsigned long u;
    > unsigned long v;
    > unsigned long x;
    > unsigned long y;
    > };
    >
    >
    > template< NameTypes nametype, typename traits_2>
    > class Test_B {
    > public:
    > Test_B();
    > ~Test_B();
    >
    > void print();
    >
    > unsigned long x;
    > unsigned long y;
    > };
    >
    >
    >
    > const unsigned char Traits_1< A_Type>::name_1 = 0x10;
    > const unsigned char Traits_1< A_Type>::name_2 = 0x23;
    >
    > const unsigned short Traits_1< B_Type>::name_1 = 0x45;
    > const unsigned short Traits_1< B_Type>::name_2 = 0x7C;
    >
    > const unsigned long Traits_2< A_Type>::data_1 = 0xFA;
    > const unsigned long Traits_2< A_Type>::data_2 = 0xEB;
    >
    > const unsigned long long Traits_2< B_Type>::data_1 = 0x35;
    > const unsigned long long Traits_2< B_Type>::data_2 = 0x92;
    >
    >
    > template< NameTypes nametype, typename traits_1>
    > Test_A< nametype, traits_1>::Test_A() : u( 0 ), v( 0 ), x( 0 ),
    > y( 0 ) {
    > }
    >
    > template< NameTypes nametype, typename traits_1>
    > Test_A< nametype, traits_1>::~Test_A() {
    > }
    >
    > template< NameTypes nametype, typename traits_1>
    > void Test_A< nametype, traits_1>::print() {
    > cout<< "name_1: "<< hex<< "0x"<< +traits_1::name_1<< endl;
    > cout<< "name_2: "<< hex<< "0x"<< +traits_1::name_2<< endl;
    > }
    >
    > // Notice two templates?
    > template< NameTypes nametype, typename traits_1>
    > template< NameTypes nametype, typename traits_2>
    > Test_A< nametype, traits_1>&
    > Test_A< nametype, traits_1>::
    > operator =( Test_B< nametype, traits_2>& test_b ) {
    > this->x = test_b.x;
    > this->y = test_b.y;
    >
    > this->u = traits_2::data_1;
    > this->v = traits_2::data_2;
    >
    > cout<< "u: "<< hex<< "0x"<< +u<< endl;
    > cout<< "v: "<< hex<< "0x"<< +v<< endl;
    > cout<< "x: "<< hex<< "0x"<< +x<< endl;
    > cout<< "y: "<< hex<< "0x"<< +y<< endl;
    >
    > return *this;
    > }
    >
    >
    > template< NameTypes nametype, typename traits_2>
    > Test_B< nametype, traits_2>::Test_B() : x( 0xFE ), y( 0xFC ) {
    > }
    >
    > template< NameTypes nametype, typename traits_2>
    > Test_B< nametype, traits_2>::~Test_B() {
    > }
    >
    > template< NameTypes nametype, typename traits_2>
    > void Test_B< nametype, traits_2>::print() {
    > cout<< "name_1: "<< hex<< "0x"<< +traits_2::data_1<< endl;
    > cout<< "name_2: "<< hex<< "0x"<< +traits_2::data_2<< endl;
    > }
    >
    >
    > int main() {
    > Test_A< A_Type, Traits_1< A_Type> > a;
    > Test_B< A_Type, Traits_2< A_Type> > b;
    >
    > a.print();
    > b.print();
    >
    > a = b;
    >
    > return 0;
    > }

    You need to rename one of your 'nametype' template parameters to
    something else.

    Hope this will help,
    -Pavel
    Pavel, Aug 24, 2010
    #2
    1. Advertising

  3. On Aug 23, 8:53 pm, Pavel
    <> wrote:
    > Immortal Nephi wrote:
    > >    I want to copy data from type 1 to another type 2.  I insert Test_B’s
    > > assignment operator function into Test_A’s class body.  The C++
    > > Compiler is able to compile successfully without errors.
    > >    If I move assignment operator function on the file scope outside
    > > class body, C++ Compiler complains to report that this function is
    > > unable to match another one and both of them look identical.

    >
    > >    The error report shows here:

    >
    > > Compiling...
    > > Test_MyCode.cpp
    > > c:\ test_mycode.cpp(167) : error C2244:
    > > 'Test_A<nametype,traits_1>::eek:perator =' : unable to match function
    > > definition to an existing declaration
    > >          definition
    > >          'Test_A<nametype,traits_1>
    > > &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2>  &)'
    > >          existing declarations
    > >          'Test_A<nametype,traits_1>
    > > &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2>  &)'

    >
    > >    You may discover that there are two template definitions in one
    > > function.  Look at assignment operator function.
    > >    Do you know how to fix that error?

    >
    > > enum NameTypes {
    > >    A_Type,
    > >    B_Type
    > > };

    >
    > > template<  NameTypes>
    > > struct Traits_1 {
    > > };

    >
    > > template<>
    > > struct Traits_1<  A_Type>  {
    > >    static const unsigned char name_1;
    > >    static const unsigned char name_2;
    > > };

    >
    > > template<>
    > > struct Traits_1<  B_Type>  {
    > >    static const unsigned short name_1;
    > >    static const unsigned short name_2;
    > > };

    >
    > > template<  NameTypes>
    > > struct Traits_2 {
    > > };

    >
    > > template<>
    > > struct Traits_2<  A_Type>  {
    > >    static const unsigned long data_1;
    > >    static const unsigned long data_2;
    > > };

    >
    > > template<>
    > > struct Traits_2<  B_Type>  {
    > >    static const unsigned long long data_1;
    > >    static const unsigned long long data_2;
    > > };

    >
    > > template<  NameTypes nametype, typename traits_2>
    > > class Test_B;

    >
    > > template<  NameTypes nametype, typename traits_1>
    > > class Test_A {
    > > public:
    > >    Test_A();
    > >    ~Test_A();

    >
    > >    void print();

    >
    > >    template<  NameTypes nametype, typename traits_2>
    > >    Test_A&  operator =( Test_B<  nametype, traits_2>&  test_b ); /* {
    > >            this->x = test_b.x;
    > >            this->y = test_b.y;

    >
    > >            this->u = traits_2::data_1;
    > >            this->v = traits_2::data_2;

    >
    > >            cout<<  "u: "<<  hex<<  "0x"<<  +u<<  endl;
    > >            cout<<  "v: "<<  hex<<  "0x"<<  +v<<  endl;
    > >            cout<<  "x: "<<  hex<<  "0x"<<  +x<<  endl;
    > >            cout<<  "y: "<<  hex<<  "0x"<<  +y<<  endl;

    >
    > >            return *this;
    > >    } */

    >
    > >    unsigned long u;
    > >    unsigned long v;
    > >    unsigned long x;
    > >    unsigned long y;
    > > };

    >
    > > template<  NameTypes nametype, typename traits_2>
    > > class Test_B {
    > > public:
    > >    Test_B();
    > >    ~Test_B();

    >
    > >    void print();

    >
    > >    unsigned long x;
    > >    unsigned long y;
    > > };

    >
    > > const unsigned char Traits_1<  A_Type>::name_1 = 0x10;
    > > const unsigned char Traits_1<  A_Type>::name_2 = 0x23;

    >
    > > const unsigned short Traits_1<  B_Type>::name_1 = 0x45;
    > > const unsigned short Traits_1<  B_Type>::name_2 = 0x7C;

    >
    > > const unsigned long Traits_2<  A_Type>::data_1 = 0xFA;
    > > const unsigned long Traits_2<  A_Type>::data_2 = 0xEB;

    >
    > > const unsigned long long Traits_2<  B_Type>::data_1 = 0x35;
    > > const unsigned long long Traits_2<  B_Type>::data_2 = 0x92;

    >
    > > template<  NameTypes nametype, typename traits_1>
    > > Test_A<  nametype, traits_1>::Test_A() : u( 0 ), v( 0 ), x( 0 ),
    > > y( 0 ) {
    > > }

    >
    > > template<  NameTypes nametype, typename traits_1>
    > > Test_A<  nametype, traits_1>::~Test_A() {
    > > }

    >
    > > template<  NameTypes nametype, typename traits_1>
    > > void Test_A<  nametype, traits_1>::print() {
    > >    cout<<  "name_1: "<<  hex<<  "0x"<<  +traits_1::name_1<<  endl;
    > >    cout<<  "name_2: "<<  hex<<  "0x"<<  +traits_1::name_2<<  endl;
    > > }

    >
    > > // Notice two templates?
    > > template<  NameTypes nametype, typename traits_1>
    > > template<  NameTypes nametype, typename traits_2>
    > > Test_A<  nametype, traits_1>&
    > > Test_A<  nametype, traits_1>::
    > > operator =( Test_B<  nametype, traits_2>&  test_b ) {
    > >    this->x = test_b.x;
    > >    this->y = test_b.y;

    >
    > >    this->u = traits_2::data_1;
    > >    this->v = traits_2::data_2;

    >
    > >    cout<<  "u: "<<  hex<<  "0x"<<  +u<<  endl;
    > >    cout<<  "v: "<<  hex<<  "0x"<<  +v<<  endl;
    > >    cout<<  "x: "<<  hex<<  "0x"<<  +x<<  endl;
    > >    cout<<  "y: "<<  hex<<  "0x"<<  +y<<  endl;

    >
    > >    return *this;
    > > }

    >
    > > template<  NameTypes nametype, typename traits_2>
    > > Test_B<  nametype, traits_2>::Test_B() : x( 0xFE ), y( 0xFC ) {
    > > }

    >
    > > template<  NameTypes nametype, typename traits_2>
    > > Test_B<  nametype, traits_2>::~Test_B() {
    > > }

    >
    > > template<  NameTypes nametype, typename traits_2>
    > > void Test_B<  nametype, traits_2>::print() {
    > >    cout<<  "name_1: "<<  hex<<  "0x"<<  +traits_2::data_1<<  endl;
    > >    cout<<  "name_2: "<<  hex<<  "0x"<<  +traits_2::data_2<<  endl;
    > > }

    >
    > > int main() {
    > >    Test_A<  A_Type, Traits_1<  A_Type>  >  a;
    > >    Test_B<  A_Type, Traits_2<  A_Type>  >  b;

    >
    > >    a.print();
    > >    b.print();

    >
    > >    a = b;

    >
    > >    return 0;
    > > }

    >
    > You need to rename one of your 'nametype' template parameters to
    > something else.
    >
    > Hope this will help,
    > -Pavel- Hide quoted text -
    >
    > - Show quoted text -


    You did not explain very clear how you say rename ‘nametype’. I
    think you are referring two separate enum types? One ‘NameTypes’ is
    for Test_A class and another ‘NameTypes2’ is for Test_B.
    Is it what you mean? Will C++ Compiler be able to compile if there
    are two enum types.
    Immortal Nephi, Aug 24, 2010
    #3
  4. Immortal Nephi

    tni Guest

    On 2010-08-24 1:27, Immortal Nephi wrote:
    > I want to copy data from type 1 to another type 2. I insert Test_B’s
    > assignment operator function into Test_A’s class body. The C++
    > Compiler is able to compile successfully without errors.
    > If I move assignment operator function on the file scope outside
    > class body, C++ Compiler complains to report that this function is
    > unable to match another one and both of them look identical.
    >
    > The error report shows here:
    >
    > Compiling...
    > Test_MyCode.cpp
    > c:\ test_mycode.cpp(167) : error C2244:
    > 'Test_A<nametype,traits_1>::eek:perator =' : unable to match function
    > definition to an existing declaration
    > definition
    > 'Test_A<nametype,traits_1>
    > &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'
    > existing declarations
    > 'Test_A<nametype,traits_1>
    > &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'
    >
    >
    > [...]
    > // Notice two templates?
    > template< NameTypes nametype, typename traits_1>
    > template< NameTypes nametype, typename traits_2>
    > Test_A< nametype, traits_1>&
    > Test_A< nametype, traits_1>::
    > operator =( Test_B< nametype, traits_2>& test_b ) {
    > [...]


    You want unique template parameters, e.g.:

    template< NameTypes nametype1, typename traits_1 >
    template< NameTypes nametype2, typename traits_2 >
    Test_A< nametype1, traits_1 >&
    Test_A< nametype1, traits_1 >::
    operator =( Test_B< nametype2, traits_2 >& test_b ) {
    tni, Aug 24, 2010
    #4
  5. Immortal Nephi

    Pavel Guest

    Immortal Nephi wrote:
    > On Aug 23, 8:53 pm, Pavel
    > <> wrote:
    >> Immortal Nephi wrote:
    >>> I want to copy data from type 1 to another type 2. I insert Test_B’s
    >>> assignment operator function into Test_A’s class body. The C++
    >>> Compiler is able to compile successfully without errors.
    >>> If I move assignment operator function on the file scope outside
    >>> class body, C++ Compiler complains to report that this function is
    >>> unable to match another one and both of them look identical.

    >>
    >>> The error report shows here:

    >>
    >>> Compiling...
    >>> Test_MyCode.cpp
    >>> c:\ test_mycode.cpp(167) : error C2244:
    >>> 'Test_A<nametype,traits_1>::eek:perator =' : unable to match function
    >>> definition to an existing declaration
    >>> definition
    >>> 'Test_A<nametype,traits_1>
    >>> &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'
    >>> existing declarations
    >>> 'Test_A<nametype,traits_1>
    >>> &Test_A<nametype,traits_1>::eek:perator =(Test_B<nametype,traits_2> &)'

    >>
    >>> You may discover that there are two template definitions in one
    >>> function. Look at assignment operator function.
    >>> Do you know how to fix that error?

    >>
    >>> enum NameTypes {
    >>> A_Type,
    >>> B_Type
    >>> };

    >>
    >>> template< NameTypes>
    >>> struct Traits_1 {
    >>> };

    >>
    >>> template<>
    >>> struct Traits_1< A_Type> {
    >>> static const unsigned char name_1;
    >>> static const unsigned char name_2;
    >>> };

    >>
    >>> template<>
    >>> struct Traits_1< B_Type> {
    >>> static const unsigned short name_1;
    >>> static const unsigned short name_2;
    >>> };

    >>
    >>> template< NameTypes>
    >>> struct Traits_2 {
    >>> };

    >>
    >>> template<>
    >>> struct Traits_2< A_Type> {
    >>> static const unsigned long data_1;
    >>> static const unsigned long data_2;
    >>> };

    >>
    >>> template<>
    >>> struct Traits_2< B_Type> {
    >>> static const unsigned long long data_1;
    >>> static const unsigned long long data_2;
    >>> };

    >>
    >>> template< NameTypes nametype, typename traits_2>
    >>> class Test_B;

    >>
    >>> template< NameTypes nametype, typename traits_1>
    >>> class Test_A {
    >>> public:
    >>> Test_A();
    >>> ~Test_A();

    >>
    >>> void print();

    >>
    >>> template< NameTypes nametype, typename traits_2>
    >>> Test_A& operator =( Test_B< nametype, traits_2>& test_b ); /* {
    >>> this->x = test_b.x;
    >>> this->y = test_b.y;

    >>
    >>> this->u = traits_2::data_1;
    >>> this->v = traits_2::data_2;

    >>
    >>> cout<< "u: "<< hex<< "0x"<< +u<< endl;
    >>> cout<< "v: "<< hex<< "0x"<< +v<< endl;
    >>> cout<< "x: "<< hex<< "0x"<< +x<< endl;
    >>> cout<< "y: "<< hex<< "0x"<< +y<< endl;

    >>
    >>> return *this;
    >>> } */

    >>
    >>> unsigned long u;
    >>> unsigned long v;
    >>> unsigned long x;
    >>> unsigned long y;
    >>> };

    >>
    >>> template< NameTypes nametype, typename traits_2>
    >>> class Test_B {
    >>> public:
    >>> Test_B();
    >>> ~Test_B();

    >>
    >>> void print();

    >>
    >>> unsigned long x;
    >>> unsigned long y;
    >>> };

    >>
    >>> const unsigned char Traits_1< A_Type>::name_1 = 0x10;
    >>> const unsigned char Traits_1< A_Type>::name_2 = 0x23;

    >>
    >>> const unsigned short Traits_1< B_Type>::name_1 = 0x45;
    >>> const unsigned short Traits_1< B_Type>::name_2 = 0x7C;

    >>
    >>> const unsigned long Traits_2< A_Type>::data_1 = 0xFA;
    >>> const unsigned long Traits_2< A_Type>::data_2 = 0xEB;

    >>
    >>> const unsigned long long Traits_2< B_Type>::data_1 = 0x35;
    >>> const unsigned long long Traits_2< B_Type>::data_2 = 0x92;

    >>
    >>> template< NameTypes nametype, typename traits_1>
    >>> Test_A< nametype, traits_1>::Test_A() : u( 0 ), v( 0 ), x( 0 ),
    >>> y( 0 ) {
    >>> }

    >>
    >>> template< NameTypes nametype, typename traits_1>
    >>> Test_A< nametype, traits_1>::~Test_A() {
    >>> }

    >>
    >>> template< NameTypes nametype, typename traits_1>
    >>> void Test_A< nametype, traits_1>::print() {
    >>> cout<< "name_1: "<< hex<< "0x"<< +traits_1::name_1<< endl;
    >>> cout<< "name_2: "<< hex<< "0x"<< +traits_1::name_2<< endl;
    >>> }

    >>
    >>> // Notice two templates?
    >>> template< NameTypes nametype, typename traits_1>
    >>> template< NameTypes nametype, typename traits_2>
    >>> Test_A< nametype, traits_1>&
    >>> Test_A< nametype, traits_1>::
    >>> operator =( Test_B< nametype, traits_2>& test_b ) {
    >>> this->x = test_b.x;
    >>> this->y = test_b.y;

    >>
    >>> this->u = traits_2::data_1;
    >>> this->v = traits_2::data_2;

    >>
    >>> cout<< "u: "<< hex<< "0x"<< +u<< endl;
    >>> cout<< "v: "<< hex<< "0x"<< +v<< endl;
    >>> cout<< "x: "<< hex<< "0x"<< +x<< endl;
    >>> cout<< "y: "<< hex<< "0x"<< +y<< endl;

    >>
    >>> return *this;
    >>> }

    >>
    >>> template< NameTypes nametype, typename traits_2>
    >>> Test_B< nametype, traits_2>::Test_B() : x( 0xFE ), y( 0xFC ) {
    >>> }

    >>
    >>> template< NameTypes nametype, typename traits_2>
    >>> Test_B< nametype, traits_2>::~Test_B() {
    >>> }

    >>
    >>> template< NameTypes nametype, typename traits_2>
    >>> void Test_B< nametype, traits_2>::print() {
    >>> cout<< "name_1: "<< hex<< "0x"<< +traits_2::data_1<< endl;
    >>> cout<< "name_2: "<< hex<< "0x"<< +traits_2::data_2<< endl;
    >>> }

    >>
    >>> int main() {
    >>> Test_A< A_Type, Traits_1< A_Type> > a;
    >>> Test_B< A_Type, Traits_2< A_Type> > b;

    >>
    >>> a.print();
    >>> b.print();

    >>
    >>> a = b;

    >>
    >>> return 0;
    >>> }

    >>
    >> You need to rename one of your 'nametype' template parameters to
    >> something else.
    >>
    >> Hope this will help,
    >> -Pavel- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > You did not explain very clear how you say rename ‘nametype’. I
    > think you are referring two separate enum types? One ‘NameTypes’ is
    > for Test_A class and another ‘NameTypes2’ is for Test_B.
    > Is it what you mean? Will C++ Compiler be able to compile if there
    > are two enum types.

    Yes. (as in the example of tni). g++ error message actually gives a
    clue. So another little common-wisdom advice is to try another compiler
    if one gives a not-too-useful error message (just for this purpose; I am
    not saying one is better than another in general; but two are sometimes
    better than one).

    -Pavel
    Pavel, Aug 28, 2010
    #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.
Similar Threads
  1. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    479
    Barry Schwarz
    Feb 8, 2005
  2. Chris
    Replies:
    34
    Views:
    1,488
  3. ABCL
    Replies:
    0
    Views:
    527
  4. Hicham Mouline
    Replies:
    2
    Views:
    681
    Juha Nieminen
    Sep 1, 2009
  5. Victor Hooi
    Replies:
    7
    Views:
    127
    Mitya Sirenef
    Mar 29, 2013
Loading...

Share This Page