-1

This is the code I'm trying to run:

class poly {
    public:
        int vnum;
        vrtx vrts[this->vnum];
};

(Note: The class name "poly" and other class "vrtx" are named as such to approximate the purpose of the problematic snippet. Vrtx is a class with int x, y, z;)

At first, the code didn't contain the "this->" pointer at all. I was confused why it wasn't working, and then realized that "vnum" doesn't mean anything. I needed an object.poly.vnum sort of thing so that I'm referencing a specific value. I tried "this.," "this.poly.," and the displayed "this->," but none of them work. I'm not great with pointers, so any advice would be appreciated!

I've looked at similar questions, but none of them address this issue in such a way that I could make the necessary fix with the information provided.

  • Are you trying to create an array with `vnum` elements? When is `vnum` set, and where? You can only create the array after `vnum` is initialized. – egrunin Feb 01 '23 at 22:31
  • As written, you're trying to declare the size of vrts as being set at runtime by the value of vnum. vnum is only valid here if it is a constexpr known at compile time. – Dave S Feb 01 '23 at 22:32
  • 1
    Classes (and non-dynamically allocated arrays) have a fixed memory footprint (not counting dynamically allocated memory they manage). `vrtx vrts[something];` requires `something` to be a compile time constant. Consider using std::vector instead which manages dynamic memory to provide a run time sized and resizable array like thing. – Avi Berger Feb 01 '23 at 22:33
  • If you want to set a fixed size at object construction time, you could use `vrtx*` in the class declaration, pass a value for vnum in the constructor, and have the constructor allocate the elements for the array. A vector is easier. – Dave S Feb 01 '23 at 22:35
  • [This might be close to what you are trying to achieve](https://godbolt.org/z/anrv9556v) though it doesn't answer your title question. – Avi Berger Feb 01 '23 at 22:48
  • This isn't valid because the compiler has to decide the memory layout of the class when it compiles the program. It needs to know how many vrts there are. – user253751 Feb 01 '23 at 23:10
  • Use `std::vector vrts;` and initialize inside the constructor, without using `this->` syntax. – Thomas Matthews Feb 02 '23 at 01:23

1 Answers1

1

Here's a code fragment that should help.

class Poly
{
  public:
    int vnum;
    std::vector<vrtx> vrts;
    Poly(int capacity)
        : vnum(capacity)
        { vrts.resize(vnum);}
};

The above fragment uses std::vector since the std::vector can expand dynamically (at run-time). The constructor uses the resize method to expand the std::vector to the given capacity.

Arrays are a pain to resize during run-time, so use std::vector.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • This answer works really well, but the things I'm doing require functions available only to arrays. At this point, I've given up and removed multi-dimensional functionality, so it's just 3D now. Edit: Removed Typo – Angus McMillan Apr 09 '23 at 01:13
  • Just out of curiosity, what functions are available to arrays but not to vectors? – Thomas Matthews Apr 09 '23 at 06:13