简体   繁体   中英

Cannot define a 3D vector in a struct

When i define my vector in this way

#include<iostream>
#include<vector>
using namespace std;
typedef std::vector<char> Image1D;
typedef std::vector<Image1D> Image2D;
typedef std::vector<Image2D> Image3D;

int main()
{

  Image3D image2(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));
  return 0;
}

every thing works fine but when i define the same vector in a struct if give me some error in terms of type specifier it have problem with this const value 10

#include<iostream>
#include<vector>

using namespace std;
typedef std::vector<char> Image1D;
typedef std::vector<Image1D> Image2D;
typedef std::vector<Image2D> Image3D;




struct CameraImages
{
   Image3D image2(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));
};

any help would be highly appreciated

The initialization of the data member of the structure is invalid.

struct CameraImages
{
   Image3D image2(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));
};

You have to use a brace-or-equal initializer.

From the C++ 17 Standard (12.2 Class members)

member-declarator:
    declarator virt-specifier-seqopt pure-specifieropt
    declarator requires-clause
    declarator brace-or-equal-initializeropt
    identifieropt attribute-specifier-seqopt : constant-expression brace-or-equal-initializeropt

For example

struct CameraImages
{
   Image3D image2 {10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0))};
};

or

struct CameraImages
{
   Image3D image2 = {10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0))};
};

or

struct CameraImages
{
   Image3D image2 = Image3D(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));
};

image2 is a member, you can set up default initialization something like:

struct CameraImages
{
   Image3D image2 = Image3D(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));
};

you could use brace initialization also like

struct CameraImages
{
   Image3D image2 = {(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)))};
};

but the former is probably clearer.

As an aside: this approach will have some significant performance implications for image operations.

struct CameraImages
{
   Image3D image2(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));
};

This is a function declaration.

To prevent a syntax ambiguity such as Most vexing parse , any member declaration like T t(args...) is not allowed.

In short, in order to fix your problem, you have to declare it as either these forms.

Image3D image2{10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0))};
Image3D image2 = Image3D(10, std::vector<std::vector<char>>(10, std::vector<char>(10, 0)));

For more information, you may check Why can in-class initializers only use = or {}?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM