由构造函数和cop-constructor混淆。 [英] Confused by the constructor and cop-constructor.

查看:71
本文介绍了由构造函数和cop-constructor混淆。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段代码:


#include< iostream>

使用命名空间std;


class整数{

int i;

public:

Integer():i(0){};


整数(int I):i(I){};

整数(整数和其他):i(other.i){} //第10行

整数& operator =(整数和其他){

i = other.i;

return(* this);

}

整数& operator =(int a){

i = a;

return(* this);

}

朋友ostream的和放; operator<<(ostream& os,Integer& I){

os<< Ii;

返回os;

}

};


int main(){

int a = 5;

整数Int1 = a; //第27行;这里发生错误。

整数Int2 = Int1; //第28行;


cout<< Int1<<""," << Int2<< endl;

Int1 = 5; //第31行

cout<< Int1<<""," << Int2<< endl;

Int2 = Int1; //第33行

cout<< Int1<<""," << Int2<<结束;

}


案例1:如果第10行在那里,则在第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:在函数`int main()''中:

Integer.cpp:27:没有匹配函数来调用`Integer :: Integer(Integer)''

Integer.cpp:10:候选者是:Integer :: Integer(Integer&)

Integer.cpp:9:Integer :: Integer(int)

Integer.cpp:27:从结果初始化'


案例2:如果第10行被删除,那么

编译时第27行没有错误。


有人可以帮我解释一下吗? br />

第27行详细介绍了什么?直接调用Integer :: Integer(int I)?或者

