运算符重载和复制构造函数。找不到错误。 [英] Operator overloading and copy constructor. Can't find the error.
问题描述
一个带有运算符重载和复制构造函数的简单程序:
#include< iostream>
#include< string>
using namespace std;
class Vector {
private:
float x,y;
public :
向量(浮动u,浮动v);
向量(无效);
向量运算符+(向量a);
Vector(Vector& source);
void显示(无效);
};
void Vector :: Show(无效){
cout<<"("<< x<<","<< y<<")" ;;
}
Vector :: Vector(float u,float v){
x = u; y = v;
}
Vector :: Vector(void){
x = 0; y = 0;
}
Vector :: Vector(Vector& source){
x =(source.x)* 2; y =(source.y)* 2;
}
矢量矢量::运算符+(矢量a){
矢量温度;
temp.x = x + ax;
temp.y = y + ay;
返回临时数;
}
int main(无效){
向量a(3,1),b(5,2),c,d;
c = a + b;
d = a.operator +(b);
cout<< 向量c的数据成员:;
c.Show();
向量e(a + b);
cout<< endl<< 向量的数据成员e:;
e.Show();
返回0;
}
编译器(g ++ -pedantic -W -Wall)说:
teste.cpp:在函数`int main()''中:
teste.cpp:36:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选人是:矢量::矢量(矢量&)
teste.cpp:37:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选人是:Vector :: Vector(Vector&)
teste.cpp:40:错误:没有匹配函数来调用< br $>
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选者是:Vector :: Vector(Vector&)
teste.cpp:21:注意:Vector :: Vector()
teste.cpp:18:注意:Vector :: Vector(float,float)
复制构造函数Vector :: Vector(Vector& source),它可以工作
罚款。
有谁知道代码中有什么问题?
提前致谢。
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector(void);
Vector operator+ (Vector a);
Vector(Vector &source);
void Show(void);
};
void Vector::Show(void) {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector(void) {
x=0; y=0;
}
Vector::Vector(Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operator+ (Vector a) {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
int main(void) {
Vector a(3,1), b(5,2), c, d;
c = a+b;
d = a.operator+ (b);
cout << "Data members of the vector c: ";
c.Show();
Vector e(a+b);
cout <<endl << "Data members of the vector e: ";
e.Show();
return 0;
}
The compiler (g++ -pedantic -W -Wall) says:
teste.cpp: In function `int main()'':
teste.cpp:36: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:37: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:40: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:21: note: Vector::Vector()
teste.cpp:18: note: Vector::Vector(float, float)
Without the copy constructor Vector::Vector(Vector &source) , it works
fine.
Would anyone know what is wrong in the code?
Thanks in advance.
推荐答案
" clicwar" < cl ***** @ gmail.com写信息
新闻:11 ********************** @ m3g2000hsh。 googlegro ups.com ...
"clicwar" <cl*****@gmail.comwrote in message
news:11**********************@m3g2000hsh.googlegro ups.com...
一个带有运算符重载和复制构造函数的简单程序:
#include< iostream>
#include< string>
使用命名空间std;
class Vector {
private:
浮动x,y;
公开:
向量(浮动u,浮动v);
向量(void) ;
矢量运算符+(向量a);
向量(向量&源);
void显示(无效);
};
void Vector :: Show(void){
cout<<"("<< x<< ","<< y<<")" ;;
}
Vector :: Vector(float u,float v){
x = u; y = v;
}
Vector :: Vector(void){
x = 0; y = 0;
}
Vector :: Vector(Vector& source){
x =(source.x)* 2; y =(source.y)* 2;
}
矢量矢量::运算符+(矢量a){
矢量温度;
temp.x = x + ax;
temp.y = y + ay;
返回临时数;
}
int main(无效){
向量a(3,1),b(5,2),c,d;
c = a + b;
d = a.operator +(b);
cout<< 向量c的数据成员:;
c.Show();
向量e(a + b);
cout<< endl<< 向量的数据成员e:;
e.Show();
返回0;
}
编译器(g ++ -pedantic -W -Wall)说:
teste.cpp:函数`int main()'':
teste.cpp:36:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24 :注意:候选人是:矢量::矢量(矢量&)
teste.cpp:37:错误:没有匹配函数来调用
`Vector :: Vector(矢量)''
teste.cpp:24:注意:候选者是:Vector :: Vector(Vector&)
teste.cpp:40:错误:没有匹配的功能打电话给
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选者是:Vector :: Vector(Vector&)
teste.cpp:21:注意:Vector :: Vector()
teste.cpp:18:注意:Vector :: Vector(float,float)
没有复制构造函数Vector :: Vector(Vector& source),它可以工作
罚款。
有人知道代码中有什么问题吗?
提前致谢
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector(void);
Vector operator+ (Vector a);
Vector(Vector &source);
void Show(void);
};
void Vector::Show(void) {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector(void) {
x=0; y=0;
}
Vector::Vector(Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operator+ (Vector a) {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
int main(void) {
Vector a(3,1), b(5,2), c, d;
c = a+b;
d = a.operator+ (b);
cout << "Data members of the vector c: ";
c.Show();
Vector e(a+b);
cout <<endl << "Data members of the vector e: ";
e.Show();
return 0;
}
The compiler (g++ -pedantic -W -Wall) says:
teste.cpp: In function `int main()'':
teste.cpp:36: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:37: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:40: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:21: note: Vector::Vector()
teste.cpp:18: note: Vector::Vector(float, float)
Without the copy constructor Vector::Vector(Vector &source) , it works
fine.
Would anyone know what is wrong in the code?
Thanks in advance.
更改为:
Vector :: Vector(const Vector& a)
它应该可以工作。
Change it to:
Vector::Vector( const Vector& a )
and it should work.
首先,谢谢Jim。它有效(但我不知道为什么
简单添加const修饰符可以做到这一点。你能告诉
我吗?)。
现在的输出是:
向量c的数据成员:(13,5)
向量e的数据成员:(13, 5)
但c和e都应该是(16,6)。
有什么建议吗?
谢谢。
月刊于22日,16:20,Jim Langston < tazmas ... @ rocketmail.comwrote:
First of all, thank you Jim. It worked ( but i don''t know why the
simple addition of the const modifier can do that. Could you tell
me? ).
The output now is:
Data members of the vector c: (13,5)
Data members of the vector e: (13,5)
But it should be (16,6) for both c and e.
Any suggestions?
Thanks.
On 22 jul, 16:20, "Jim Langston" <tazmas...@rocketmail.comwrote:
" clicwar" < clic ... @ gmail.com写信息
新闻:11 ********************** @ m3g2000hsh.googlegro ups.com ...
"clicwar" <clic...@gmail.comwrote in message
news:11**********************@m3g2000hsh.googlegro ups.com...
一个带有运算符重载和复制构造函数的简单程序:
#include< iostream>
#include< string>
using namespace std;
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u,float v);
Vector(void);
向量运算符+(向量a) ;
Vector(Vector& source);
void显示(无效);
};
无效Vector :: Show(void){
cout<<"("<< x<<","<< y<<" ;)" ;;
}
Vector :: Vector(float u,float v){
x = u; y = v;
}
Vector :: Vector(void){
x = 0; y = 0;
}
Vector :: Vector(Vector& source){
x =(source.x)* 2; y =(source.y)* 2;
}
矢量矢量::运算符+(矢量a){
矢量温度;
temp.x = x + ax;
temp.y = y + ay;
返回临时数;
}
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector(void);
Vector operator+ (Vector a);
Vector(Vector &source);
void Show(void);
};
void Vector::Show(void) {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector(void) {
x=0; y=0;
}
Vector::Vector(Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operator+ (Vector a) {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
int main(void){
向量a(3,1),b(5,2),c ,d;
c = a + b;
d = a.operator +(b);
cout<< 向量c的数据成员:;
c.Show();
向量e(a + b);
cout<< endl<< 向量e的数据成员:;
e.Show();
int main(void) {
Vector a(3,1), b(5,2), c, d;
c = a+b;
d = a.operator+ (b);
cout << "Data members of the vector c: ";
c.Show();
Vector e(a+b);
cout <<endl << "Data members of the vector e: ";
e.Show();
返回0;
}
return 0;
}
编译器(g ++ -pedantic -W -Wall)说:
teste.cpp:函数`int main()'':
teste.cpp:36 :错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选者是:Vector :: Vector (矢量&)
teste.cpp:37:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选人是:矢量::矢量(矢量&)
teste.cpp:40:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选者是:Vector :: Vector(Vector&)
teste.cpp: 21:注意:Vector :: Vector()
teste.cpp:18:注意:Vector :: Vector(float,float)
The compiler (g++ -pedantic -W -Wall) says:
teste.cpp: In function `int main()'':
teste.cpp:36: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:37: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:40: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:21: note: Vector::Vector()
teste.cpp:18: note: Vector::Vector(float, float)
如果没有复制构造函数Vector :: Vector(Vector& source),它可以正常工作
。
Without the copy constructor Vector::Vector(Vector &source) , it works
fine.
有人知道代码中有什么问题吗?
提前谢谢。
Would anyone know what is wrong in the code?
Thanks in advance.
更改为:
Vector :: Vector(const Vector& a)
它应该可以工作。
Change it to:
Vector::Vector( const Vector& a )
and it should work.
" clicwar" < cl ***** @ gmail.com写信息
news:11 ******************** @ k79g2000hse.googlegrou ps .com ...
"clicwar" <cl*****@gmail.comwrote in message
news:11********************@k79g2000hse.googlegrou ps.com...
首先,谢谢Jim。它有效(但我不知道为什么
简单添加const修饰符可以做到这一点。你能告诉
我吗?)。
现在的输出是:
向量c的数据成员:(13,5)
向量e的数据成员:(13, 5)
但c和e都应该是(16,6)。
有什么建议吗?
谢谢。
于18月18日16:20,Jim Langston < tazmas ... @ rocketmail.comwrote:
First of all, thank you Jim. It worked ( but i don''t know why the
simple addition of the const modifier can do that. Could you tell
me? ).
The output now is:
Data members of the vector c: (13,5)
Data members of the vector e: (13,5)
But it should be (16,6) for both c and e.
Any suggestions?
Thanks.
On 22 jul, 16:20, "Jim Langston" <tazmas...@rocketmail.comwrote:
>" clicwar" < clic ... @ gmail.com写信息
新闻:11 ********************** @ m3g2000hsh.googlegr oups .com ...
>"clicwar" <clic...@gmail.comwrote in message
news:11**********************@m3g2000hsh.googlegr oups.com...
一个带有运算符重载和复制构造函数的简单程序:
#include< iostream>
#include< string>
using namespace std;
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u,float v);
Vector(void);
向量运算符+(向量a) ;
Vector(Vector& source);
void显示(无效);
};
无效Vector :: Show(void){
cout<<"("<< x<<","<< y<<" ;)" ;;
}
Vector :: Vector(float u,float v){
x = u; y = v;
}
Vector :: Vector(void){
x = 0; y = 0;
}
Vector :: Vector(Vector& source){
x =(source.x)* 2; y =(source.y)* 2;
}
矢量矢量::运算符+(矢量a){
矢量温度;
temp.x = x + ax;
temp.y = y + ay;
返回临时数;
}
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector(void);
Vector operator+ (Vector a);
Vector(Vector &source);
void Show(void);
};
void Vector::Show(void) {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector(void) {
x=0; y=0;
}
Vector::Vector(Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operator+ (Vector a) {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
int main(void){
向量a(3,1),b(5,2),c ,d;
c = a + b;
d = a.operator +(b);
cout<< 向量c的数据成员:;
c.Show();
向量e(a + b);
cout<< endl<< 向量e的数据成员:;
e.Show();
int main(void) {
Vector a(3,1), b(5,2), c, d;
c = a+b;
d = a.operator+ (b);
cout << "Data members of the vector c: ";
c.Show();
Vector e(a+b);
cout <<endl << "Data members of the vector e: ";
e.Show();
返回0;
}
return 0;
}
编译器(g ++ -pedantic -W -Wall)说:
teste.cpp:函数`int main()'':
teste.cpp:36 :错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选者是:Vector :: Vector (矢量&)
teste.cpp:37:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选人是:矢量::矢量(矢量&)
teste.cpp:40:错误:没有匹配函数来调用
`Vector :: Vector(Vector)''
teste.cpp:24:注意:候选者是:Vector :: Vector(Vector&)
teste.cpp: 21:注意:Vector :: Vector()
teste.cpp:18:注意:Vector :: Vector(float,float)
The compiler (g++ -pedantic -W -Wall) says:
teste.cpp: In function `int main()'':
teste.cpp:36: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:37: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:40: error: no matching function for call to
`Vector::Vector(Vector)''
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:21: note: Vector::Vector()
teste.cpp:18: note: Vector::Vector(float, float)
没有复制构造函数Vector :: Vector(Vector& source),它可以工作
罚款。
Without the copy constructor Vector::Vector(Vector &source) , it works
fine.
有人知道代码中有什么问题吗?
提前谢谢。
Would anyone know what is wrong in the code?
Thanks in advance.
将其更改为:
Vector :: Vector(const Vector& a)
它应该可以工作。
Change it to:
Vector::Vector( const Vector& a )
and it should work.
好吧,我的代码(26.3或其他东西)的结果更糟糕。不是
确定原因,但是如果你修好你的算子+它会显示正确答案
16.6
矢量运算符+ (const Vector& a)const;
运算符和构造函数具有特定的签名。 const是他们的一部分
我相信参考也是。我真的不知道发生了什么事情
给了我们错误的结果(IE不知道编译器是什么东西
donig)并且不是真的小心,导致修复操作员+修复问题。
至于为什么没有你的构造函数有没有const,那就是
因为它没有认出它是一个复制构造函数。
这让我觉得,将它从副本更改为引用可能意味着
你的复制构造函数是不正常的,让我看一下......
您的复制构造函数是否将值乘以2?这不是一个副本
构造函数应该做的。
很明显,你可以将你的2个构造函数合并为一个。
向量(浮动u,浮动v);
向量::向量(浮点数u = 0,浮点数v = 0):x(u),y (v)
{
}
此外,这是C ++。当
方法/函数没有参数时,我们不会放(无效)。我们只是使用()I.E.
void Show();
Well, I get even a weirder result with your code (26.3 or something). Not
sure why, but if you fix your operator+ it''ll show the correct answer of
16.6
Vector operator+ (const Vector& a) const;
Operators and constructors have specific signatures. const is part of them
and I believe references are also. I really don''t know what was going on
that was giving us the wrong results (I.E. no clue what the compiler was
donig) and don''t really care, cause fixing the operator+ fixes the problem.
As to why it didn''t work without your constructor having const, that''s
because it didn''t recognize it as a copy constructor.
Which makes me think, changing it from a copy to a reference probably means
your copy constructor is not working right, let me look at it...
Your copy constructor is mulitplying the values by 2? That''s not what a copy
constructor is supposed to do.
Incidently, you can merge your 2 constructors into one.
Vector(float u, float v);
Vector::Vector(float u = 0, float v = 0): x(u), y(v)
{
}
Also, this is C++. We don''t put (void) when there are no parameters to a
method/function. We just use () I.E.
void Show();
这篇关于运算符重载和复制构造函数。找不到错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!