Struct problems

I'm having some troble with structs.
It says sword_rusted does not name a type;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef ITEMS_H_INCLUDED
#define ITEMS_H_INCLUDED

#include "Item.h"

#define SWORD 0

struct ITEM
{
    const char* name;
    const char* itemAddr;
    int TYPE;
};

ITEM sword_rusted;
sword_rusted.name = "";

#endif // ITEMS_H_INCLUDED 
You can't put statements (other than declarations) outside functions like that. Put line 16 in a function or make sure to initialize the data members when you define sword_rusted on line 15.
 
ITEM sword_rusted = {"", "", 0};
move lines 15 and 16 into "something" e.g. i've moved them into main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define SWORD 0

struct ITEM
{
	const char* name;
	const char* itemAddr;
	int TYPE;
};




int main()
{
	ITEM sword_rusted;
	sword_rusted.name = "";

	return 0;
}


and it compiles fine for me.

Are you doing this in C or C++?
Last edited on
Ok thx, tho i changed it to a class and it works fine xD
In addition to what others have said, beware of doing

sword_rusted.name = "";


You're not creating your own storage for that string -- you're just setting the pointer to point to the area of memory containing the string literal for an empty string. This may have unintended consequences later, if you try and do any manipulation of the string.
tho i changed it to a class

then you're doing this in c++. In that case (if you are allowed to) i'd use std::string to hold the item name.
Topic archived. No new replies allowed.