Tuesday, July 14, 2009

What is wrong with this source code? (C++) (Not homework, it is my own project, and I am not a student)?

I am writing a turn based combat game (text based // console) using C++. It is to be featured on my website ONLY. However, I have just started teaching myself how to use structs and vectors. Somehow, I think that the primary thing causing the errors is the two dots in the struct/vector thing





ex: myStruct.myVector[i].push_back(myString)...





Something in the source code won't let me use the push_back function built into vector. Can someone please help me or tell how to fix this problem? Example code would be helpful, but please make sure to include //comments as well.





Thank you to all who answer.





Here is the source code that I have written so far:

What is wrong with this source code? (C++) (Not homework, it is my own project, and I am not a student)?
You wrote: yMain.Name[i].push_back(Name);





yMain.Name is a vector. Name[i] is an element of the vector. Name.push_back will add the string Name (unfortunately having the same name as the vector) to the end of the Name vector. Unless this is a vector of vectors, Name[i].push_back is invalid.





Get rid of those subscripts, and you should eliminate a lot of the compile errors.
Reply:Unfortunately, its hard to see where your error is without a specific error message or if its a compiler or runtime error.





Some observations:


1) Consider using classes for your ships, and either use a constructor that takes an ofstream %26amp; or a factory class





2) For larger projects, post on a code sharing web site. This site ruins your formatting

bloom

No comments:

Post a Comment