You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

compilation error in cpp vector

int main()

{

vector<vector<int>> arr

{

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};


/* output */

for (int i = 0; i < arr.size(); i++)

{

for (int j = 0; j < arr[0].size(); j++)

{

cout << arr[i][j] << " ";

}

cout << "\n";

}

}



ERROR:

error: non-aggregate type 'vector<vector<int> >' cannot be initialized with an initializer list

vector<vector<int>> arr {


clang++ --version:

Apple clang version 13.1.6 (clang-1316.0.21.2)

Target: arm64-apple-darwin21.4.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Posted on Apr 7, 2022 8:39 AM

Reply
13 replies

Apr 7, 2022 9:02 AM in response to prayash16

The following code compiles and outputs the array:


// use the working draft of ISO C++ 2023 DIS
// clang++ -Oz -o vecx vec.C -std=c++2b

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
    vector<vector<int>> arr 
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    /* output */
    for (int i = 0; i < arr.size(); i++)
    {
        for (int j = 0; j < arr[0].size(); j++)
        {
            cout << arr[i][j] << " ";
        }
        // cout << "\n";
        cout << endl;
    }
}


freya: ~/Code/Cxx % vecx

freya: ~/Code/Cxx % vecx
1 2 3 
4 5 6 
7 8 9


Apr 7, 2022 11:16 AM in response to etresoft

Yes, I could have systematically chosen incremental c++ standards and worked my way forward until it compiled and ran correctly, but I chose c++2b, and compiled once, presuming the important parts of c++11 would be grandfathered into it. As it stands c++98/c++03 both have a hissy fit with the code.


I would not be this cavalier assumptive if I were writing production code.

Apr 7, 2022 12:39 PM in response to prayash16

prayash16 wrote:

how can I set it up??


In the Xcode source code editor in a C++ module, compare the C++ code that you posted with what VikingOSX then posted—there are three lines' difference—and make those changes in your C++ code.


The C++ code should then compile and run.


The updated code—with the added two header inclusions and a namespace declaration—compiles and runs here in a C++ session, using Xcode 13.3 on macOS Monterey.

Apr 7, 2022 1:27 PM in response to VikingOSX

Let's say your C++ example is in a file named vec2.C, and my working changes (shown above) are in the file vec.C — that you copy/paste/save into an editor. You can now simply make changes to your original vec2.C in the Terminal:


diff -u vec2.C vec.C > original.patch
patch vec2.C original.patch
diff vec2.C vec.C


Then, you can issue the commented compilation line at the top of my example code, using your own original filename, and the c++11 standard.

Apr 7, 2022 2:05 PM in response to VikingOSX

I was just intrigued by that vector declaration. It's modern coding in a nutshell. I can't tell whether it is just a simple syntax error or if the OP actually meant to do that. In either case, I'm surprised it compiles, but it does. I thought it was another one of those funky new C++ constructs because you used c++2b (which I didn't even know about), but even c++11 was happy with it.


I think this is a legitimate concern. Suppose I'm trying to write code to do A, but I'm not sure of the syntax and I type it in wrong. Unbeknownst to me, my syntax is actually correct, but for something completely different.


I want to retire.

Apr 7, 2022 2:34 PM in response to etresoft

etresoft wrote:
I want to retire.


The following C "hello world" example was circulating recently, and it works (🤯) with gcc and clang:


#include <stdio.h>
void print(char* str, char s[static printf("%s", str)]) { }
int main() {
    print("hello", "");
    print(" ", "");
    print("world", "");
}


Another C compiler I work with tossed a snit over that static declaration usage, so there's that.


There's a fair chunk of C and C++ code that compiles and that even works, but that is dependent on either obscure features—and static array declarations are that, IMO—or on undefined behavior.


Or both.


If you're working in this area, Godbolt is useful for exploring where code does and does not work.


As any of us continue to learn more about C and C++, there will be more of these (paraphrasing) "that works?" cases you've referenced, too.


Apr 7, 2022 3:16 PM in response to MrHoffman

MrHoffman wrote:

The following C "hello world" example was circulating recently, and it works (🤯) with gcc and clang:

People are always coming up with crazy things like that. I would never write something like that.


In this case, I saw that array initializer and though of Swift's trailing closure. That's something I might do. Is it a trailing closure or an initializer without = or parens?


Swift and C++ are in a race now to see who can add more new, obscure syntax features. All the while, I'm taking heavily templated code from Boost geometry and shoving in back into object-oriented Objective-C wrappers.

Apr 8, 2022 7:46 AM in response to etresoft

I thought it interesting that C++98 found an error with arr and subsequent standards did not… ***? The following will throw an error from the -std= usage and list the available C++ standards for you. It is a more complete list than provided on the clang man page.


clang++ -std= vec.C


I just want to retire:


I did, and I can tell you that sleeping in until you are done is welcome after all those decades of 5 am alarm clocks to ensure I got to the airport for that redeye to a variable destination…

Apr 8, 2022 8:01 AM in response to VikingOSX

VikingOSX wrote:

I thought it interesting that C++98 found an error with arr and subsequent standards did not… ***?

Back then, you couldn't have two ">>" characters next to each other. You either had to separate them with a space or use a typedef for the inner template. Any non-trivial example would likely be using a typedef anyway, so it wasn't a big deal.


Next up is the C-style initializer. Again, it's pretty pointless. Why go through the trouble to use a C++ container and then initialize it with a static expression? That's an example of the divide that exists between examples that people write for the internet and real-world apps. If I'm going to bother to put C++ in an app, and use a vector container, it better have at least 100,000 elements in it or I'm wasting my time. Yet much of the language development these days is driven by social media influencers whose primary goal is clever, succinct code they can post in a blog or tweet. This stuff just makes life more difficult for people trying to make a living writing real software.

The following will throw an error from the -std= usage and list the available C++ standards for you. It is a more complete list than provided on the clang man page.

clang++ -std= vec.C

That's a clever trick. Thanks!

I just want to retire:

I did, and I can tell you that sleeping in until you are done is welcome after all those decades of 5 am alarm clocks to ensure I got to the airport for that redeye to a variable destination…

I did too. Twice. I was more likely to get up a 5 am to drive to some god-awful suburban business park. It still hasn't been settled if COVID is going to kill that practice or not. Somebody like Apple could setup mini offices in all over world and still be cheaper than their terrestrial space stations. Yet they are still clinging to that drive-to-god-awful-suburban-business-park mentality.

compilation error in cpp vector

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.