vlg
asked on
newbie string question
Hello all
I'm trying to use strings because I feel more comfortable with them - char* stuff is still a bit of a mystery.
This is using g++ no a Unix box.
Anyways, I have a header file - this.h:
#ifndef THIS
#define THIS
class x
{
public:
bool this();
int that(int);
string theOther(string); //call this line 5
bool someThing(string); //line 6
}
#endif
and a .cc file - this.cc:
#include <string.h>
#include "this.h"
string this::theOther(string str) //call this line 7
{
//...
}
when I try to compile this.cc, the compiler says
this.h:5: syntax error before '('
this.h:6: 'string' was not declared in this scope
this.h:6: invalid data member initialization
this.h:6: use '=' to initialize static data members
this.cc:7: syntax error before '::'
Why all the grief about strings?
I even tried adding #include <string.h> to the .h file, but that didn't help.
The points go to whomever can tell me how to get strings working in this context.
Thanks
v
I'm trying to use strings because I feel more comfortable with them - char* stuff is still a bit of a mystery.
This is using g++ no a Unix box.
Anyways, I have a header file - this.h:
#ifndef THIS
#define THIS
class x
{
public:
bool this();
int that(int);
string theOther(string); //call this line 5
bool someThing(string); //line 6
}
#endif
and a .cc file - this.cc:
#include <string.h>
#include "this.h"
string this::theOther(string str) //call this line 7
{
//...
}
when I try to compile this.cc, the compiler says
this.h:5: syntax error before '('
this.h:6: 'string' was not declared in this scope
this.h:6: invalid data member initialization
this.h:6: use '=' to initialize static data members
this.cc:7: syntax error before '::'
Why all the grief about strings?
I even tried adding #include <string.h> to the .h file, but that didn't help.
The points go to whomever can tell me how to get strings working in this context.
Thanks
v
Also your return types don't match.
You're also missing a simicolon at the end of your class declaration.
For starters, here how you can setup your header.
#ifndef MY_HEADER_GAURD_FOR_X_CLAS S
#define MY_HEADER_GAURD_FOR_X_CLAS S
#include <string> //Link to <string> and NOT <string.h>
class x
{
public:
bool SomeNameOtherThenThis();
int that(int);
std::string theOther(std::string); //Use std:: namespace when using stl code in header
bool someThing(std::string); //Use std:: namespace when using stl code in header
};
#endif //MY_HEADER_GAURD_FOR_X_CL ASS
#ifndef MY_HEADER_GAURD_FOR_X_CLAS
#define MY_HEADER_GAURD_FOR_X_CLAS
#include <string> //Link to <string> and NOT <string.h>
class x
{
public:
bool SomeNameOtherThenThis();
int that(int);
std::string theOther(std::string); //Use std:: namespace when using stl code in header
bool someThing(std::string); //Use std:: namespace when using stl code in header
};
#endif //MY_HEADER_GAURD_FOR_X_CL
continue ....
#include <string> //Use <string> instead of <string.h>
#include "my_header.h"
std::string x::theOther(std::string str)//change "this" to x
{
//...
return "";
}
#include <string> //Use <string> instead of <string.h>
#include "my_header.h"
std::string x::theOther(std::string str)//change "this" to x
{
//...
return "";
}
ASKER
Axter -
Thanks for the help.
One quick question:
you said that in the .cc file I also have to:
std::string x::theOther(std::string str)//change "this" to x
{
//...
return "";
}
even though I'm including the library file:
#include <string> //Use <string> instead of <string.h>
?
I'm obviously going to follow the rules, but I'm curious as to why I have to use
std::string x::someFunction(std::strin g str)
when I #include<string> at the top of the file?
What good is including the file doing for me?
v
Thanks for the help.
One quick question:
you said that in the .cc file I also have to:
std::string x::theOther(std::string str)//change "this" to x
{
//...
return "";
}
even though I'm including the library file:
#include <string> //Use <string> instead of <string.h>
?
I'm obviously going to follow the rules, but I'm curious as to why I have to use
std::string x::someFunction(std::strin
when I #include<string> at the top of the file?
What good is including the file doing for me?
v
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
close -
what I meant was, if I'm #including it in the header file, then:
1) why does it also (as in your example above) also have to be in the .cc file, and if it is in either/both places, why do I still have to preface all the string stuff with "std::"?
that's the only thing - I'm just curious - the points will be yours whether you know the answer or not, so no need to make something up if you don't know - just curious
thanks
v
what I meant was, if I'm #including it in the header file, then:
1) why does it also (as in your example above) also have to be in the .cc file, and if it is in either/both places, why do I still have to preface all the string stuff with "std::"?
that's the only thing - I'm just curious - the points will be yours whether you know the answer or not, so no need to make something up if you don't know - just curious
thanks
v
ASKER
weird - now I can't see that comment you added about having to check the string length or something with stol(?) - it was similar to the comment posted by the other person - and I can't see his comment now, either!
Yikes - can you please repost it, Axter? - I'll never figure out what you were talking about without that comment.
thanks
Yikes - can you please repost it, Axter? - I'll never figure out what you were talking about without that comment.
thanks
ASKER
oops - ignore that last request for a repost - my bad
only the question about the inclusion remains.
v
only the question about the inclusion remains.
v
ASKER
if you have the time - i'm stil curious about the question...
but thanks in any event!
but thanks in any event!
>>why does it also (as in your example above) also have to be in the .c
It doesn't need to be in both. You can just keep it in the *.h file.
>>why do I still have to preface all the string stuff
>>with "std::"?
You need the prefix because it's in the std namespace.
If you don't want to use the prefix you can use a using-statement.
Example:
using namespace std;
But I don't recommend that you do this in the header. Only in the *.cpp file.
You should always avoid bring a namespace into the gobal area in the header, because this conficts with the author's intentions.
It doesn't need to be in both. You can just keep it in the *.h file.
>>why do I still have to preface all the string stuff
>>with "std::"?
You need the prefix because it's in the std namespace.
If you don't want to use the prefix you can use a using-statement.
Example:
using namespace std;
But I don't recommend that you do this in the header. Only in the *.cpp file.
You should always avoid bring a namespace into the gobal area in the header, because this conficts with the author's intentions.
Change the name to something else besides "this"