X
Xiangliang Meng
The fragment code:
#include <iostream>
using namespace std;
class Integer {
int i;
public:
Integer() : i(0) {};
Integer(int I) : i(I) {};
Integer(Integer & other) : i(other.i) {} // line 10
Integer & operator=(Integer &other) {
i = other.i;
return (* this);
}
Integer & operator=(int a) {
i = a;
return (* this);
}
friend ostream& operator<<(ostream &os, Integer &I) {
os << I.i;
return os;
}
};
int main() {
int a = 5;
Integer Int1 = a; // line 27; error happens here.
Integer Int2 = Int1; // line 28;
cout << Int1 <<", " << Int2 << endl;
Int1 = 5; // line 31
cout << Int1 <<", " << Int2 << endl;
Int2 = Int1; // line 33
cout << Int1 <<", " << Int2 << endl;
}
case 1: If the line 10 is there, compiling aborts at line 27.
g++.exe "Integer.cpp" -o
Integer.exe" -g3 -I"d:\Dev-Cpp\include\c++" -I"d:\Dev-Cpp\include\c++\mi
ngw32" -I"d:\Dev-Cpp\include\c++\backward" -I"d:\Dev-Cpp\include" -L"d:\
Dev-Cpp\lib"
Integer.cpp: In function `int main()':
Integer.cpp:27: no matching function for call to `Integer::Integer(Integer)'
Integer.cpp:10: candidates are: Integer::Integer(Integer&)
Integer.cpp:9: Integer::Integer(int)
Integer.cpp:27: initializing temporary from result of `
case 2: If the line 10 is delete, there is no error at line 27 when
compiling.
Could someone explain this for me?
What does line 27 do in detail? Invoke Integer::Integer(int I) directly? or
Create a temporary Integer object from the variable 'a' and use
copy-constructor to create Int1 from this termporary object?
Could someone explain more in details on line 27, 28, 31 and 33? What's the
difference among them?
Thanks a lot.
Best Regards,
Xiangliang Meng
#include <iostream>
using namespace std;
class Integer {
int i;
public:
Integer() : i(0) {};
Integer(int I) : i(I) {};
Integer(Integer & other) : i(other.i) {} // line 10
Integer & operator=(Integer &other) {
i = other.i;
return (* this);
}
Integer & operator=(int a) {
i = a;
return (* this);
}
friend ostream& operator<<(ostream &os, Integer &I) {
os << I.i;
return os;
}
};
int main() {
int a = 5;
Integer Int1 = a; // line 27; error happens here.
Integer Int2 = Int1; // line 28;
cout << Int1 <<", " << Int2 << endl;
Int1 = 5; // line 31
cout << Int1 <<", " << Int2 << endl;
Int2 = Int1; // line 33
cout << Int1 <<", " << Int2 << endl;
}
case 1: If the line 10 is there, compiling aborts at line 27.
g++.exe "Integer.cpp" -o
Integer.exe" -g3 -I"d:\Dev-Cpp\include\c++" -I"d:\Dev-Cpp\include\c++\mi
ngw32" -I"d:\Dev-Cpp\include\c++\backward" -I"d:\Dev-Cpp\include" -L"d:\
Dev-Cpp\lib"
Integer.cpp: In function `int main()':
Integer.cpp:27: no matching function for call to `Integer::Integer(Integer)'
Integer.cpp:10: candidates are: Integer::Integer(Integer&)
Integer.cpp:9: Integer::Integer(int)
Integer.cpp:27: initializing temporary from result of `
case 2: If the line 10 is delete, there is no error at line 27 when
compiling.
Could someone explain this for me?
What does line 27 do in detail? Invoke Integer::Integer(int I) directly? or
Create a temporary Integer object from the variable 'a' and use
copy-constructor to create Int1 from this termporary object?
Could someone explain more in details on line 27, 28, 31 and 33? What's the
difference among them?
Thanks a lot.
Best Regards,
Xiangliang Meng