//Header file for the Race Program
//
//Contains the Runner class
#ifndef _RACE_H
#define _RACE_H
#include <iostream>
#include <string>
usingnamespace std;
class Runner
{
string runners_name;
int runners_time;
int runners_age;
Public:
void set_runners_name ();
};
#endif
//////////////////////////
But when I add the line "Public:", I can no longer build, I get these two errors:
Thanks, that seems to be the problem. I know that's a rookie move but it's been a long time since I've used C++.
Another quick question for anyone out there. I'm using Visual Studio 08 Pro for the first time to do this simple C++ program, and I'm using their template for a Win32 app and I'm not sure where to add in my definitions.
The files they provide are my "Race.cpp" and "Race.h", then I also have this standard "stdafx.cpp" and "stdafx.h".
Eh, don't worry about the "rookie move". We all had to start somewhere. :)
What do you mean by your definitions? Do you mean where to include your custom header file in your program? If so, put it at the top of your "Race.cpp" file.
#include "Race.h"
Else... I'm not sure how to answer this question. What do you mean?
Well, back in my college classes I always remembered making classes in a specific way. In the header you provide the declarations, then somewhere else is where you would provide your actual definitions.
For example, in my above class, I would only declare it in my "race.h" file, like this:
So in the declaration you just have the first line from every accessor or whatever, along with the number of parameters, then somewhere else in my files I'm supposed to have a definition, where I actually say what each thing should execute.
Again this is going on my memory, so maybe I need to be caught up on better coding habits.
For one-cpp-file-programs, it goes after your include, and it goes before everything that uses it within that C++ file.
Seeing as you have only one C++ file, you can do the above, however if you have multiple files that use those definitions, then put your definitions in a separate file. It's also more organized. :)
-Albatross
P.S.-What you're doing isn't mandatory, and there is a simpler way of doing it. You could have used only the definitions. You didn't have to have the header file.
P.P.S.-Your accessors will not work. They cannot return both void and something.
Yeah, that makes sense. But this is kinda weird, when I try to add my definition, it is giving me an error on that line that says "error C2011: 'Runner' : 'class' type redefinition"
So it would seem like VS thinks that my class declaration within the header is acting as a definition as well. Because I definitley cannot put it in my cpp file without error!
Ah, it's been ages since I used classes across multiple files, but that's a possibility. You could try to axe "class" from in front of the definition... I don't know how that would work...
Another way of doing the definitions if you want to keep the header file is: