If I have 2 classes

a) class Business
b) class Customer

I want the Business class to be able to store a dynamic list of it's customers. So I thought the easiest way to do this would be to use a vector.

class Business
{
    vector<Customer> customers;
public:
    Business();
};

Business::Business()
{
    //I want to be do something like this, but it's not working
    customers.push_back(Customer("Name1"));

}

My question is how do I get Customer objects into my vector?

What do you mean by "it's not working"? Are you getting a compile time error? A run time error? "It's not working" is not a helpful description of the problem.

Compile time error...

manager.cpp: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/ios_base.h:738: error: `std::ios_base::ios_base(const std::ios_base&)' is private
manager.cpp:18: error: within this context
manager.cpp: In copy constructor `std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/streambuf:769: error: `std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
manager.cpp:18: error: within this context
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc: In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:238: instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Client, _Alloc = std::allocator<Client>]'
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/stl_vector.h:564: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Client, _Alloc = std::allocator<Client>]'
manager.cpp:18: instantiated from here
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/ios_base.h:741: error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:238: error: within this context
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc: In member function `std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >& std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::operator=(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/streambuf:776: error: `std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:238: error: within this context

This is my code:

#ifndef MANAGER_H
#define MANAGER_H

#include "dvd.h"
#include "client.h"
#include <vector>

class Manager
{
	std::vector<Client> clients;
public:
	Manager();
};

#endif

#include "manager.h"
#include "dvd.h"
#include "client.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

Manager::Manager()
{
	//Works great
	Client c(101, "Jill", "123 Sesame St.");
	c.addMovie("Some Great Movie");
	cout << c.getInfo();
	
	//Compile time error
	clients.push_back(Client(101, "Bill", "88 Whatever St."));
}

In any case, I can create individual clients I just can't push them onto a vector without causing a compile time error. I have no idea what I'm doing wrong.

Can you post the definition of the Client class? It looks like the problem is a field that does not have copy semantics, and the vector class requires those semantics.

#ifndef CLIENT_H
#define CLIENT_H

#include <vector>
#include <string>
#include <iostream>
#include <sstream>

class Client
{
	int id;
	double lateFee;
	std::string name, address, clientData, stringID, stringLateFee;
	std::vector<std::string> movies;
	std::stringstream toString;
public:
	Client(int cId, std::string cName = "Default", std::string cAddress = "Default");
	~Client();
	
	void setName(std::string cName);
	void setAddress(std::string cAddress);
	void addMovie(std::string cMovie);
	void addLateFee(double cLateFee);
	void payLateFee(double cLateFee);
	
	std::string getInfo();
	double getLateFee() const;
};

#endif

A stringstream is still a stream, and stream objects in C++ do not have copy semantics. You can make that field a reference or a pointer if the stringstream is a necessary instance variable. Or you can recreate the stream each time you need it, but that would depend on what you are using the stream for.

commented: You're a life saver. Thank you. +1

_base(const std::ios_base&)' is private
manager.cpp:18: error: within this context.

I don't think your problem is on the stream. It may be, but look at your error. Obviously, the entirety of it isn't applicable but you should have some insight as to what caused it at the top. Check line 18 of your code. It seems like you are trying to access a private class member. I see you have no access designation for your clients list (vector). Also, could you provide which line of code is line 18 in manager.cpp? That seems to be the problem child. It looks like the vector class is not liking the object passed to it. What happens when you call:

clients.push_back(c);

instead?

Thanks, I haven't learned about copy semantics yet so I had no idea. I'm using that variable to convert some integers to strings so I will need it in the object, but I'm sure that now I can get it working. Thanks for the help. This was driving me nuts all evening.

I temporarily removed the stringstream variable and the function that depends on it. It compiles great now. Problem solved.

Thanks for the replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.