0

I would like to give values to a vector/array within a struct. Something like following. However, following code gives errors, and cannot compile.

struct Params
{

    float a = 1.132;
    vector<float> v(100);

    for (int n = 0; n < 100; ++n)
    {
        v[n] = n*a;
    }
};

How do I fill v with value I'd like to fill, and save v in the struct? Thanks.

5
  • How is this different from your previous question? Commented Mar 11, 2019 at 14:00
  • @Someprogrammerdude I was trying to put previous one into a struct, and ran into problem, as this post stated. This time is within a struct. Thank you. Commented Mar 11, 2019 at 14:01
  • 1
    use constructor Commented Mar 11, 2019 at 14:02
  • @Jahid I would think so too. Care to post some sample code perhaps? Thanks. Commented Mar 11, 2019 at 14:03
  • 2
    @NickXTsui You really need to find a C++ book, and read up on what a constructor is. Hard to imagine anything more fundamental to C++. If you've got this far and you don't know what a constructor is you're going about things the wrong way. Commented Mar 11, 2019 at 14:08

2 Answers 2

2

Put the code into a contructor like this:

Params() {
    v.resize(100);
    for (int n = 0; n < 100; ++n)
    {
        v[n] = n * a;
    }
}

This way, when you make a Params, it is already initialized with your values:

Params a;
std::cout << a.v[10] << std::endl;

This will print 11.32, for instance.

Sign up to request clarification or add additional context in comments.

6 Comments

"Put the code into a contructor like this:" Just wondering where is the constructor?
The constructor is the function of the form Params() {}. You can find a detailed explanation here: geeksforgeeks.org/constructors-c
Perfect. Thanks!
what if I want to do a lot of things, then putting a lot of code in the constructor is ugly. Can I define a bunch of functions outside the constructor, then just call those function in the constructor? Where do I put all those functions then? Thanks.
Sure. You can call any other function in your class from your constructor. You put them in the class like any other function, before or after the constructor (typically the constructors come first and then the other functions).
|
1

You can put the code on a constructor (is a good solution). If for some reason, you dont want to declare a Constructor you can do the next workaround:

struct Params {
    float a = 1.132f;
    std::vector<float> vec {
        [this] {
            std::vector<float> v(100);
            for (int n = 0; n < 100; ++n)
            {
                v[n] = n*this->a;
            }
            return v;
        }()
    };
};

Lambda function is called on "Params" instantiation.

Also in my humble opinion is better to use a Constructor for clarity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.