Sign in to follow this  
orcfan32

The wierdest header file ever?!

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 this post


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

Post the contents of your header file if you may.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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_H
int CreateWND(string Title, int Width, int Height);
#endif

Share this post


Link to post
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by orcfan32
You mean #include <string> right?

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


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
You need
#include <string>
using namespace std;

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


Bad programmer. No suggesting putting using namespace in headers. Bad, bad programmer.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by Promit
You need
#include <string>
using namespace std;

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


Bad programmer. No suggesting putting using namespace in headers. Bad, bad programmer.


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

BTW, it's still not compiling..

Share this post


Link to post
Share on other sites
This will compile:

//my_header.hh/.hpp
inline void hello_world() {
std::cout << "Hello world" << std::endl;
}

//my_program.cc/.cpp
#include <iostream>
#include "my_header.hh" // /.hpp

int main () {
hello_world();
}


If I change the order of the includes in my_program.cc/.cpp to:
#include "my_header.hh"
#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:
Original post by SiCrane
Bad programmer. No suggesting putting using namespace in headers. Bad, bad programmer.


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" here
void 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_H
int CreateWND(string Title, int Width, int Height);
#endif


Your macro name (__FUNCTIONS_H) is very bad not recommended:

Quote:
[i]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 this post


Link to post
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 this post


Link to post
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.

Do a search on your hard drive for that header file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this