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>:
perator =' : unable to match function
definition to an existing declaration
definition
'Test_A<nametype,traits_1>
&Test_A<nametype,traits_1>:
perator =(Test_B<nametype,traits_2> &)'
existing declarations
'Test_A<nametype,traits_1>
&Test_A<nametype,traits_1>:
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 >:
rint() {
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 >:
rint() {
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;
}
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>:
definition to an existing declaration
definition
'Test_A<nametype,traits_1>
&Test_A<nametype,traits_1>:
existing declarations
'Test_A<nametype,traits_1>
&Test_A<nametype,traits_1>:
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 >:
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 >:
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;
}