0

I created a new Xcode project by selecting "external build system" and set the build tool to /usr/bin/g++ (which is what I used when compiling from the terminal). Now I am trying to initialize a 2d vector of doubles using an initializer list, but get the error:

Non-aggregate type 'vector<vector<double>>' cannot be initialized with an initializer list

This is the code I am trying to compile:

vector< vector < double > > dct_mat =
{
    {0.3536,  0.3536,  0.3536,  0.3536,  0.3536,  0.3536,  0.3536,  0.3536},
    {0.4904,  0.4157,  0.2778,  0.0975, -0.0975, -0.2778, -0.4157, -0.4904},
    {0.4619,  0.1913, -0.1913, -0.4619, -0.4619, -0.1913,  0.1913 , 0.4619},
    {0.4157, -0.0975, -0.4904, -0.2778,  0.2778,  0.4904,  0.0975, -0.4157},
    {0.3536, -0.3536, -0.3536,  0.3536,  0.3536, -0.3536, -0.3536,  0.3536},
    {0.2778, -0.4904,  0.0975,  0.4157, -0.4157, -0.0975,  0.4904, -0.2778},
    {0.1913, -0.4619,  0.4619, -0.1913, -0.1913,  0.4619, -0.4619,  0.1913},
    {0.0975, -0.2778,  0.4157, -0.4904,  0.4904, -0.4157,  0.2778 ,-0.0975}
};

How can I enable initializer lists? I don't care if I need to change my g++ for something else. I just don't know what compiler should I use. If you know which compiler will work for me, please also state how to tell Xcode to use it.

Alex Terreaux
  • 1,881
  • 5
  • 23
  • 39
  • Which g++ version are you using? Try `g++ --version` – Brian Bi Sep 05 '14 at 23:02
  • Did you try adding `-std=c++11` compiler flag? – πάντα ῥεῖ Sep 05 '14 at 23:03
  • This is the output of 'g++ --version' Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.3.0 Thread model: posix – Alex Terreaux Sep 05 '14 at 23:03
  • @AlexTerreaux Ouch, GCC 4.2.1 is stone age! You should have 4.7 at least, or merely 4.9. Consider updating your machine! – πάντα ῥεῖ Sep 05 '14 at 23:04
  • 1
    Related: http://stackoverflow.com/questions/4574246/can-i-use-c11-with-xcode. @πάνταῥεῖ: This is clang masquerading as g++ on Mac OS. The compiler version is fine. – T.C. Sep 05 '14 at 23:06
  • @πάνταῥεῖ adding -std=c++11 fixed the compiling error. However, I get a segfault when running it. If I comment out the initializer list, it works good. – Alex Terreaux Sep 05 '14 at 23:09
  • 1
    @AlexTerreaux _"However, I get a segfault when running it."_ Sounds like this is an orthogonal problem, – πάντα ῥεῖ Sep 05 '14 at 23:10
  • @πάνταῥεῖ You mean unrelated? I would have thought that too, but I am not even referencing the vector anywhere in the code. Just having that initialization makes it crash. – Alex Terreaux Sep 05 '14 at 23:13
  • @AlexTerreaux The vector isn't the root of your crash. I just pasted the listed vector of vector definition a clean `main()` on my Mac (Xcode 5.1.1, same toolchain as yours). No errors. Constructs the vector of vectors fine. Your crash is somewhere in your code; not this def. – WhozCraig Sep 05 '14 at 23:34
  • @πάνταῥεῖ Yes, I was declaring it as a global variable. Doing so inside a method fixed. Why do you think this is? – Alex Terreaux Sep 06 '14 at 04:13
  • Will this be fixed if I declare it in a .h file? :( – Alex Terreaux Sep 06 '14 at 04:14

0 Answers0