Jump to content
  • Advertisement
Sign in to follow this  
dhulli

Problems with the string class

This topic is 3660 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

Hi, I don't know what I am doing wrong here. I include the string.h file in a header file of my own. In which there is a class in which i declare a string member variable. like: string str; which gives the errors: f:\ogl\hge\rpg\player.h(15) : error C2146: syntax error : missing ';' before identifier 'str' f:\ogl\hge\rpg\player.h(15) : error C2501: 'string' : missing storage-class or type specifiers And if I declare a string variable as an argument to a function of the class like: void func(string str); then it gives the following error: f:\ogl\hge\rpg\player.h(14) : error C2061: syntax error : identifier 'string' Could someone tell me what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Sounds like you missed out the std::

std::string

or not included the header.

Share this post


Link to post
Share on other sites
Show us more context. Remember that class and struct definitions always have to end with a semicolon. If you forget that, you can get very confusing error messages.

class X
{
...
}; <--- very important semicolon

Share this post


Link to post
Share on other sites
Did you mean to

a) use std::string instead of string (or to import its namespace)?
b) include <string> instead of <string.h>?

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>

int main()
{
std::string Text;
Text = "Hello String";
std::cout << Text << std::endl;
}

Share this post


Link to post
Share on other sites
Import it like this:

#include <string>

Instantiate it like this:

std::string str;

Similarly with functions:

void foo(std::string s);

Share this post


Link to post
Share on other sites
Just for completeness of clarification, <string.h> was the old name for the C standard library string routings (strlen, strcat etc), now called <cstring> with the functions in the std namespace.

The C++ string class has always been in <string>.

For backwards compatibility, most C++ compilers still support the old style C includes like <stdio.h> instead of <cstdio> and <string.h> instead of <cstring>, generally importing the names in global scope rather than in std.

So, #include <string.h> normally includes the old-style C string functions into the global scope, not the C++ std::string class.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!