Accessing member functions

I have an if statement inside of a loop that creates dynamic objects. After the if statement finishes, I need to get back to these objects and access their private variables, but I am having trouble with scope. Here is the relevant part of my code (some pseudocode for simplicity):

1
2
3
4
5
6
7
class Customer
{
  Customer(int x)
  {
     my_variable = x; 
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
...
   for (int i = 0; i < some_number; i++)
   {
      if (some condition)
      {
         Customer* c;
         c = new Customer(int x);
      }
      ...
   }


Later on in the for loop (after many iterations), I need to be able to access these objects and access their private member variables. The new operator may have been called hundreds of times, so there would be potentially hundreds of objects out there.

How would I reference a specific object created like this? I suspect that I'm not doing this correctly, so if there is a better way to create these objects, please suggest.

Thanks!
You could create an array of pointers and assign a Customer to each one.

EDIT: Or if you don't want do deal with memory, pointers, and new, you could probably just make a vector of Customers.
Last edited on
Make an array, or a vector, and store the pointers you new. Otherwise you'll be creating memory leaks all over.
Thanks. I created a vector of customers. After the many structural redesigns that this forced, now I don't know how to use the vector that I set up in a function. In int main(), I have vector<int> customer;. I need to access this vector in another function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool is_switching(unsigned int this_second, unsigned int this_customer)
{
    int n;
    int::iterator it;
    
    for (it = customer.begin(); it != customer.end(); it++)
    {
        if (this_customer == customer(it))
            n = it;
    }

    //determine how long the customer has been waiting
    int time_waiting = this_second - this_customer;

    if ( time_waiting > switch_tolerance(n) ) //customer has exceeded their tolerance
        return true;

    return false;
}


This causes three errors:

expected initializer before it
'it' was not declared in this scope
'customer' was not declared in this scope

I've seriously had it with C++. It was put on this earth to make my life hell. Can somebody PLEASE help me fix this mess.

Thanks.
First, you need to pass vector<int> customer into your function:

bool is_switching(unsigned int this_second, unsigned int this_customer, const vector<int>& customer)

In the above, the vector is passed in as a reference to avoid copy constructing and it has a const modifier because your function doesn't modify it.

Next, you're iterating over a vector<int>, so int::iterator should be vector<int>::iterator. You can't iterate over an int anyway.

I've seriously had it with C++. It was put on this earth to make my life hell.

I'm sure with some practice you'll be bringing it to its knees.
Topic archived. No new replies allowed.