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];
}
3
  • 3
    You need to define a default Constructor in your Simplex class, so a constructor without parameters. Simplex(){} Commented Jan 17, 2021 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. Commented Jan 17, 2021 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<Simplex> instead. Commented Jan 17, 2021 at 12:56

1 Answer 1

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*)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.