0

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;

}
5
  • 5
    C or C++? They are different languages. Don't tag both. Compiler? Commented Nov 9, 2016 at 16:15
  • What's paperFold? Anyway, array code seems fine, what compiler are you using? Commented Nov 9, 2016 at 16:18
  • Are you absolutely sure that you're compiling that code and not something else in a file with the same name, and that you haven't forgotten to save? I've lost count of how many times I've made those mistakes. Commented Nov 9, 2016 at 16:24
  • C++, and I'm compiling with g++. Commented Nov 9, 2016 at 16:26
  • Just a class - solving some problems on TopCoder to knock off some C++ rust. You can find more here : arena.topcoder.com/#/u/practiceCode/1285/1721/1846/1/1285 Commented Nov 9, 2016 at 16:30

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now sure how, but it appears it was being caused by eclipse (or more likely, my own dumb error because I'm unfamiliar with the IDE). Deleted the project, recreated/pasted in exact code, and compiled fine.
1

The error message complains that you are trying to initializing a scalar object paper with multiple initializers. i.e. something like this:

double paper = {8.0, 11.0};

But obviously paper is an aggregate type in the code you posted which shouldn't produce that error. So, it means you haven't posted the code that produced the error or simply failed to save the source file?

1 Comment

Swapped over to using an IDE (eclipse) as I'm running through a large amount of practice problems to knock off some rust - appears that is causing the error. Successfully compiled at the command line, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.