我不知道我错了....... plz cn任何人都帮助我 [英] I dont kno where I am wrong .......plz cn anyone help me

查看:104
本文介绍了我不知道我错了....... plz cn任何人都帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在另一个程序中使用这个课程

它显示错误







i can use this class in another program
and it shows a errror



#include<dos.h>

class mydate
{
 int d,m,y;
 public:
    friend void inputdate();
       void displaydate(void);
};
   void inputdate(mydate &t)
{
  mydate t;
  TOP:
  cout<<"\n Enter date(dd-mm-yy)forms:";
  cin>>t.d>>t.m>>t.y;
  if(t.d<1||t.d>31)
  {
    cout<<"\n Wrong day entered";
    goto TOP;
  }
  if(t.m<1||t.m>12)
  {
   cout<<"\nWrong month entered";
   goto TOP;
 }
 if(t.y<1000||t.y>9999)
 {
   cout<<"\n Wrong year entered";
   goto TOP;
 }
}





我的尝试:



我不知道我的错误在哪里



What I have tried:

I dont knw where is my mistake

推荐答案

这个清单不小...



首先,当你收到错误时, 告诉我们它是什么 - 这意味着给我们提供错误信息,告诉我们在哪里它会发生,以及如何让它发生。

告诉我们你正在使用哪个编译器也是个好主意,因为我们不知道...



但是......当我尝试在线C ++编译器时,它会产生很多错误:

The list is not small...

To start with, when you get an error, tell us what it is - that means give us the error message, tell us where it occurs, and how you get it to happen.
It's also a good idea to tell us which compiler you are using as we have no idea...

But...when I try an online C++ compiler, it gives many errors:
void inputdate(mydate &t)
{
mydate t;

声明局部变量 t 隐藏同名参数:这意味着您无法访问参数所有,为什么呢?猜测一下,你应该完全删除本地声明,并使用参数。

The declaration of a local variable t shadows the parameter of the same name: which means you cannot access the parameter at all, so why have it? At a guess, you should remove the local declaration completely, and use the parameter.

class mydate
{
int d,m,y;



And

cin>>t.d>>t.m>>t.y;

既然你不喜欢t在声明变量时指定变量的访问权限,默认情况下它们是 private :因此您无法从类本身外部访问它们。由于 inputdate 函数不是 mydate 类的一部分,因此无法访问私有成员。它没有与在类中声明的 friend 函数共享相同的签名:

Since you don't specify an access for the variables when you declare them, they are private by default: so you can't access them from outside the class itself. Since the inputdate function is not a part of the mydate class, it can't access private members. It doesn;t share the same signature as the friend function declared in the class:

class mydate
    {
    ...
    friend void inputdate();
    ...
    };
void inputdate(mydate &t)

因此无法访问私人数据。

从函数定义中删除参数,或将其添加到朋友函数声明中。



并忘记那个转到甚至存在。立即从你的所有代码中删除它,并且=不要尝试使用它至少五年 - 这时你将有足够的经验来理解为什么你不应该将它用于这样的琐碎代码,当它是适合使用它。使用循环,例如,而执行...而代替。

So it isn't able to access private data.
Either remove the parameter from the function definition, or add it to the friend function declaration.

And forget that goto even exists. Remove it from all your code immediately, and do =not attempt to use it for at least five years - by which time you will have enough experience to understand why you shouldn't be using it for trivial code like this, and when it is appropriate to use it. Use loops such as while or do ...while instead.


这篇关于我不知道我错了....... plz cn任何人都帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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