-2

I only know Java, and I am learning how to do c++ right now. I currently have an object called "node". I want to make an array of those elements in a different class, and I have to perform many operations on this array. Because of this, I am trying to declare a global array variable that gets initialized in my constructor. In Java, this would've been done by

ObjectName[] variableName = new ObjectName[size];

but I am not sure how to do it in c++. I've tried declaring it similar to how I declared the other global arrays, with

Node* nodes;

and then in my constructor:

nodes = new Node[size]

but I got a bunch of compiler errors. How am I supposed to do this? This is only my second week of coding in c++, so try to keep answers basic.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Could you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and include the text of the compilation errors? – Brian Bi Feb 13 '22 at 21:20
  • Please learn C++ from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Using `new` at all is probably the wrong approach already. Most likely you should be using `std::vector`. Constructs that look similar to Java often have different meaning in C++. – user17732522 Feb 13 '22 at 21:26
  • Stop using `new` when unnecessary... a simple `std::vector` should be sufficient. – Jepessen Feb 13 '22 at 21:28
  • Folks could explain what to do, but they would be doing you a disservice if they did. Find decent introductory material on C++, and read it, practice using examples. Don't try to learn C++ by analogy with Java- C++ and Java are VERY different languages, even if they have some syntax in common. Learning C++ by analogy from Java is an effective way of becoming a terrible C++ programmer. (Similarly, learning Java by analogy from C++ is an effective way to become a terrible Java programmer). – Peter Feb 14 '22 at 00:47

2 Answers2

0

In C++ you use vector more often than array. You also distinguish between creating objects on the stack and on the heap (you already mentioned that concept; in C++ you are more actively involved in thinking about that).

You also may want to pay attention which C++ Standard you are using. Some concepts are not available in older standards. I tried to mention some in the example code below.

When dealing with arrays in C/C++ you should understand the notion of pointers, which I believe is the probable cause of your confusion. new creates an object on the heap and returns a pointer. When creating an array, then the returned pointer points to the first element.

Avoid new if you can. In newer C++ standards there are better concepts of smart pointers (e.g. std::unique_ptr<...>); I will not dive into that since you are just beginning. Be patient with learning C++, I am sure you will succeed, it takes time really.

#include <iostream>
#include <array>
#include <vector>

struct Node {
    std::string name = "node";
};


int main() {

    const size_t size = 10;

    // you can create it on the stack
    // will be deleted when leaving the block/scope
    Node nodes1[size];
    nodes1[0].name = "first node1";
    std::cout << nodes1[0].name << std::endl;

    // you can create it on the heap
    // you have to delete the objects yourself then
    Node *nodes2 = new Node[size];
    nodes2[0].name = "first node2";
    std::cout << nodes2[0].name << std::endl;

    // in C++ 11 and later you can use std::array<...>
    // you have to include the header <array> for that
    std::array<Node, size> nodes3;
    nodes3[0].name = "first node3";
    std::cout << nodes3[0].name << std::endl;

    // in C++ you use array "seldom"
    // instead you use the containers quite a lot as far as I have learned
    // e.g. you can include <vector>; can be used like an array
    std::vector<Node> nodes4(size);
    nodes4[0].name = "first node4";
    std::cout << nodes4[0].name << std::endl;

    // you can iterate over a vector like you know it from an array
    for (size_t i = 0; i < nodes4.size(); ++i) {
        if (i == 0) {
            std::cout << nodes4[i].name << std::endl;
        }
    }

    // in C++ you will soon learn about iterators too
    for (auto iter = nodes4.begin(); iter != nodes4.end(); iter++) {
        if (iter == nodes4.begin()) {
            std::cout << iter->name << std::endl;
        }
    }

    return 0;
}
Ely
  • 10,860
  • 4
  • 43
  • 64
0

How do I create an array of objects?

Given a type named ObjectName, you can define an array variable with name variableName and a compile time constant size size like this:

ObjectName variableName[size]{};
eerorika
  • 232,697
  • 12
  • 197
  • 326