0

I am trying to handle C++ through examples of Numerical Analysis (root finding using different methods)

For this purpose, I would like to loop over an array of initial values and try several methods for each value. I found one way to loop on another topic, but it doesn't totally solve my problem :

int main(int argc, char **argv) {
    double init[] = {-2, 3.8, 20, -40};
    int N = 5;
    for(const double &x : init)
        cout << "value of x" << FindRootNewton(x,N) << endl;
        cout << "value of x" << FindRootSecant(x,N) << endl;
    return 0;
}

I have 2 problems with this code :

  1. I have a warning on the line starting the loop (for ...) : range-based for loop is a C++11 extension [-Wc++11-extensions] What does it mean ? I think I have a C++98 compiler, I tried to figure out using __cplusplus macro and the output is 199711L.

  2. He can't read the second line of the loop, "use of undeclared identifier x", as if the variable vanished after first use.. But if I comment this second line out, all works fine.

Thanks for any help on this

  • 4
    your loop is missing braces `{}` that scope its interior (C++ is not Python) – AndyG Feb 01 '18 at 14:48
  • The second `cout` line is not in the loop, despite your misleading indentation. C++ is not Python. Fir 1., try `-std=C++11`. – Baum mit Augen Feb 01 '18 at 14:48
  • How come you all assume the OP comes from Python? There are many languages where indentation is significant. Googling it, it seems Haskell, Occam and YAML fall into this category. – Cheers and hth. - Alf Feb 01 '18 at 14:52
  • Thanks, 2nd problem solved. For the -std=C++11, I saw this in another post saying "Try compiling with -std=c++11 as a command line flag", but I don't know what it means. – SiboTor Feb 01 '18 at 14:58
  • @SiboTor: If you were compiling using clang or gcc it would make sense :-) If you're using a graphical ide to compile like visual studio, then you need to change the tool settings to use C++11. In visual studio, it means using the compiler from MSVC 12 or later (I think 11 had some minor C++11 support) – AndyG Feb 01 '18 at 15:06
  • @AndyG: I am compiling using clang (with CodeLite), but not since enough time to know what the "command line flag" is :( – SiboTor Feb 01 '18 at 15:17
  • @SiboTor: If you're using a C++98 compiler, probably it won't work. Check which version of clang you're using. Somewhere, when you compile, it is calling clang. You should be able to add `-std=c++11` to that call. – AndyG Feb 01 '18 at 15:20

0 Answers0