mongolol mongolol - 29 days ago 21
C++ Question

Basic Array Initialization Returning Error

The starred lines below are throwing back the following error:

error: scalar object 'paper' requires one element in initializer


Any idea what might be causing this? This seems like a relatively straightforward initialization.

int main(){

double paper [] = {8.0, 11.0};//***
double box [] = {6.0, 10.0};//***

paperFold pf;
cout << pf.numFolds(paper,box) << endl;
return 0;

}

Answer

This line :

double paper [] = {8.0, 11.0};

is perfectly correct either in C or in C++, for any versions I can remember.

But compilers can be disturbed by other errors in the stream.

This code is fine in C++:

#include <iostream>

int main() {
    double paper [] = { 8.0, 11.0 }; //***

    std::cout << paper[0] << " - " << paper[1] << std::endl;

    return 0;
}

As is this one in C:

#include <stdio.h>

int main() {
    double paper [] = { 8.0, 11.0 }; //***

    printf("%f - %f\n", paper[0], paper[1]);

    return 0;
}