N
Nephi Immortal
I am trying to figure out why C++ Compiler generates an error message
as saying “3 overloads have similar conversions” It will compile
without any problem if I comment casting operator function. I still
need to use casting operator function and add operator function.
class A {
public:
A( unsigned int data ) : m_data( data ) {
}
~A() {
}
A( const A& r ) : m_data( r.m_data ) {
}
const A& operator=( const A& r ) {
m_data = r.m_data;
return *this;
}
const A& operator=( const unsigned int data ) {
m_data = data;
return *this;
}
// Compile without error if you comment below.
operator unsigned int() {
return m_data;
}
A operator+( const A& r ) {
A t( m_data );
t.m_data += r.m_data;
return t;
}
A operator+( const unsigned int data ) {
A t( m_data );
t.m_data += data;
return t;
}
A &operator+=( const A& r ) {
m_data += r.m_data;
return *this;
}
A &operator+=( const unsigned int data ) {
m_data += data;
return *this;
}
private:
unsigned int m_data;
};
int main () {
unsigned int x = 1, y = 2;
A d( x ), d2( y );
d = d + d2; // OK
d += d2; // OK
unsigned int data = d; // OK
d = d + 7; // ERROR
d += 7; // OK
return 0;
} // end function main
------ Build started: Project: MPU_65xx, Configuration: Debug Win32
------
Main.cpp
c:\ main.cpp(216): error C2666: 'A:perator +' : 3 overloads have
similar conversions
c:\main.cpp(46): could be 'A A:perator +(const unsigned
int)'
c:\main.cpp(41): or 'A &A:perator +(const A &)'
or 'built-in C++ operator+(unsigned int, int)'
while trying to match the argument list '(A, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
as saying “3 overloads have similar conversions” It will compile
without any problem if I comment casting operator function. I still
need to use casting operator function and add operator function.
class A {
public:
A( unsigned int data ) : m_data( data ) {
}
~A() {
}
A( const A& r ) : m_data( r.m_data ) {
}
const A& operator=( const A& r ) {
m_data = r.m_data;
return *this;
}
const A& operator=( const unsigned int data ) {
m_data = data;
return *this;
}
// Compile without error if you comment below.
operator unsigned int() {
return m_data;
}
A operator+( const A& r ) {
A t( m_data );
t.m_data += r.m_data;
return t;
}
A operator+( const unsigned int data ) {
A t( m_data );
t.m_data += data;
return t;
}
A &operator+=( const A& r ) {
m_data += r.m_data;
return *this;
}
A &operator+=( const unsigned int data ) {
m_data += data;
return *this;
}
private:
unsigned int m_data;
};
int main () {
unsigned int x = 1, y = 2;
A d( x ), d2( y );
d = d + d2; // OK
d += d2; // OK
unsigned int data = d; // OK
d = d + 7; // ERROR
d += 7; // OK
return 0;
} // end function main
------ Build started: Project: MPU_65xx, Configuration: Debug Win32
------
Main.cpp
c:\ main.cpp(216): error C2666: 'A:perator +' : 3 overloads have
similar conversions
c:\main.cpp(46): could be 'A A:perator +(const unsigned
int)'
c:\main.cpp(41): or 'A &A:perator +(const A &)'
or 'built-in C++ operator+(unsigned int, int)'
while trying to match the argument list '(A, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========