Jump to content

C++ modify vector of objects

Guest
Go to solution Solved by Sauron,

You need to reference the vector's object with a pointer, otherwise you're just modifying a local copy.

for (auto &i : vec) {
...
}

 

Dear LTT users.

Let's cut to the chase. I have 2 classes: Object and VectorOfObjects.

Object class stores:

-name

-variable

VectorOfObject stores:

- vector <Object> vec;

Only vectorofObjects is visible from main, and I added a few methods to add and display objects.

The problem starts when I try to modify an object. I call myVector.modify_variable_of("John", 21) -> where first value is used for indexing and second value is supposed to change, but doesn't.

So the program should find element with name == "John" and modify variable from 10 to 21.

Any help is appreciated because my brain does not compute.

main:

#include <iostream>
#include "VectorOfObjects.h"
int main()
{
    std::cout << "Why doesn't this work?\n";
    VectorOfObjects myVector;
    myVector.add_object(10, "John");
    myVector.add_object(2007, "Chell");
    myVector.display_objects();

    myVector.modify_variable_of("John", 21);
    std::cout << "-----------------------------------------\n";
    myVector.display_objects();
}

Object.h

#pragma once
#include <iostream>
class Object
{
private:
	friend class VectorOfObjects;
	int variable;
	std::string name;
public:
	Object(int variable, std::string name) :variable{ variable }, name{ name } {
		std::cout << "Constructor of Object class "<<this->name<<"\n";
	}
};

VectorOfObjects.h

#pragma once
#include <vector>
#include <iostream>
#include "Object.h"
class VectorOfObjects
{
private:
	std::vector <Object> vec;
public:
	VectorOfObjects() {
		std::cout << "Created empty vector of objects\n";
	}
	void add_object(int variable,std::string name) {
		Object* new_object = new Object(variable,name);
		vec.push_back(*new_object);
		delete new_object;
	}
	void display_objects() const {
		for (auto i : vec) {
			std::cout <<i.name<<": "<<i.variable << std::endl;
		}
	}
	void modify_variable_of(std::string name,int variable) {
		for (auto i : vec) {
			if (i.name == name) {
				std::cout << "\nobject found and variable should be modified: \n";
				i.variable = variable;
			}
			else
				continue;
		}
	}
};

And the output:image.png.3d5f8a8c085cbe99b3c436d5770fdcd1.png

Link to comment
Share on other sites

Link to post
Share on other sites

You need to reference the vector's object with a pointer, otherwise you're just modifying a local copy.

for (auto &i : vec) {
...
}

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

It's alive! Thank you very much 💪🧠😃

image.png.9d03e360a24098230df408e548a7e370.png

I love C++ but man... 😂

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×