Jump to content
  • Advertisement
Sign in to follow this  
Servant of the Lord

header problem

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

If you intended to correct an error in the post then please contact us.

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

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
Guest Anonymous Poster
pass by const reference.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!