samedi 19 octobre 2019

Operator new undefined

While trying to learn dynamic assignment in C++, every time I use the "new" operator, the code fails to compile.

I have already tried to use malloc and the other functions. They worked for a time, but now I need to dynamically declare an object and I can't seem to get malloc to work with that. (Also, I should be able to use new so...)

#include <stdio.h>
#include <stdlib.h>

int main(){
    int * x = new int[10];
    for(int i = 0; i < 10; i++){
        x[i] = i;
    }
    for(int i = 0; i < 10; i++){
        printf("%d\n", x[i]);
    }
}

When I try to compile with "gcc main.cpp -o main.exe" it always gives me the same error:

[temporary file name]cc8cDkmk.o:main.cpp:(.text+0x13): undefined reference to `operator new[](unsigned long long)'

Aucun commentaire:

Enregistrer un commentaire