Having trouble making a class and/or header file

I have this simple header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Header file for the Race Program
//
//Contains the Runner class

#ifndef _RACE_H
#define _RACE_H
#include <iostream>
#include <string>

using namespace 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:

error C2062: type 'void' unexpected
error C2238: unexpected token(s) preceding ';'

Same thing happens when I try to add "Private" to this class too. Not sure what the problem is. Any help is appreciated.
Last edited on
Hello,

"Public" need not be capitalized. In fact, there can't be any capital letters in any keywords...

-Albatross
Last edited on
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".

Thanks.
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?

-Albatross
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Runner
{
private:

	string runners_name;
	int runners_time;
	int runners_age;

public:
	//Accessors
	void get_runners_name ();
	void get_runners_time ();
	void get_runners_age ();

	//And Mutators
	void set_runners_name (string name);
	void set_runners_time (int time);
	void set_runners_age (int age);
};


But then again somewhere else I should have the actual definition, which should match the declaration as far as parameters and what not go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Runner
{
private:

	string runners_name;
	int runners_time;
	int runners_age;

public:

	//Accessors
	void get_runners_name()
	{
		return runners_name;
	}
	void get_runners_time()
	{
		return runners_time;
	}
	void get_runners_age()
	{
		return runners_age;
	}

	//And Mutators
	void set_runners_name (string name)
	{
		runners_name = name;
	}
	void set_runners_time (int time)
	{
		runners_time = time;
	}
	void set_runners_age (int age)
	{
		runners_age = age;
	}

};



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.

Thanks a lot.
OH! /desk

I knew that difference!

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.
Last edited on
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:
1
2
3
4
5
6
7
8
returntype Runner::FunctionWithin
{
   def;
}
otherreturntype Runner::OtherFunctionWithin
{
   otherdef;
}

And so on.

EDIT: -Albatross
Last edited on
Topic archived. No new replies allowed.