-
basic c++ question
Hi Everyone,
I'm trying to call a class called time....
class Time //define the time class which is managed by the track.
{
//friend Time operator++(Time&);
private:
int mins;
int secs;
public:
//Time(int m= 0, int s= 0)
//{ setTime(m,s); }
//bool setTime(int, int);
void increase(void);
int get_seconds()
{return secs; }
int get_mins()
{return mins;}
};
Iam declaring the time class in my manager class called track like this...
class track //define track class
{
Time timer;
etc etc
};
However, I keep getting this error....
error C2501: 'Time' : missing storage-class or type specifiers
What is going wrong???
any help would greatly be appreciated!
Smithy.
-
on what line do you get the error?
-
The Time class must be defined above any code that uses it. So:
Code:
class Time { ... };
class track { ... };
...will be just fine.
Z.
-
-
How come you have no class constructor and destructor?
Is this allowed? Any C++ experts can explain?
-
Hi Zaei,
can you explain what you mean in more detail?
Thanks.
-
oddly it just compiles for me.
you can but don't have to do as Zaei mentioned if you declare them:
class Time;
class track;
on top of your file
-
1 Attachment(s)
here's the source
Hi guys,
thanks for the hlep so far, i can't understand why it's not working, I have included my c++ code with this post, i have a lot of other stupid little errors in this program as well, if you all could have a look at why the timer object isn't workin and maybe anything else which you can easily fix in it it would be very greatly apprecited!
Thanks.
Best Regards,
Ben Smith.
-
eek! That's a load of errors, try to compile once in a while as you code further...
Anyways, declare
class Time;
at the top and that problem should be over
if you don't understand the errors, look them up in msdn.
-
Hi kedaman,
Thanks for that, do i clear it up the top of the track class ie.
class track
{
class Time;
or at the top of the header file below the include statements.
Oh and also, I am stuck on quite a few of the errors, do you know how to fix any more of them? whatever help i can get will be greatly appreciated!
Best Regards,
Ben Smith.
-
1 Attachment(s)
I corrected some of the errors, now there are only 87 left.
-
thanks!
Thanks Bee!
I've kinda been working non stop though and i'm down to 12!
It's reall fusteraring getting the last of them! i'll put the new source on here now....
-
at the moment I'm working on a list of tips for you...
-
1 Attachment(s)
new source
here's the new source, so close to being complete!
-
This is for the old source, I haven't seen the new yet.
I stopped correcting errors because I didn't know what you wanted to do. Here are some tips on general programming:
I) General programming
1) strcpy is only used for strings, not for any other data types. To copy ints or doubles, use =
2) Only use variables you really declared. Check the variable scope. Don't use names that are already used by the standard library (exit() is a function).
3) Be aware of double includes. Put that into every header you write:
Code:
#ifndef _SOME_NAME_THAT_IS_USED_ONLY_HERE
#define _SOME_NAME_THAT_IS_USED_ONLY_HERE
// code goes here
#endif
4) array names can't be assigned to. If the array is used as string, this is the time to use strcpy.
II) CLASSES
1) You cannot put code inside the class definitions. Always put such code in the constructor. That's what they are here for.
2) You don't need a class for everything. This is not Java. Sometimes it's simpler to use functions instead.
3) A class name is neither a variable nor an array. To get an array of class objects, use:
classname varname[numberOfElements];
for example
horse horses[10];
This is by far the most common error in you program.
-
For the new source:
You pass reference parameters just as would pass normal parameters. No & or something, this is all done at the time of declaration.
You didn't correct the horse[] failure in the raceway constructor.
-
Transcendental: It is legal to not declare constructors//destructors. The compiler will create one that doesn't take arguments and does nothing.
-
thanks again!
Thanks again bee,
if you could explain it with the code i posted, i.e. make the change and repost it it would be very greatl appreciated! i kind of get what you mean but if you could change it i would understand a lot better.
thanks!
-
Ok. My last upload contains some corrections. e.g. service.h contains the #ifndef thing. Others come soon. (After lunch :p)
-
hehe, after lunch. I'm thinking about havin an early early early lunch, it's about 11:30pm here at the moment.
See there's a big difference between Austria and Australia, there in completely different time zones! (I read your profile, hopefully some americans will read this too, hehe).
Oh, and I was born in Australia and the last time i saw a kangaroo would have been about 5 years ago, when i last went camping out in a national park.
-
Yeah, it would be pretty unusual to see a kangaroo hopping through the streets of a city...
But I have seen kangaroos lately - in our local zoo :)
-
Do you want the ++ operator of time as postfix or prefix?
Code:
int i = 1;
i++; // postfix
++i; // prefix
-
-
1 Attachment(s)
I got it down to 3 errors. The problem is that the bookmaker and gambler objects are members of raceway, sothey cannot be accessed in track::start_race. A solution would be to make those objects global, but I'll leave that up to you.