0

I have tried to make an array of a struct I have defined like in Creating an array of structs in C++ but I get an `no matching function to call to Simplex::simplex()' error. What is going wrong? Underneath is (what I think is) the relevant code. The error is in the Simplex simplices[numberOfSimplices] line.

struct Simplex {
public:
    int dimension;
    long* vertices;

    // Constructor definition
    Simplex(int d, long* v) {
        dimension = d;
        vertices = v;
    }
};

void f() {
    // Initializing random seed
    srand(time(NULL));

    long numberOfSimplices = rand() % maxSimplices + 1;

    Simplex simplices[numberOfSimplices];
}
Pel de Pinda
  • 113
  • 4
  • 3
    You need to define a default Constructor in your Simplex class, so a constructor without parameters. ````Simplex(){}```` – A M Jan 17 '21 at 12:43
  • 1
    Or better: use a `std::vector´ instead of an array, then you can add elements that have been created from any non-default constructor. – Werner Henze Jan 17 '21 at 12:50
  • 1
    That variable-length array isn't supported by the C++ standard anyway, so yet another reason to avoid it and use `std::vector` instead. – WhozCraig Jan 17 '21 at 12:56

1 Answers1

1
Simplex simplices[numberOfSimplices];

is going to create numberOfSimplices number of Simplex's. Since you don't convey how to create those objects, the assumption is Simplex objects are default constructable using Simplex(), but there is no default constructor available in Simplex class, the only constructor available is Simplex(int, long*)

Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34