Hi there.

I'm currently attempting to create a vector of structs whereby the struct itself contains a vector. The problem I am having is upon creation of the struct itself, throwing the following error.

error:C2075: 'Target of operator new()' : array initialization needs curly braces.

struct DISPLAY_DEVICE_CAPABILITIES{
	vector<int[3]> resolutions;
	int brightness;
};

vector<DISPLAY_DEVICE_CAPABILITIES> displays;

DISPLAY_DEVICE_CAPABILITIES newDisplay;
displays.insert(displays.begin(), newDisplay);

I have tried using push_back instead to no avail; the error remains the same. Does anyone have any ideas of how to alleviate this problem? Sadly it's not an easily googleable issue...vector of structs containing a vector, heh.

Any help would be greatly appreciated... thanks for your time :)


edit:argh, typo in the thread title...how annoying :(

Dani AI

Generated

Using a built‑in C array as the element type of an STL container is the real problem here. Standard containers expect element types with value semantics (copy/assign/move). Raw arrays lack those semantics, so a declaration that tries to make the inner element an int[3] will provoke confusing compiler messages. The practical solution is to replace the C array with a small value type.

Some clean alternatives (modern C++ and readable):

#include <vector>
#include <array>

struct DisplayDeviceCapabilities {
    std::vector<std::array<int,3>> resolutions;   // fixed-size triple per resolution
    int brightness = 0;
};

or give the triple a named type:

struct Resolution { int width, height, refresh; };
std::vector<Resolution> resolutions;

If the inner list needs to be variable-length, use a container-of-containers (for example a vector of vectors or another sequence type). For compact fixed-size tuples, std::tuple<int,int,int> is another option.

When adding elements to the outer vector, prefer in-place construction on modern compilers to avoid extra copies:

std::vector<DisplayDeviceCapabilities> displays;
displays.emplace_back();                      // construct in-place (C++11+)
displays.back().resolutions.push_back({1024,768,60});

Notes and troubleshooting tips:

  • std::array requires C++11; for pre‑C++11 use a tiny struct or boost::array.
  • If the compiler error looks cryptic (MSVC messages often are), focus on whether the template argument is an array type.
  • Reserve capacity (reserve) on outer/inner vectors if many insertions are expected to reduce reallocations.
    As observed, the issue disappears once the inner element is not a raw array; confirmed that approach fixed the build.

Recommended Answers

All 4 Replies

did you try:

typedef struct DISPLAY_DEVICE_CAPABILITIES{
	vector<int[3]> resolutions;
	int brightness;
}DISPLAY_DEVICE_CAPABILITIES;

did you try:

typedef struct DISPLAY_DEVICE_CAPABILITIES{
	vector<int[3]> resolutions;
	int brightness;
}DISPLAY_DEVICE_CAPABILITIES;

It comes up with:

error C2923: 'std::vector' : DISPLAY_DEVICE_CAPABILITIES' is not a valid template type argument for parameter '_Ty' x7 unfortunately :(

It doesn't like the vector declaration in the header file.

he doesn't like:

vector<int[3]> resolutions;

should be:

vector<int> resolutions;

if you need an array:

vector<list<int>> resolutions;
commented: Problem solved, thanks. +1

if you need an array:

vector<list<int>> resolutions;

Thanks mate, it worked. Will work out the intricacies(sp?) from this. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.