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: typedefint 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
typedefstruct 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?
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.
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.
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?
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}’
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;
};
typedefint 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
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.