hello.. just started learning cpp so my question might be bit stupid.. sorry 4 that.
I have a functions creating a server with specific Id and putting it in vector of servers. Now, the type is defined in the constructor as 's' for server:
pretty much the same thing I have for my printers but I have 'p' in the type.
now - when I do the cout right after creating the server in the first code I get type 115 stands for 's'.
when checking it with debug/expression view with breakpoint on the return myDevices lets say myDevices[0]->type I get -1079945296
myDevices[1]->type I get 0
myDevices[2]->type I get 125713
Now Found another thing - I have 4 line in my file - 3 of them adding new servers and the 4 one is adding new printer. myDevices[3]->type ( or any other property like Id for that matter ) is returning me ( in the expression view ) Target request failed: Cannot access memory at address 0x0 As if it doesn't exists but I pushback printers also:
can someone please help me figure it out - been searching the net 4ever.
So It actually 2 questions since I'm not sure how to use vectors.
1. How come I get garbage right after push back.
2. Where have I lost my printer?
Code:
class Parser{
public:
static vector<device*> parseConfiguration(ifstream& configFile) {
string line;
vector<device*> myDevices;
..
...
...
if (device.compare("server") == 0) {
int bufferSize;
Id = helpFunctions::stoi(lineInVector[1]);
name = lineInVector[2];
bufferSize = helpFunctions::stoi(lineInVector[3]);
server newServer(Id, name, bufferSize);
newServer.setSerialId(myDevices.size());
myDevices.push_back(&newServer);
cout << newServer.serialId << endl;
cout << newServer.type << endl;
}
...
....
...
}
return myDevices;
}
Code:
server::server(int Id, string name, int bufferSize)
{
this->type = 's';
this->Id=Id;
this->name=name;
this->bufferSize=bufferSize;
}
now - when I do the cout right after creating the server in the first code I get type 115 stands for 's'.
when checking it with debug/expression view with breakpoint on the return myDevices lets say myDevices[0]->type I get -1079945296
myDevices[1]->type I get 0
myDevices[2]->type I get 125713
Now Found another thing - I have 4 line in my file - 3 of them adding new servers and the 4 one is adding new printer. myDevices[3]->type ( or any other property like Id for that matter ) is returning me ( in the expression view ) Target request failed: Cannot access memory at address 0x0 As if it doesn't exists but I pushback printers also:
Code:
if (device == "printer") {
int serverId;
int paperAmount;
string type;
Id = helpFunctions::stoi(lineInVector[1]);
name = lineInVector[2];
serverId = helpFunctions::stoi(lineInVector[3]);
paperAmount = helpFunctions::stoi(lineInVector[4]);
type = lineInVector[5];
//if(helpFunctions::IsServerAvailable(myDevices, serverId)){
printer newPrinter(Id, name, serverId, paperAmount, true);
if (type == "BLACK")
newPrinter.setColor(false);
newPrinter.setSerialId(myDevices.size());
myDevices.push_back(&newPrinter);
cout << newPrinter.type << endl;
//}
}
So It actually 2 questions since I'm not sure how to use vectors.
1. How come I get garbage right after push back.
2. Where have I lost my printer?
Comment