Use Assignment Operator On Both Types

I

Immortal Nephi

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

Pavel

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

Immortal Nephi

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

tni

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 ) {
 
P

Pavel

Immortal said:
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
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top