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. :)

Comments
Excellent first post.

The problem is your copy constructor, for client.

So let us see how we get to that :
Line 5 of youf first program uses this: vektori.push_back(Client(account, firstName, lastName, balance)); This constructs a client object and copies it into the vector. With this line
you also call Client(const Client&); .

At this point you have a probelm: your Copy constructor is this

Client::Client(const Client& client) {};
// This does NOT copy the account number / the strings etc

Try writing this instead:

Client::Client(const Client& client) : 
   account(client.account),firstName(client.firstName),
   lastName(client.lastName),balance(client.balance)
{
   std::cout<<"Confirmation that copy constructor called"<<std::endl;
}

I would STRONGLY recommend adding an assignment operator. If you do not the compiler will add one for your AND because you are using string it will be 100% certain to be wrong [std::string allocates memory]. Always add a assignment operator unless you are 100% certain about not needing it. [Most of the time just add it anyway.]

It would be something like this:

Client&
Client::operator=(const Client& C)
{
   if( this!= &C)
     {
       account=C.account;
       firstName=C.firstName;
       lastName=C.lastName;
       balance=C.balance;
     }
    return *this;
}

p.s. That is an extremely well set out first post. Clear as to the problem, sufficient code to replicate the problem, but not excessive, and uses code tags. Thanks!

Edited 6 Years Ago by StuXYZ: n/a

Wow, thanks! That really solved my problem. I just wonder, is it possible to create new Client objects and insert them to the vector without using the copy constructor at all?

Also, my knowledge about assignment operator usage is next to nil, but I think I can work it out. It seems to work the strings out nicely without, but I'll study it further and do it just for learning. Thanks again for your time and effort!

EDIT:
Actually, I figured it just out, basically just writing the assignment version over the earlier copy constructor. It seems to work fine, and, of course, it now protects itself for unintended self-assignments. Now, on to the next quest :)

Edited 6 Years Ago by bleedi: Found out the solution!

This question has already been answered. Start a new discussion instead.