Converting from a typedef to a struct

I was recently given the task in my class to change a bit of code(included below) from a typedef to a struct. My teacher did not really go over this process in class, so I'm not 100% sure how to do it. What I found on google was somewhat confusing.

The original typedef is:
typedef int ListItemType;

The struct has to have three parts: a word, the id, and the next id. This is what I have:
1
2
3
4
5
typedef struct ListItemType{
	int ID;
	int nextID;
	string word;
    } item;


That seems to blow up my code, so could anyone give me a quick overview on the conversion?

Any help would be greatly appreciated.

Thanks,
Chris
I think your teacher just means that you should write the struct without the typedef.
1
2
3
4
5
struct ListItemType {
	int ID;
	int nextID;
	string word;
};
His exact words are "Create a struct that holds just the three fields for a single item. Then modify the typedef for ListItemType to be this struct." Wouldn't removing the word typedef be deleting it, not modifying it? I'll ask in class just to make sure.

Thank you for your quick reply
When you do this:
1
2
3
4
struct MyStruct
{
    //...
};

You can do this:
1
2
MyStruct ms;
//... 


When you do this:
typedef MyStruct MyType;
You can do this same thing:
1
2
MyType ms;
//... 


The only reason to use a typedef is to change a type of something and not have to change every occurance in the code of it.

In this case for you, a typedef will not help (it will hurt) - because you originally used an int and now you are using a struct, so you will STILL have to go through and replace all your code.
cdseasholtz wrote:
His exact words are "Create a struct that holds just the three fields for a single item. Then modify the typedef for ListItemType to be this struct." Wouldn't removing the word typedef be deleting it, not modifying it? I'll ask in class just to make sure.

Presumably you are supposed to have written a bit of 'generic' code (At a guess, some kind of list?) which uses your typedef of ListItemType. If you've written that code in a truly generic way, then it shouldn't matter what the typedef is aliasing; if you haven't written it in a truly generic way (e.g. perhaps you've used int instead of the typedef in various parts of your code), then it's likely to cause errors

What does your code look like? What are the errors when you change your typedef?
Last edited on
Thank you guys for your replies. I went and talked to my teacher and he said to make a struct with the items inside of it then change the typedef to the struct name instead of int. Basically he told me to do what L B said.
i.e.
1
2
3
4
5
6
7
struct item {
    int ID;
    int nextID;
    std::string word;
};

typedef item ListItemType;


Now I'm trying to assign a value to the ID, nextID, and word. How do I access those?

I have read in an integer and stored it in a temporary holder variable(IDhold), but I can't figure out the syntax to store it in the ID variable in the struct, if that makes sense.

I have tried something along the lines of:
1
2
ListItemType *itemPtr = new ListItemType;
itemPtr.ID = IDhold

But it isn't working, any tips?
The error I get is:
error: request for member ‘ID’ in ‘itemPtr’, which is of non-class type ‘ListItemType {aka int}’
Last edited on
closed account (zb0S216C)
Use the arrow operator on itemPtr to access the members, like so: itemPtr->ID = IDhold.

Wazzak
I get essentially the same error.

error: request for member ‘ID’ in ‘* itemPtr’, which is of non-class type ‘ListItemType {aka int}’
closed account (zb0S216C)
Can you post the full code? I can't replicate you problem.

Wazzak
Here is the constructor(what I'm trying to get working):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Client::Client(std::ifstream& in)
{
    ListItemType *itemPtr = new ListItemType;
    int IDhold = 0;
    int IDnexthold = 0;
    std::string wordHold;
    
    in >> IDhold;
    in >> wordHold;
    in >> IDnexthold;
    
    itemPtr->ID = IDhold;
    
}


This is in the header:
1
2
3
4
5
6
7
struct item {
    int ID;
    int nextID;
    std::string word;
};

typedef int ListItemType;


I understand that I do nothing with the word and nextID hold, but I'm just trying to get one part working for now. I will eventually change it to a while loop so I can read in multiple items and make a linked list from an input file
Last edited on
closed account (zb0S216C)
The compiler sees ListItemType as an alias for int, not item. So when you attempt to access item::IDhold, the compiler informs you that int doesn't have the member you're trying to access. To fix this, change your type-definition to: typedef item ListItemType;

You really should use delete on itemPtr when you're done with it.

Wazzak
Last edited on
Crap. Thank you for that. I just realized that I had it written correctly in a previous post, but forgot to change it in my actual code...
Topic archived. No new replies allowed.