# The wierdest header file ever?!

This topic is 4600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I'm including my own header with ONLY FOUR LINES in it. My program has compiled before. (And this is with no changes) It's giving me the errors In file included from C:/CPP/Main.cpp:4: C:/CPP/Functions.h:6: error: string' was not declared in this scope C:/CPP/Functions.h:6: error: syntax error before ,' token So, it says I have errors on line 6 with only 4 lines of code??

##### Share on other sites
Maybe you have four lines of code but actually six lines, two are blank and just returns.

##### Share on other sites
The incorrect line numbers are probably because something is being preincluded in your header, or something like that.

Anyway, to get past your error, it looks like you need to #include and say 'using namespace std::string' in the HEADER file. That should solve your compile problem.

##### Share on other sites
It would help a lot if you showed us those lines, as well as what comes before/after the point you #included it at.

##### Share on other sites
Actually, no, they aren't blank lines. They are backspaced out (is that a word?) anyways, here's the code:

#ifndef __FUNCTIONS_H#define __FUNCTIONS_Hint CreateWND(string Title, int Width, int Height);#endif

##### Share on other sites
u forgot to include a required header. String is NOT a default datatype, and will require a header file to be included prior to usage.

Wow...

##### Share on other sites
You mean #include <string> right?

Anyways, this program compile just right before, and #include <string> isn't helping the errors!

##### Share on other sites
You need
#include <string>
using namespace std;

Or you need to replace "string" with "std::string".

##### Share on other sites
Quote:
 Original post by orcfan32You mean #include right?Anyways, this program compile just right before, and #include isn't helping the errors!

Try adding a std:: in front of string in the function declaration.

##### Share on other sites
Quote:
 Original post by PromitYou need#include using namespace std;Or you need to replace "string" with "std::string".

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by PromitYou need#include using namespace std;Or you need to replace "string" with "std::string".

I second that, but how else would you do it?

BTW, it's still not compiling..

##### Share on other sites
This will compile:

//my_header.hh/.hppinline void hello_world() {    std::cout << "Hello world" << std::endl;}//my_program.cc/.cpp#include <iostream>#include "my_header.hh" // /.hppint main () {    hello_world();}

If I change the order of the includes in my_program.cc/.cpp to:
#include <iostream>
The program will now fail to compile.

<iostream> must be included before using std::cout/std::endl, and <string> before std::string.

Quote:
Original post by orcfan32
Quote:

I second that, but how else would you do it?

Prefix everything that's in the std:: namespace with std:: (e.g. "string" -> "std::string") - if you have large complex functions which for some reason need to be implemented in a header (because you're using templates or something, for example), then I use "using namespace ___;" WITHIN A LOCAL BLOCK.

This is okay:
//must specify "std::string" herevoid foo () {    using namespace std;    //may use "string" or "std::string" here}//must specify "std::string" here

This is not:
using namespace std;void foo () {}

(My opinion/2 cents)

Quote:
 #ifndef __FUNCTIONS_H#define __FUNCTIONS_Hint CreateWND(string Title, int Width, int Height);#endif

Quote:
 The MSDN:Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.

My convention for include guard names is IG_PROJECTNAME_HEADERNAME (IG stands for Include Guard).

This code should work:
#ifndef IG_EXAMPLE_FUNCTIONS#define IG_EXAMPLE_FUNCTIONS#include <string>int CreateWND( std::string Title, int Width, int Height);#endif //ndef IG_EXAMPLE_FUNCTIONS

##### Share on other sites
I tried your source code, but it's still giving me the same errors, I tried the same code in different header and......it worked...?

##### Share on other sites
You must have multiple copies of the same header in the project path.
In other words the one you are changing isnt the one the compiler is finding.