header problem

Started by
13 comments, last by dave 18 years ago
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++.
Advertisement
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
Dave, using yours I get these messages:
"'string' was not declared in this scope"
"parse error before ')' token"

What am I doing wrong? Thanks for your help!
'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.
--Riley
pass by const reference.
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
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!
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
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.
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.

This topic is closed to new replies.

Advertisement