Click here to Skip to main content
14,218,748 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted
Updated 1-Apr-17 21:44pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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;
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;
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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100