从变量''''创建一个临时的Integer对象并使用

copy-constructor从这个临时对象创建Int1?

有人可以在第27,28,31和33行详细解释吗?他们之间有什么区别?


非常感谢。


最诚挚的问候,


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

推荐答案



我已经摆弄了代码a小。以下编译:


#include< iostream>


使用命名空间std;


class Integer {

private:


int i;


public:


整数():i(0){;}

整数(int I):i(I){;}


//你可以拥有以下两个代替上面两个:

//整数(int I = 0):i(I){;}


整数(整数&其他):i(other.i){;} //第10行

整数& operator =(整数和其他){

i = other.i;

return(* this);

}


整数& operator =(int a){

i = a;

return(* this);

}


朋友ostream& operator<<(ostream& os,Integer& I){

os<< Ii;

返回os;

}

};

int main()

{

int monkey = 72;


整数Int1(猴子); //神奇的线条


整数Int2 = Int1;


cout<< Int1<<""," << Int2<< endl;


Int1 = 5;

cout<< Int1<<""," << Int2<< endl;


Int2 = Int1;

cout<< Int1<<""," << Int2<<结束;

}


我不知道为什么,上面的神奇线:


整数Int1(猴子);

将编译,而:

整数Int1 =猴子;


不会!!这是一个编译器错误,因为两个语句都应该是完全的

相同吗?

-JKop

I''ve fiddled with the code a little. The following compiles:

#include <iostream>

using namespace std;

class Integer {
private:

int i;

public:

Integer() : i(0) {;}
Integer(int I) : i(I) {;}

//Instead of the two above you could have:
//Integer(int I = 0) : 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 monkey = 72;

Integer Int1(monkey); //THE MAGICAL LINE

Integer Int2 = Int1;

cout << Int1 <<", " << Int2 << endl;

Int1 = 5;
cout << Int1 <<", " << Int2 << endl;

Int2 = Int1;
cout << Int1 <<", " << Int2 << endl;
}

I am at a loss to why, with THE MAGICAL LINE above:

Integer Int1(monkey);
will compile, while:
Integer Int1 = monkey;

won''t!! Is this a compiler bug, because both statements should be EXACTLY
THE SAME?
-JKop


Xiangliang Meng写道:
Xiangliang Meng wrote:

片段代码:

#include< iostream>
使用命名空间std;

类Integer {
int i;
public:
Integer():i(0){};

整数(int I):i(I){};
整数(整数和其他):i(other.i){} //第10行
整数和& operator =(整数和其他){
i = other.i;
return(* this);
}
整数& operator =(int a){
i = a;
返回(* this);
}
朋友ostream&运算符<<(ostream& os,Integer& I){
os<< Ii;
返回os;
}
};

int main(){
int a = 5;
整数Int1 = a ; //第27行;这里发生错误。
整数Int2 = Int1; //第28行;

cout<< Int1<<""," << Int2<< endl;
Int1 = 5; //第31行
cout<< Int1<<""," << Int2<< endl;
Int2 = Int1; //第33行
cout<< Int1<<""," << Int2<<情况1:如果第10行在那里,则在第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:在函数`int main()''中:
Integer.cpp:27:没有匹配的函数调用`Integer :: Integer(Integer)''
Integer.cpp:10:候选者是:Integer :: Integer(Integer&)
Integer.cpp:9:Integer :: Integer(int)
Integer.cpp:27:初始化临时结果`

情况2:如果第10行被删除,则在编译时第27行没有错误。 />
有人可以帮我解释一下吗?


整数Int1 = a; //第27行;这里发生错误。


这里创建一个临时的Integer对象。然后将这个临时的

输入到复制构造函数中以创建Int1。


复制构造函数的签名是:


整数(整数和其他):i(other.i){} //第10行


但临时不能绑定到不是

const!因此错误。


如果您自己不编写复制构造函数,编译器

会生成一个。但是生成的编译器有签名


整数(const整数和其他):i(other.i){} //第10行


看到区别?


这个区别是,为什么编译器生成一个有效,而你的

不是。
copy-constructor从这个临时对象创建Int1?


后者。

有人可以在第27,28,31和33行详细解释吗?它们之间的区别是什么?

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?
Integer Int1 = a; // line 27; error happens here.

Here a temporary Integer object is created from a. This temporary
is then feed into the copy constructor to create Int1.

The signature of your copy constructor is:

Integer(Integer & other) : i(other.i) {} // line 10

But a temporary cannot be bound to a reference which is not
const! Thus the error.

If you don''t write the copy constructor yourself, the compiler
generates one. But the compiler generated has the signature

Integer( const Integer & other) : i(other.i) {} // line 10

See the difference?

This difference is, why the compiler generated one works, while yours
doesn''t.

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?
The later one.

Could someone explain more in details on line 27, 28, 31 and 33? What''s the
difference among them?




27:整数Int1 = a;


对象获取创建 - >使用构造函数,

因此''a''首先必须转换为Integer

对象,使用ctor为此获取一个int。


28:整数Int2 = Int1;


对象被创建 - >使用构造函数

由于Int1已经是一个Integer对象,因此对于Int2使用副本

构造函数

31:Int1 = 5 ;


这是一项任务。因此使用了opertor =

首先尝试找到允许将int分配给某个整数的东西

直接到整数 - >

整数与operator =(int a)
找到并使用



33:Int2 = Int1;


这是作业。因此使用了operator =。

找到一个operator =可以取一个Integer对象 - >

整数& operator =(整数和其他)

被找到并使用。


-

Karl Heinz Buchegger
kb******@gascad.at


Xiangliang Meng写道:
Xiangliang Meng wrote:
片段代码:

#include< iostream>
使用命名空间std;

class Integer {
int i;
public:
Integer():i(0){};

整数(int I):i(I){}; 整数& operator =(整数和其他){
i = other.i;
return(* this);
}
整数& operator =(int a){
i = a;
返回(* this);
}
朋友ostream&运算符<<(ostream& os,Integer& I){
os<< Ii;
返回os;
}
};

int main(){
int a = 5;
整数Int1 = a ; //第27行;这里发生错误。
整数Int2 = Int1; //第28行;

cout<< Int1<<""," << Int2<< endl;
Int1 = 5; //第31行
cout<< Int1<<""," << Int2<< endl;
Int2 = Int1; //第33行
cout<< Int1<<""," << Int2<<情况1:如果第10行在那里,则在第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:在函数`int main()''中:
Integer.cpp:27:没有匹配的函数调用`Integer :: Integer(Integer)''
Integer.cpp:10:候选者是:Integer :: Integer(Integer&)
Integer.cpp:9:Integer :: Integer(int)
Integer.cpp:27:初始化临时结果`

情况2:如果第10行被删除,则在编译时第27行没有错误。 />
有人可以帮我解释一下吗?

第27行详细介绍了什么?直接调用Integer :: Integer(int I)?或者从变量a创建一个临时的Integer对象并使用
copy-constructor从这个临时对象创建Int1?

有人可以详细解释一下第27,28,31和33行?他们之间的区别是什么?

非常感谢。

最诚挚的问候,

孟祥亮
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




这并不是特别回答你的所有问题,但如果你将第10行改为第10行,你可以摆脱编译错误的



整数(const整数和其他):我(other.i){}


我会稍后再试看如果没有人打败我的话,那就明白你为什么会出现这个错误。


Alan



This doesn''t particularly answer all your questions, but you can get rid
of the compiler error if you change line 10 to:

Integer(const Integer & other) : i(other.i) {}

I''ll try later to look it up and see exactly why the error you are
getting occurs, if nobody beats me to it.

Alan


这篇关于由构造函数和cop-constructor混淆。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