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

## 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 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 on other sites
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!

##### 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 on other sites
pass by const reference.

##### Share on other sites
Hey bud,

#include <string>using std::string;

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

Hope that helps,

Dave

##### 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 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 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.

Dave

##### Share on other sites
Quote:
 Original post by DaveI 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 on other sites
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.

##### 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 on other sites
Quote:
 Original post by Servant of the LordOkay, 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 on other sites
Quote:
 Original post by Servant of the LordOkay, 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 on other sites
Quote:
 Original post by DaveIn 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 on other sites
Yeah, its just to do with grouping code and makeing code less ambiguous.

Dave

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628659
• Total Posts
2984079

• 10
• 9
• 9
• 10
• 21