-
Class question - solved
Why do I have to have these things that are marked in bold below in the code?
Code:
class keeper
{
private:
int hours;
int minutes;
public:
keeper();
keeper(int s, int p);
void addKeeper(int, int);
void writeKeeper(keeper &k);
keeper getKeeper(keeper &h, char *name); keeper operator+(const keeper &k) const;
void show();
~keeper() {};
};
.
.
.
keeper keeper::getKeeper(keeper &h, char *name)
{
ifstream fin;
fin.open(name);
for(int i=0;i<3;i++)
{
switch(i)
{
case 0: fin >> h.hours;
break;
case 1: fin >> h.minutes;
break;
}
}
fin.close();
return h;
}
I know I can't have void keeper::........, but why do I have to have keeper beofre the code?
thanx
-
You're returning a copy of a reference already passed into the function :confused:
Either take out the reference being passed, or change it to a void. When you pass a reference you can change it from the function.
-
I think you should have keeper& as return value
It's quite useful when building expresions with the sideeffect-paradigm
For instance:
char source[10]="something";
char* copy=strncpy(new char[10],source,10);
-
Problem solved thanks to Kedaman and Parksie, thank you!