Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:41:50+00:00 2026-06-18T05:41:50+00:00

I have an array of objects that have a vector as a class member

  • 0

I have an array of objects that have a vector as a class member (actually it is a struct). The struct is:

struct Cell {
  vector<int> borderNodes;
  vector<int> helperNodes;

  int minNodePointer;
  int maxNodePointer;


  Cell() {
    minNodePointer = -1;
    maxNodePointer = -1;
  }

  Cell(int _minNodePointer, int _maxNodePointer) {
    minNodePointer = _minNodePointer;
    maxNodePointer = _maxNodePointer;
  }

  vector<int>& getHelperNodes() {
    return helperNodes;
  }

  vector<int>& getBorderNodes()  {
    return borderNodes;
  }

  void setHelperNodesArray() {
    sort(helperNodes.begin(), helperNodes.end());
  }

  void setBorderNodesArray() {
    sort(borderNodes.begin(), borderNodes.end());
  }
}; 

I have built an array of those objects as a global variable with :

Cell* cells = new Cell[maxNumberOfCells];

and I want to add some integers to the vectors inside the objects.

I tried this (inside a function):

cells[cellId].borderNodes.push_back(node_id);

or

cells[cellId].getBorderNodes().push_back(node_id);

and they compile fine, but nothing is added to the vector. How is the correct way to do this? Here is the function that reads from a db, and adds the integers. The query and reading from db is correct, so no mistakes there.

void loadBorderNodesPerCellBnOnly(bool shouldRearrangeNodes, int subtractor, int maxNumberOfCells, int** cellBorderNodes) {
    cellBorderNodes = new int*[maxNumberOfCells];
    try {
        work W(Conn);
        for (int rownum = 0; rownum < r.size(); ++rownum) {
            const result::tuple row = r[rownum];

            vector<string> s = split(row[1].as<string > (), ' ');
            const int mySize = s.size();
            int cellId = row[0].as<int> ();
            cellBorderNodes[cellId] = new int[mySize];
            for (int k = 0; k < mySize; k++) {
                int node_id = atoi(s[k].c_str());
                cellBorderNodes[cellId][k] = node_id;
                (cells[cellId].getBorderNodes()).push_back(node_id);
                try {
                    nodes[node_id - subtractor].cellId = cellId;
                } catch (const std::exception &e) {
                    std::cerr << e.what() << std::endl;
                }
                nodes[node_id - subtractor].isBorderNode = true;
            }
            s.clear();
        }
    } catch (const std::exception &e) {
        std::cerr << e.what() << std::endl;
    }


    for (int k = 0; k < maxNumberOfCells; k++) {
        cout << k << ": " << cells[k].borderNodes.size() << endl;
        cells[k].setBorderNodesArray();
        if (maxBorderNodesPerCell < cells[k].borderNodes.size()) {
            maxBorderNodesPerCell = cells[k].borderNodes.size();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:41:51+00:00Added an answer on June 18, 2026 at 5:41 am
    void loadBorderNodesPerCellBnOnly([...], int** cellBorderNodes) {
        cellBorderNodes = new int*[maxNumberOfCells];
    

    The function above takes an int** by value. The copy is called cellBorderNodes inside the function. In the next line you change the value of the copy and continue updating that. None of those changes will be visible outside of the function. (And you will leak the acquired memory)

    If you want to pass an int** and modify it inside the function, consider passing it by reference as in int**&. But you might be better off using higher level constructs (pass a vector<vector<int>>&, create a type that represents the data and pass it by value…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector-like class that contains an array of objects of type T
I have one 'main' thread that creates an array of objects of class SlowData
I have a class symbol_table that has a vector of objects of another class
I have a class that creates a vector of objects. In the deconstructor for
Let's say I have a class that includes an array of some struct, and
I have an array of objects that have QTTime structs as attributes. The objects
I have a function that takes an array of objects and I need to
I have a figure below: And my question is that the objects in Array
I have a mutable array that has about 100 objects in it and each
I have a form that generates an array of attribute objects. The attribute objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.