Hi, I'm studying C++ at the university and came across a problem in my exercises. Basically I'm trying to retrieve an integer from an object via its getter method. The object is stored in a vector.
Here's the main.cpp part:
vector<Client> vektori;
...
vektori.push_back(Client(account, firstName, lastName, balance));
...
vector<Client>::const_iterator iter = vektori.begin();
const vector<Client>::const_iterator end = vektori.end();
ofstream file("clients.txt");
while(file && iter != end) {
file << (*iter).getAccount() << endl;
*iter++;
}
file.close();
This compiles and links alright, but it retrieves a random, seven-digit integer, not the one stored in the object. I've tried changing (*iter).getAccount() in many ways, but this is the only way it has compiled so far.
Here's the Client.h:
#ifndef CLIENT_H
#define CLIENT_H
class Client {
private:
int account;
std::string firstName;
std::string lastName;
double balance;
public:
Client(int account_par, std::string firstName_par,
std::string lastName_par, double balance_par);
Client(const Client& client);
~Client();
int getAccount() const;
double getBalance();
std::string getName() const;
};
#endif
... and here's the Client.cpp:
#include <string>
#include "Client.h"
Client::Client(int account_par, std::string firstName_par, std::string lastName_par, double balance_par) {
account = account_par;
firstName = firstName_par;
lastName = lastName_par;
balance = balance_par;
};
Client::Client(const Client& client) {};
Client::~Client() {};
int Client::getAccount() const {
return account;
};
double Client::getBalance() {
return balance;
};
std::string Client::getName() const {
return firstName + " " + lastName;
};
I would be really thankful if you guys could help me out here. :)