Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Constructors in a vector?

This topic is 4605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive made a neuron class, the constructor takes an int as an argument which says how many inputs it should be prepared to take. Neuron(int TotalInputs); // ctor Now my "agent" is a little ball which has four engines, one for every direction. The goal for is to stay in the center of the screen. Neuron Engine[4]; // one engine for every direction Now, I have four neurons, but how do I give the neurons the required argument for the constructor now? If I do it this way, the compiler doesnt complain. Have I tricked the class so it wont be getting any ctor argument? :P

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure how you do that when the objects are allocated on the stack, but one way you can do it is by changing your array to be a Neuron* array and then iterating through it, calling "new Neuron(...)" every time.

Of course if you do it that way, be sure to delete it in your destructor.

Share this post


Link to post
Share on other sites
The simplest way would be like this:
(I'm assuming you're using c++, correct me if I'm wrong.)


Neuron Engine[4] = { 1, 2, 3, 4 };


Where "1, 2, 3, 4" are the values you want to send to the constructors.

Share this post


Link to post
Share on other sites
Using {1, 2, 3, 4} creates an array of integers. That statement reads "assign an object of type integer array to type Neuron array" which makes no sense to the compiler.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tac-Tics
Using {1, 2, 3, 4} creates an array of integers. That statement reads "assign an object of type integer array to type Neuron array" which makes no sense to the compiler.

Not if the constructor for Neuron is non-explicit it doesn't.

@ Mizipzor : I'd use Rayno's advice, or if you'd rather not have the conversions,

class Neuron {
public:
explicit Neuron( int TotalInputs ) : i(TotalInputs) {}
private:
int i;
};

int main() {

Neuron Engine[4] = { Neuron(4),Neuron(3),Neuron(2),Neuron(1) };
...
}


EDIT: BTW, since you're using C++, you may consider replacing your array with a std::vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rayno
The simplest way would be like this:
(I'm assuming you're using c++, correct me if I'm wrong.)


Neuron Engine[4] = { 1, 2, 3, 4 };


Where "1, 2, 3, 4" are the values you want to send to the constructors.


But I cant put that in the header of the class (the engine array is a private), where should I put it?

Quote:
Original post by stylin
EDIT: BTW, since you're using C++, you may consider replacing your array with a std::vector.


I prefer vectors cause their size are dynamic, but now I know Im going to have one engine in every direction (four directions) every time, so I may as well go with an normal array.

Good suggestions though, a bit more advanced but Ill go with it if I cant get Raynos to work. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Mizipzor
Where "1, 2, 3, 4" are the values you want to send to the constructors.


But I cant put that in the header of the class (the engine array is a private), where should I put it?[/quote]

Is it supposed to be a static member of class Neuron?

Is it supposed to be accessible from the rest of the program, or just neuron.cpp?

Have you read this?

Share this post


Link to post
Share on other sites
Arrays and array elements are difficult to initialize via constructors. In the constructor for the class containing the Engine variable, you might do something like this:
    Engine[0] = Neuron( Neuron_0_initial_value );
Engine[1] = Neuron( Neuron_1_initial_value );
Engine[2] = Neuron( Neuron_2_initial_value );
Engine[3] = Neuron( Neuron_3_initial_value );
BTW, the uninitialized array code compiles because you have written a default constructor for Neuron ("Neuron::Neuron()") or you don't have any constructor for Neuron so the compiler creates a default constructor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!