Click to See Complete Forum and Search --> : string


hexaplus
November 28th, 2004, 12:44 AM
Hi, I'm having some trouble using the string data type I'm very new to c++:

I would like to have a class with a non sataic data member of type string. as defined in #include <string>

Here is the jist of what I'm trying to do:


#include <string>
using namespace std;

class treeNode
{
public:
//lots of methods
private:
string nodeElement = "text";// a string that is in each node of a tree

}

Marc G
November 28th, 2004, 02:45 AM
You can't initiliaze your string in your .H file. Do something like:
.h#include <string>
using namespace std;

class treeNode
{
public:
//lots of methods
private:
string nodeElement;
}
.cpptreeNode::treeNode() : nodeElement("text")
{
...
}

Andreas Masur
November 28th, 2004, 02:57 AM
#include <string>
using namespace std;

As a small side note...it is not a good idea to map the complete namespace 'std' to your application in a header file (with 'using namespace std'). Every file that includes this header will get everything out of the namespace as well which is not desirable...

The following shows you the four different methods to map a namespace...

// Using the full member name, including the namespace it belongs to:
std::cout << "Hello World";


// By taking advantage of Using-Declarations:
using std::cout; // This declares cout in the current
// scope as synonym for std::cout
cout << "Hello World";


// By taking advantage of Using-Directives:
using namespace std; // Which specifies that the current
// scope can refer to names in the
// 'std' namespace without using
// full qualifiers. This is mostly
// used when porting legacy code.
cout << "Hello World";


// Using aliases:
namespace X
{
namespace Y
{
class Z { ... };
}
}

X::Y::Z // The full qualifier for 'Z' is
// 'X::Y::Z'

namespace w = X::Y; // This declares 'w' as an alias for
// namespace 'X::Y'

w::Z // Access 'Z' using 'w::Z'

Marc G
November 28th, 2004, 03:02 AM
As a small side note...it is not a good idea to map the complete namespace 'std' to your application in a header file (with 'using namespace std'). Every file that includes this header will get everything out of the namespace as well which is not desirable...
You are absolutely correct. I just copied/paste the code and didn't pay attention to that using namespace ;)

hexaplus
November 28th, 2004, 04:53 PM
thanks

hexaplus
November 28th, 2004, 04:58 PM
so something like this in the stead:

using std::string;

wien
November 28th, 2004, 06:29 PM
Nope.. Not quite. That will also pollute the global namespace. In your header, specify the std namespace explicitly on every string you declare:std::string some_string;In your cpp file however, you can bring in as many namespaces as you like since it will only pollute the global namespace of that particular file. (And not all files that include it like it would in a header.)

Andreas Masur
November 29th, 2004, 03:12 AM
Nope.. Not quite. That will also pollute the global namespace.
Well...actually, it will only declare 'string' in the current scope as a synomyn for 'std::string'...in other words, it does not map the complete namespace to your application... :cool:

wien
November 29th, 2004, 06:20 AM
Well...actually, it will only declare 'string' in the current scope as a synomyn for 'std::string'...in other words, it does not map the complete namespace to your application... :cool:Well, you do bring that "string" name in there.. ;) It's not as bad as putting "using namespace" in the global scope, no, but should still be avoided.

However, if you put the "using" line inside the scope of an inline function, that is, not in the global scope, it's a completely different matter.