-1

I need use a var from pointer reference in another function, so, when I call that I pass the address value, but how can I use this pointer there (not creating another instance)? Look the code segment:

//Main region - using Product class -> _products = vector<Product>
int main()
{
    _products.reserve(2);
    for(i=0;i<2;i++)
        productRefe.SetProduct(&_products);
    return 0;
}


//Method in Product.cpp
void Product::SetProduct(vector<Product> *productsP)
{
    vector<Product> products = *productsP;
    Product productInsert;
    cout << "Type the description: ";
    cin >> productInsert.Struct.description;
        products.push_back(productInsert);
}

  void Product::GetProducts(string description, bool all, vector<Product> *productsP)
  {
int i = 0;
vector<Product> products = *productsP;
if(all)
    for(i = 0; i < products.size(); i++)
    {
        cout << "Description: " << products[i].Struct.description << endl;
        cout << "Value" << products[i].Struct.value << endl << endl;
            }
   }

I believe the problem is in the first line in method SetProduct... But, what can I change to it works? Thanks

1

4 Answers 4

1

The above alternatives are better, but this would work also (local variable is a reference): vector<Product> &products = *productsP;

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is adding a new product to a vector and then destroying the new vector almost immediately.

void Product::SetProduct(vector<Product> *productsP)
{
    vector<Product> products = *productsP; // creates a new vector<Product> and copies the vector pointed to by ProductsP into it
    Product productInsert;
    cout << "Type the description: ";
    cin >> productInsert.Struct.description;
        products.push_back(productInsert); // adds the new product to the new vector<Product>
    // as this function exits, products goes out of scope and is destroyed, so no change is ever saved
}

In addition to the other solutions which can fix this problem, I would suggest making the vector a member of a collection class:

class Products
{
// other members/functions
public:
    void AddProduct(const Product& prod)
    {
        m_Products.push_back(prod);
    }
private:
    std::vector<Product> m_Products;
};

Or, just using the collection itself. What you have posted would lead me to believe you are mixing concerns in your class.

1 Comment

That's another good way, thanks, but I'm using the @Jason solution vector<Product> &products = *productsP;
1

Suggested alternatives:

// Declare a reference in your signature (instead of a pointer)
void Product::SetProduct(vector<Product>& products)
{
    Product productInsert;
    cout << "Type the description: ";
    cin >> productInsert.Struct.description;
        products.push_back(productInsert);
}

... or ...

void Product::SetProduct(vector<Product> *productsP)
{
    Product productInsert;
    cout << "Type the description: ";
    // Declare a pointer ... and simply use the pointer as a pointer
    cin >> productInsert.Struct.description;
        products->push_back(productInsert);
}

In neither case do you need or want "products" - it's extraneous. Just use the variable you passed in.

3 Comments

Yes, sounds good... but I need to get index by index on function GetProducts, and I cant do it with pointer reference (I think)
It should also be noted that: "vector<Product> products = *productsP" copies the contents pointed to by productsP into products, and subsequently productsP never gets updated.
@MGE if you use a reference for GetProducts as well, you can use the subscript operator as usual. I.e. cout << products[i].
0

In the function, don't make a copy, but use *productsP directly, for example:

cin >> productsP->Struct.description;

1 Comment

Sorry... but productsP's a vector of Product

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.