构造函数问题 [英] Constructor problem

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

问题描述

我正在编写一个类,通过接受一个值(即3)和一个度量单位(即m)来表示度量距离




我已经在.h文件中写了我的构造函数


距离(int,char);


我已经在.cpp实现文件中编写了构造函数:


Distance :: Distance(int n,char m):nu(n),me(m){ }


当我尝试在main中输入一个值时使用:

距离a =距离(5,m);


我收到一个错误,说m值。是未申报的,但是5被接受了。

考虑到我在初始构造函数中声明了这些变量我不能理解为什么我会收到这个错误。


如果有人能给我解释我正在做的事情,我会很感激。
错误以及如何纠正我的错误。 />

谢谢

I am in the process of writing a class that will represent metric distances
by accepting a value (ie, 3) and a unit of measure (ie, m).

I''ve written my constructor in the .h file as

Distance (int, char);

I''ve written the constructor in the .cpp implementation file as:

Distance :: Distance ( int n, char m) : nu(n), me(m) {}

When I try to enter a value in main using:
Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared, yet the 5 is accepted.
Considering I declared these variables in the initial constructor I can''t
understand why I''m getting this error.

I''d appreciate it if someone could give me an explanation of what I''m doing
wrong and how I can correct my mistake.

Thanks

推荐答案

2004年4月10日星期六03:43:59 GMT in comp .lang.c ++,Chiller < ... @ ...>

写道,
On Sat, 10 Apr 2004 03:43:59 GMT in comp.lang.c++, "Chiller" <...@...>
wrote,
距离(int,char);
距离a =距离(5,m);

我收到错误,说m值为m。是未申报的,
Distance (int, char); Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared,




未申报。 m是一个标识符,而不是char字面值,因为''m''

将是。



It is undeclared. m is a identifier, not a char literal as ''m'' would
be.


4月10日星期六2004 03:43:59 GMT in comp.lang.c ++,Chiller < ... @ ...>

写道,
On Sat, 10 Apr 2004 03:43:59 GMT in comp.lang.c++, "Chiller" <...@...>
wrote,
距离(int,char);
距离a =距离(5,m);

我收到错误,说m值为m。是未申报的,
Distance (int, char); Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared,




未申报。 m是一个标识符,而不是char字面值,因为''m''

是。



It is undeclared. m is a identifier, not a char literal as ''m'' would
be.


" Chiller" < ... @ ...>在消息中写道

news:3d ****************************** @ news.teranew s。 com ...
"Chiller" <...@...> wrote in message
news:3d******************************@news.teranew s.com...
我正在编写一个类,通过接受一个值(即3)和一个度量单位来表示公制
距离(即,m)。

我已经在.h文件中写了我的构造函数

距离(int,char);

我'已经在.cpp实现文件中编写了构造函数:

距离::距离(int n,char m):nu(n),me(m){}

当我尝试在main中输入一个值时使用:
距离a =距离(5,m);

我收到一个错误,说值为m。是未申报的,但5被接受。
考虑到我在初始构造函数中声明了这些变量,我不能理解为什么我会收到这个错误。

我如果有人能给我解释我做错了什么以及如何纠正我的错误,我会很感激。
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m).

I''ve written my constructor in the .h file as

Distance (int, char);

I''ve written the constructor in the .cpp implementation file as:

Distance :: Distance ( int n, char m) : nu(n), me(m) {}

When I try to enter a value in main using:
Distance a = Distance (5, m);

I get an error saying the value "m" is undeclared, yet the 5 is accepted.
Considering I declared these variables in the initial constructor I can''t
understand why I''m getting this error.

I''d appreciate it if someone could give me an explanation of what I''m doing wrong and how I can correct my mistake.




如果你意思是文字m,那么你可以这样做:


距离a =距离(5,''m'');


或更简洁:


距离a(5,''m'');


如果你不是意思是字面字符m,那么你需要更多的具体关于你想要做什么。特别是,提供一个

实际可编辑的代码 - 你提供的内容是不可编辑的。


祝你好运,

Tom



If you mean the literal character ''m'', then you can do:

Distance a = Distance(5, ''m'');

or, more concisely:

Distance a(5, ''m'');

If you don''t mean the literal character ''m'', then you need to be more
specific about what it is you are trying to do. In particular, provide an
actual compilable piece of code - what you provided is not compilable as is.

Best regards,

Tom


这篇关于构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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