use vector as reference

hello,

I'm writing a programme and I want to pass vector as reference.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
#include "Customers.hpp"
using namespace std;

std::vector<Customer*> customerList;

int main()
{
    string name;
    int id;

    cout << "Please enter customer name:" << endl;
    getline(cin,name);

    // new Customer
    Customer * customer = new Customer(name);
    customerList.push_back(customer);

// show all results
    for(int i=0; i<customerList.size(); i++)
    {
        cout << "name" << "\t" << "id" <<  endl;
        cout << customerList.at(i)->getName() << "\t" << customerList.at(i)->getId() << endl;
        cout << endl;
    }


return 0;
}


Customers.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef CUSTOMERS_HPP
#define CUSTOMERS_HPP
#include <iostream>
#include <string>
using namespace std;

class Customer
{
    static int customerId;
    string customerName;

public:
    Customer(string name);
  //  void addCustomer(string name);
    void showCustomerInfo(int id);
    void sortCustomerByName(string name);
    void listAllCustomers();
    string getName();
    int getId();
    void deleteCustomer(int id);
   // ~Customer();
};

#endif


Customers.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "Customers.hpp"

/*
    Customer(string name);
    void addCustomer(string name);
    void showCustomerInfo(int id);
    void sortCustomerByName(string name);
    void listAllCustomers();
    ~Customer();
*/

Customer::Customer(string name){

    this->customerName = name;
    customerId++;
};

void Customer::listAllCustomers()
{

}

string Customer::getName()
{
    return Customer::customerName;
}


int Customer::getId()
{
    return Customer::customerId;
}

void Customer::deleteCustomer(int id)
{
    if(Customer::customerId == id)
        customerList->erase(id);

        Customer::customerId--;


}

   int Customer::customerId = 0;


I want to use the vector in main.cpp as reference in method listAllCustomers() because I want to list all customers

thank you
Last edited on
To pass a vector by reference as a parameter, you type something like:
void func ( std::vector<your_type> & thevector )
Last edited on
In customers.hpp:
 
static void listAllCustomers(const vector<Customer *> & custlist);


In customers.cpp:
1
2
3
4
void Customer::listAllCustomers(const vector<Customer *> & custlist)
{  for (inti=0;i<custlist.size(); i++)
      custlist[i]->List();
}

Note the use of static on listAllCustomers, since it doesn't refer to a specific customer. I also introducted a List() function which is responsible for listing a single Customer instance.

Edit:
BTW, I don't recommend using a vector of pointers. You have a memory leak because you don't clean up the allocated instances before you exit.

A more elegant approach would be to create a Customers class which could inherit from the vector, keep track of the customer id, and be responsible for printing all customers.

You also have an issue with how you're handling customerId. If you delete CustomerId 5 in the vector, the remaining entries in the vector move down to occupy the space taken by the deleted entry. CusomterId 6 is now in slot 5 in the vector, etc.




Last edited on
thank you

I'm getting this error

C:\Users\software\Desktop\Banking System\Customers.hpp|20|error: 'std::vector' has not been declared|

void deleteCustomer(int id, std::vector<Customer> &customerList);

implement

1
2
3
4
5
6
7
8
9
void Customer::deleteCustomer(int id, std::vector<Customer> &customerList)
{
    if(Customer::customerId == id)
        customerList->erase(id);

        Customer::customerId--;


}
Make sure you have #include <vector> in customer.hpp.
Also, please see my edit above if you missed it.,
Yes I have included already, This problem is hard to me. anyway I have to spend time learning and solving this problem.

thank you brother :)
Without seeing your revised code, I suspect you have an inconsistency between your header and your implementation.
 
void Customer::deleteCustomer(int id, std::vector<Customer> &customerList)

Your implementation defines the argument as vector<Customer>, but your definition of Customer list is: std::vector<Customer*> customerList;
Unless you are planning on using polymorphism in the customers, don't store pointers.
std::vector<Customer> customerList;
If you do plan to support polymorphism, use an adequate smart pointer.


Also, a customer shouldn't be responsible to handle a list of customers.
1
2
3
4
    void showCustomerInfo(int id);
    void sortCustomerByName(string name);
    void listAllCustomers();
    void deleteCustomer(int id);
shouldn't be member functions
Topic archived. No new replies allowed.