String error with header file

I'm working on an RPG (text based) for a school project. My teacher suggested using a header file for declearing the items side of things. The problem occurs in the data structure of the item:

1
2
3
4
5
6
7
8
struct itemdata
{
string iname //this is where the problem is.
itemtype itype //itemtype is a data type I made.
int idefence
int iattack
...
}irondagger ;

I included string before itemdata in both the main file and the header in all the different combinations just in case it mattered.

Also I am going to use a character array instead of a string; just want to know why it doesn't work.
You might have circular inclusion; We'll need to see more code to find out.

Also:
I am going to use a character array instead of a string

Why?
you realize you are missing the semicolons right?

if that isn't the case did you declare the std namespace?

string isn't a data type, it more synonymous to a container.

You probably need to either do

1
2
3
4
5
6
7
#include <string>

struct itemdata
{
       std::string itype;
       ....
} irondagger;


or

1
2
3
4
5
6
7
8
#include <string>

using namespace std;
struct itemdata
{
      string itype;
      ....
} irondagger;


or

1
2
3
4
5
6
7
8
9
#include <string>

using std::string;

struct itemdata
{
      string itype;
      ....
} irondagger;


The first then the third are usually recommended when declaring in the header file because the using declarations are carried over to all files that include that header file and its considered poor coding etiquette/practice to cloud the namespaces in other files.

And I agree with firedraco, a character array is not recommended. the string class has member functions that can convert them if needed and you will lose alot of simple, dynamic functionality.

Best of luck
Thanks, I'll try that. As with the character array; thought it might use less memory.

1
2
3
4
5
6
7
#include <string>

struct itemdata
{
       std::string itype;
       ....
} irondagger; 


I thought that string wasn't part of std.
Last edited on
Topic archived. No new replies allowed.