Header file help

Grab your favourite IDE and tinker with the innards of game engines

Header file help

Postby BlekksPoncho on Fri Apr 06, 2007 2:31 pm

Hi I have been refreshing myself on simple C stuff, but something isn't working; Say I am creating a little program that outputs Hello as a string.

I include string in stdafx.h (which VC++2005 creates to manage all the headers).

Then i decide to make it a function that outputs hello above the main():

Code: Select all
void helloOutput(string string1)
{
cout<<string1;
}


Then I decide to put this function in its own header file called, MainHeaders.

I then reference this header file in stdafx.h but when I compile it says that "string is an undelcared identifier" so I am guessing that means that I have not included <string>. But it will be the main file that uses this function, and the main file has stdafx included.

What is wrong?

Thanks.
-----------------------
I'm not drunk, I always eat plastic.
User avatar
BlekksPoncho
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Sat Apr 01, 2006 10:48 pm
Location: England - UK

Postby Sauce on Sat Apr 07, 2007 12:14 am

last time i checked, there was no 'string' data type (or at least for me, for some reason it doesnt work D: ) so instead, use an array of char variables:

Code: Select all
char[] string;

or is it
Code: Select all
char string[];


I cant remember, havent done any coding in ages.[/code]
Image
Blink wrote:Do you watch porn and decide you don't need to have sex because you've seen the ending? :-D

zombie@computer wrote:what retarded countries measure in stones anyway?
or feet? or inches? Your dick is a lot longer in cms
User avatar
Sauce
Senior Member
Senior Member
 
Joined: Sat Nov 26, 2005 4:36 am
Location: Australia

Postby Forceflow on Sat Apr 07, 2007 2:09 am

In C: no classes, thus no string. Chars are the way to go.
In C++: String should be included by default. Check that auto-generated file for the correct includes.

#include <string>
User avatar
Forceflow
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 12:13 pm
Location: Belgium

Postby BlekksPoncho on Sat Apr 07, 2007 12:04 pm

Well here is my file structure:

main.cpp includes:

stdafx.h
-------------------

stdafx.h includes:

<string>
<iostream>
"mainquiz" (my header file with all the functions)
-------------------

Strings work in main.cpp as they are included through stdafx.h but when compiling, I get an error saying that mainquiz.h has the undeclared identifier "string".
But the main.cpp file is the one using the functions so why does it matter, cos they will have <string> included anyway?
I then included <string> in the mainquiz.h header, but I got the same errors.

Anyone know what is wrong?

Thanks.
-----------------------
I'm not drunk, I always eat plastic.
User avatar
BlekksPoncho
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Sat Apr 01, 2006 10:48 pm
Location: England - UK

Postby Sauce on Sun Apr 15, 2007 1:04 pm

show us the files?
Image
Blink wrote:Do you watch porn and decide you don't need to have sex because you've seen the ending? :-D

zombie@computer wrote:what retarded countries measure in stones anyway?
or feet? or inches? Your dick is a lot longer in cms
User avatar
Sauce
Senior Member
Senior Member
 
Joined: Sat Nov 26, 2005 4:36 am
Location: Australia

Postby BlekksPoncho on Sun Apr 15, 2007 2:47 pm

All the files are here:

http://www.mpagbyorksregion.co.uk/tomsmith/c++/

Thanks.
-----------------------
I'm not drunk, I always eat plastic.
User avatar
BlekksPoncho
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Sat Apr 01, 2006 10:48 pm
Location: England - UK

Postby ChopperDave on Sun Apr 15, 2007 4:36 pm

The string class is part of the std namespace. Unless you declare you're using that namespace somewhere in your code, you'll have to explicitly access the string class through the namespace by doing std::string.

To use the namespace (and thus eliminate the need for going through it), you can say:

using namespace std;

Near the top of your cpp file, after your #includes.
User avatar
ChopperDave
Rockstar San Diego
 
Joined: Wed Feb 21, 2007 6:06 pm

Postby BlekksPoncho on Sun Apr 15, 2007 5:32 pm

If you look at MethodTesr.cpp I do have "using namespace std" at the start of that file, but do I need to add it anywhere else?

Such as in the MainQuiz.h header file which has all the functions in, that contain strings that MethodTesr.cpp uses.
-----------------------
I'm not drunk, I always eat plastic.
User avatar
BlekksPoncho
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Sat Apr 01, 2006 10:48 pm
Location: England - UK

Postby zombie@computer on Sun Apr 15, 2007 5:49 pm

if you use string in the header file, you must do a using namespace std in the header file as well offcourse
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Postby BlekksPoncho on Sun Apr 15, 2007 7:35 pm

Yeah ok, I am using it in the header file now but I am getting weird errors about chars and the MethodTesr.obj file. I am not at my computer right now but on my Archos 604 Wifi.
-----------------------
I'm not drunk, I always eat plastic.
User avatar
BlekksPoncho
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Sat Apr 01, 2006 10:48 pm
Location: England - UK

Return to Programming

Who is online

Users browsing this forum: No registered users