Skip to main content
gamedev.net
🔒 Locked

Function Declarations

Started by Chris41411 Nov 6, 2004 at 7:00 AM 3 replies 450+ views
Original Post
Chris41411
Chris41411
What I am doing is creating a program that, when run, will let you choose between several different, very simple games (a few hundred lines of code each). What I want to do is have a .cpp and .h file for each of the separate games. I don't want to use classes for the games' functions, so what I was going to do is put the function declarations in the .h files and the definitions in the .cpp file, but I keep getting all sorts of errors when I do this, like: error C2871: 'std' : does not exist or is not a namespace, error C2065: 'string' : undeclared identifier, error C2143: syntax error : missing ';' before '{', and error C2447: missing function header (old-style formal list?). Here is how a generic version of what I used looked.


//Game1.h

#ifndef GAME1_H
#define GAME1_H


void GetInfo(string & name, int & explorers, int & killed, int & survivors);
void GameFunction(string & name, int & explorers, int & killed, int & survivors);

#endif GAME1_H


Most of the errors are coming from the header.

//Game1.cpp


#include <string>
#include <iostream>
#include "Game1.h"

using namespace std;

void GetInfo(string & name, int & explorers, int & killed, int & survivors)
{
	
     //definition

}

void GameFunction(string & name, int & explorers, int & killed, int & survivors)
{
	
    //definition

}



//Main.cpp


#include "TreasureHunt.h"

int main()
{
	


	return 0;
}


Do I have to put the functions in classes if I want to separate them from the definitions?
Chris41411
Chris41411
Sorry. In the main.cpp file, the header should read "Game1.h" not "TreasureHunt.h".
Endurion
Endurion
Put either the #include in the header of game1.h or the using namespace std ABOVE the include of "game1.h".
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Staffan E
Staffan E
I think you have to have the line:

#include

in game1.h instead of in game1.cpp. Otherwise the .h file won't know what the string is. The .cpp file will inherit the includes from the .h file so it doesn't need that line.

btw. shouldn't the

#endif GAME1_H

be only

#endif

That works with my code and I don't know if it is proper to have the GAME1_H there...

[edit] sorry didn't notice Endurion had answered already...
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
Chris41411
Chris41411
Hey, thanks a lot guys. I did what you said, and it compiled clean as a whistle. I thought that I had tried doing that, but I have been up working on different projects all night, so I guess I'm just a little out of it. Oh, about the #endif thing you were right, I did that by mistake too. Anyway, thanks again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.