Sign in to follow this  
Servant of the Lord

header problem

Recommended Posts

Hmm, I am having a slight problem with a header file. In my main project .cpp file I have this function:
string lower(string str)
{
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    return str;
}
And in my header file I have tried:
extern string lower();

extern string lower(str);

extern string lower(string str);

extern lower(string str);

extern lower(str);
And some others. It is the header line that doesn't compile; I get a 'syntax error'. I have added functions to headers before, but I am having trouble with functions that have arguments in the '()' Could someone go over this for me? I am using Dev C++.

Share this post


Link to post
Share on other sites
Hey bud,

All you should need to do is this (in the header file):

string lower(string str);

That's the function prototype for the method in the cpp file.

Hope that helps,

dave

Share this post


Link to post
Share on other sites
'string' is a type that is defined in the "string" include file. You need to have lines like

#include <string>
using namespace std;

near the top of any file in which you want to use it.


"string" is defined in the namespace called "std", so the "using namespace std;" line tells the compiler to look in that namespace if it can't find a definition in the local namespace. You can google "namespace" and C++ if you are curious about them - otherwise, just make sure to include those two lines of code in all of your files and you should be OK.

Share this post


Link to post
Share on other sites
Hey bud,

Have you got this in your header file?


#include <string>

using std::string;


You must also have header file included in the CPP file.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by rileyriley
'string' is a type that is defined in the "string" include file. You need to have lines like

#include <string>
using namespace std;

near the top of any file in which you want to use it.


"string" is defined in the namespace called "std", so the "using namespace std;" line tells the compiler to look in that namespace if it can't find a definition in the local namespace. You can google "namespace" and C++ if you are curious about them - otherwise, just make sure to include those two lines of code in all of your files and you should be OK.


std::string lower(std::string str);

This works. Thanks. I had the <string> library included, but I didn't have using namespace std; as I was told not to in headers, so I forgot the std:: part. Thanks for all the help guys! Rate++ both of you! [Edit:] err, I would rate you up, but I did already in the past it seems lol.

Thanks guys!

Share this post


Link to post
Share on other sites
I would suggest being more explicit with the using of the using keyword. It is better to do:

using std::stirng;

Than:

using namespace std;


This is because bringing in the whole namespace defies the point in having it to begin with. It doesn't cost you anything to be this explicit so i recommend doing it.

Glad to help,

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
I would suggest being more explicit with the using of the using keyword. It is better to do:

using std::stirng;

Than:

using namespace std;


In my .cpps or headers? Actaully, I never knew that you could use using std::string;. Once again, I am in your debt.

Man, without gamedev.net I would've given up programming a long time ago.

Share this post


Link to post
Share on other sites
In your header files. See the purpose on namespaces is to separate code into more distinct groups. For example, consider two people writing a class called File, or some such. If you want to use both of these, for whatever reason, then their names would be ambiguous. If you put each users class in differently name namespaces you can qualify one or the other more easily.

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Okay, on a unrelated topic this line doesn't work.
(In my .cpp file)

cin.get();

It compiles, it just doesn't wait for a key press. I could use system("pause"); but I would rather not.


Try: cin.get(); cin.get(); [wink] Some character from previous input is eating away the first cin.get() it sounds like.

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Okay, on a unrelated topic this line doesn't work.
(In my .cpp file)

cin.get();

It compiles, it just doesn't wait for a key press. I could use system("pause"); but I would rather not.


Hey,

Right well i use this to halt for a key press:


unsigned int a;
std::cin >> a;


Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
In your header files. See the purpose on namespaces is to separate code into more distinct groups. For example, consider two people writing a class called File, or some such. If you want to use both of these, for whatever reason, then their names would be ambiguous. If you put each users class in differently name namespaces you can qualify one or the other more easily.

Dave

Ahh..., I think I understand. So if there is two type of classes named 'cout' I would using namespace std::cout; so I don't have to type 'std::' on one type, but still type whatever::cout whenever I use the second class 'cout'?

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