I'm trying to create a list of objects that change name every time they are created.
I.e, Square1, Square2, Square3
For some reason it's giving me the error over "square_name->Sname("name_one");"
red line under square_name, says "must have pointer-type"
I've tried all different types of things, but nothing works not even just using a default string like "string test = "test"" - doesn't work..
1 2 3 4 5 6 7 8 9 10 11 12 13
string part1 = "square"; //for the name of the square
char part2 = '0'; //for adding too the name of the square
//adding the strings together------------------------------------
string square_name = part1 + part2; ;//adding them together for the name i.e. = square1, square2, square3
//the name of square_name is cauing the problem
CSquare* square_name = new CSquare;
square_name->Sname("name_one");
part2++;
You're either looking for arrays/vectors or a map.
Variable names only exist for the compiler and the programmer, they no longer exist in the final program - at that stage, variables are nothing more than values in memory or CPU registers.
If you want a sequence of squares:
1 2 3 4 5
vector<CSquare> squares;
squares.resize(10);
//now you have ten squares squares[0], squares[1] ... squares[9]
squares.push_back(CSquare());
//now you have 11
If you want squares that have arbitrary "names":
1 2 3 4
map<string,CSquare> squares;
squares["square_one"]=CSquare(); //if you only call the default constructor anyway, squares["square_one"]; would suffice
squares["square_two"]=CSquare();
[...]
The Squares are for a monopoly game, each square is an object with it's own values like name, property number etc.
I've used a list to stored the squares because I'm loading the details about them from a .txt file + the squares need/with details need to be accessible in the main.
Also it needs to be dynamic, which why I have no decided a list length, I've used to string too name each square in the list.
Can I use the vector method too create each square object?
Yes, a vector is actually more suitable than a list here, as you'll probably want random access to the squares.
You can just keep adding squares with push_back() while you read them from the file. With squares.back() you can access the last object in the vector (to set the details). You can also create a temporary square, set the details on it and then push it into the vector.
You wouldn't. With a vector, you can only access the objects by their index, e.g. squares[1].
If you want to access them by name, you'll need a map. If you want both, you can use a combination, i.e. a vector with the actual squares and a map that maps the names to their indexes in the vector.
Alternatively, you can use std::find (or a manual loop) to find the square with the desired name in the vector.
In either case, this should be details of a class that provides getSquareByName and getSquareByIndex. Then you won't have to change any other parts of the programs if you decide to change how the squares are stored.
You aren't actually initializing that correctly at all.
First, I don't thing you need any pointers to squares here.
Second, you aren't actually assigning anything to the vector, which means on lines 6/7 you aren't accessing valid memory.
One would have to see the actual code to see what's wrong.
There's still no need for pointers as long as CSquare objects are copyable.
Even if they were not, one would have to use ptr_vector (not part of the standard library) to stay in conformance with RAII.
This isn't the actual code, but it's a copy. (I can't display the actual code due university as they got this checker system for when i hand in the project :-/)
Basically what I'm saying is, when I try to output the first test[0] it displays the last object always!
You must perform allocation with new before each push_back. Also, you must free them when the portion that uses them no longer needs them, by iterating the vector and calling delete on each. Like this:
#include <iostream>
#include <vector>
usingnamespace std;
class test
{
public:
int one;
int two;
};
void squares(vector <test*> &i)
{
test* newtest = new test();
int y = 11;
int u = 12;
newtest->one = y;
newtest->two = u;
i.push_back(newtest);
newtest = new test();
newtest->one = 2;
newtest->two = 3;
i.push_back(newtest);
}
int main()
{
typedef vector<test*>::iterator iter_t;
vector<test*> test;
squares(test);
cout << test[0]->one;
for(iter_t it = test.begin(); it != test.end(); ++it)
{
delete *it;
}
}
I have tried to change your code as little as possible, working around the fact that you name a local variable identically to your global class type :)
Another approach is to use vector of values, not pointers, but that will depend on your plans.
I plan too have a list of objects, their values can change though out the program, I have no idea what the difference between vector of values or vector of pointers is.
A vector of values stores the actual objects, and thus you don't have to use new/delete. Storing pointers means that you will have to use new/delete on them, but can use polymorphism, etc.