Doesn't Include STRING.H

Started by
4 comments, last by Squeejee 22 years, 6 months ago
I am using MSVC++ 6.0 trial version and I am trying to better my skills at C++ by making a little text-based RPG. Right now, I am working on the battle engine, and I am strings to hold the player’s and the monsters’ names. I am having a lot of errors come up saying that the string data type is undefined, even though I have included the STRING.H file. This is the code from the file that these errors are arising:

#include "iostream.h"
#include "string.h"

//DATA TYPES
struct character
{
  string name;
  short  maxhp;
  short  maxmp;
  short  atk;
  short	 amr;
  short  agi;
  short  str;
  short  def;
};


//CHARACTERS
character you =
{
  youname,
  100,
  0,
  4,
  0,
  80,
  5,
  0
};

//MONSTERS
character gnome =
{
  GNOME,
	100,
	0,
	3,
	0,
	75,
	5,
	0
};
 
And here are some of the errors I am getting: error C2146: syntax error : missing ';' before identifier 'name' error C2501: 'string' : missing storage-class or type specifiers error C2501: 'name' : missing storage-class or type specifiers error C2065: 'youname' : undeclared identifier error C2078: too many initializers error C2065: 'GNOME' : undeclared identifier error C2078: too many initializers error C2011: 'character' : 'struct' type redefinition error C2374: 'you' : redefinition; multiple initialization see declaration of 'you' error C2078: too many initializers error C2374: 'gnome' : redefinition; multiple initialization see declaration of 'gnome' error C2078: too many initializers Edited by - Squeejee on October 11, 2001 10:23:16 PM
-----
Advertisement
Your problem is that string.h does not define a type named "string". String.h is a header file from the old days of plain of C. Back then you would use a character array (i.e. char name[50]) to hold strings. You would then have to use strcpy() (which is defined in string.h) to copy things into your character array (i.e. strcpy(name, "GNOME")) You can't use the assignment operator (=) on a character array (so you can't do name="GNOME"; )

Now, if you're talking C++, then you have a nice STL (STL is a standard set of libraries for C++) type, named "string". To use it you need to #include the header file "string" (NOT "string.h"). With the string type, things like assignment (i.e. name="GNOME"; ) IS allowed.

Hope this helps.


EDIT: Formatting wasn't proper...

Edited by - Worf on October 11, 2001 10:31:11 PM
...keeping it simple...
You should include <string> (i.e. without the .h).
string.h contains functions for manipulating standard C-style strings (char *).
You need:

      #include <string>using namespace std;      


To get this to work... Gotta have both.

Edited by - DoomX on October 12, 2001 2:38:42 AM
There are 10 kinds of people in the world: those who get binary, and those who don't...
Okay, thanks guys, that worked. But now I have another error that pops up. It says "error C2552: 'you' : non-aggregates cannot be initialized with initializer list".

    #include <iostream>#include <string>using namespace std;//DATA TYPESstruct character{  string name;  short  maxhp;  short  maxmp;  short  atk;  short	 amr;  short  agi;  short  str;  short  def;};//CHARACTERScharacter you ={ <------------------------------------ ERROR  youname,  100,  0,  4,  0,  80,  5,  0};//MONSTERScharacter gnome ={  GNOME,	100,	0,	3,	0,	75,	5,	0};    


Edited by - Squeejee on October 12, 2001 3:42:59 PM
-----
Structure initialization lists only work for structures of simple data types. Here is the wonderous information that can be found from the F1 key (which is much more accessible than the "Post Reply" key, but that most people seem to neglect):
quote:
Compiler Error C2552
'identifier' : non-aggregates cannot be initialized with initializer list

The specified identifier was incorrectly initialized.

An initializer list is needed to initialize the following types:

- An array


- A class, structure, or union that does not have constructors, private or protected members, base classes, or virtual functions

These types are known as “aggregates.”

string violates the second bullet, having almost all of these things (constructors, private members, etc.), so it's a non-aggregate. You can't use an initializer list.

Edited by - Stoffel on October 12, 2001 4:51:46 PM

This topic is closed to new replies.

Advertisement