#cant add to class vars

107 messages · Page 1 of 1 (latest)

merry atlas
glass coveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

humble trellis
#
LineClass CreateLineInstance(NodeClass N1, float Weight, NodeClass N2)
{
    LineClass LineInst;
    LineInst.N1 = N1;
    LineInst.Weight = Weight;
    LineInst.N2 = N2;

    return LineInst;
}

this copies new instances of the passed nodes into the line instance

#

even the parameters that you receive within the function are copies of the arguments that you call the function with, because you're taking those parameters by value

#

this is roughly what happens

#

as you can see the nodes N1 and N2 of LineInst, which ends up stored in Brain1, actually have nothing to do with Nodes[0] and Node[1]: they're separate objects

#

You need to be using references instead, both for your function parameters and for the N1 and N2 members of LineClass

#

It's a meh-solution because it comes with some constraints, for example you can't reseat references, so a given LineClass object can never be a Line between any other nodes than the two nodes that it was assigned to in the first place

#

Also, references must be initialized, and this line from above:

LineClass LineInst;

would entirely skip the reference initialization for N1 and N2 so you'll get a compile error

#

You need to be doing this instead:

LineClass LineInst{N1, Weight, N2};

this way, N1 and N2 are immediately initialized from the moment they start existing, and it will compile fine

#

If all of what I just said flew over your head, don't worry: you're getting started, and C++ is rough on beginners

#

read about references and ask questions if you're getting confused

#

hopefully once you learn about references this will make more sense

merry atlas
#

hey thx so much about understanding me firstly starting i have experience with game dev with unity but ever since the thing tha thappened i tried godot and its nice but i wanna learn a real language not gdscript which isnt bad but yk

#

ill try it btw

#

and tell you if it works

#

and it didnt fly over my head or atleast one of it did why do i have to do lineinst{bla bla bla}

why cant i do lineinst.bla bla = bla bla isnt it the same thing

#

i tried it but it didnt work and just showed these errors

#

the error line showed up at "{" for both of them

merry atlas
humble trellis
# merry atlas and it didnt fly over my head or atleast one of it did why do i have to do linei...
LineClass lineInst; // creates a LineClass object without initializing N1 and N2 inside it, which is illegal since N1 and N2 are references and MUST be initialized
lineInst.N1 = N1; // this doesn't assign the reference, because it's already supposed to be assigned at this point. What that _would do_ is that it assigns a new value to the object being referred to.
...

vs.

LineClass lineInst{N1, Weight, N2}; // creates a LineClass object and initializes its members using the provided values. lineInst.N1 will refer to the N1 object that you're passing in, and likewise, lineInst.N2 will refer to the N2 object that you're passing in
humble trellis
# merry atlas i tried it but it didnt work and just showed these errors

This error here is due to the fact that both your LineClass and NodeClass classes have a default constructor. This disables aggregate initialization which is what we're trying to do with LineClass lineInst{N1, Weight, N2}; (also I believe that it's disabled anyway since there's inheritance involved). In order to fix that you also need to provide an additional constructor that takes in the stuff you're passing in, like so:

NodeClass(float value) : // constructor for NodeClass which takes in a float, the : marks the beginning of a *member initialization list*
    Thing(),             // the first item in the member init list must be a call to a constructor of the parent class
    Value(value)         // the next items initialize the members in some way: here, Value is initialized with the value parameter that was passed in
{
    // the body remains empty, since we did all we need to do in the member init list
}

I'll let you figure out what to do with LineClass and how, but if you have questions you can post them here of course cat_up

merry atlas
#

i got 8 errors

#

this is my nodeclass after adding that

#

class NodeClass : public Thing
{

NodeClass(float Value) :

    Thing(),

    Value(Value)

};

#

these are the errors

humble trellis
#

because you're lacking a constructor body after the member init list

#

it's empty but it needs to be there

merry atlas
#

sorry that was this morning i couldnt think i had 30 mins to get ready so i kinda did this in between

#

ok im super sorry i dont understnad like i do but then i write it and i just dont know can you write what the inside of nodeclass should look like like exactly what i should put

#

sorry if im like bothering by asking too myuch

humble trellis
#

it should look like this:

class NodeClass : public Thing { // Declaration of NodeClass, publicly inheriting from Thing
public:
    float Value = 0.f;

    NodeClass() {              // Default constructor
        Name = "Node";         // Simply assign a name
    }

    NodeClass(float value) :   // constructor for NodeClass which takes in a float, the : marks the beginning of a *member initialization list*
        Thing(),               // the first item in the member init list must be a call to a constructor of the parent class
        Value(value)           // the next items initialize the members in some way: here, Value is initialized with the value parameter that was passed in
    {
        Name = "Node";         // Assign the name there too
    }
};
#

it would be easier if you just read up about classes altogether

merry atlas
#

why assign two times

humble trellis
#

you don't assign twice

#

the two assignments you see are in different constructors

#

only one constructor is ever called per object

#

so the assignment of Name is always going to take place exactly once

merry atlas
#

oooooh ok

#

and then im guessing i do the same for line class

humble trellis
#

yes

merry atlas
#

ok lemme see if it works

#

ok so it works like i can now add directly to a nodes value but my proccess brain func still doesnt work any reason why?

here it is:

int ProccessBrain(BrainClass Brain)
{
int x = 0;
while (x < Brain.Lines.size())
{
cout << "Line = " << x << endl << "N1.Value = " << Brain.Lines[x].N1.Value << endl << "N2.Value = " << Brain.Lines[x].N2.Value << endl << "LineWeight = " << Brain.Lines[x].Weight << endl;
Brain.Lines[x].N2.Value = Brain.Lines[x].N1.Value * Brain.Lines[x].Weight;
x += 1;
}

return 0;

}

humble trellis
#

can you show your LineClass class

merry atlas
#

yes

humble trellis
#

mandatory word of advice: naming classes SomethingClass is redundant, just name them Something

merry atlas
#

here:

class LineClass : public Thing
{
public:
NodeClass N1;
float Weight;
NodeClass N2;

    LineClass()
    {
        Name = "Line";
    }

    LineClass(NodeClass N1, float Weight, NodeClass N2) :

        Thing(),

        N1(N1),

        Weight(Weight),

        N2(N2)

        {

        }

};

humble trellis
#

there's still no references in there

merry atlas
#

i thought it was like the same as the node class

humble trellis
#

and then read about references

#

and classes while you're at it

merry atlas
#

i feel so dumb

#

ok

#

i will

humble trellis
humble trellis
merry atlas
#

i did this:

LineClass LineInst{N1, Weight, N2};

#

hello?

#

ok it wouldnt let me send messages

#

im tryna send my code

humble trellis
#

yes, but again: the N1 that you're passing in this line is not the N1 that is received by the LineClass constructor, it is a copy that gets transferred over, and it is yet another copy that finally gets assigned to LineInst.N1 within the constructor

#

the reason why is because you're not using references

#

you're using what's called value semantics, which performs copies of the data you're passing around, which is why the N2 at the end of one Line is not the N1 at the beginning of the next Line in your Brain thing

merry atlas
#

ok im going to go do something because i have to and when i get back i will read about classes refrences and constructors then text you again maybe

humble trellis
#

sure cat_up

merry atlas
#

ok bye thx for the help

merry atlas
#

so i tried learning about classes but it didnt fix my solution as my problem being kinda specific so i tried doing what you said and used pass by refrences and this is my code but it still outputs the same value:

humble trellis
merry atlas
#

i did this with my code and theres no errors until i compile heres my code and errors:

humble trellis
#

This is one of the dangers I mentioned about using references as class members: it makes the class non-copiable by default, because since references can't be reseated, the compiler silently deletes the default copy constructor and default copy assignment operator.
The errors you're seeing are about you making an attempt to use those deleted things: the assign function which you use to fill your Brain1 vectors actually attempts to make copies of the stuff that you're passing in.

Fortunately there is a simple fix: instead of having NodeClass& N1 and NodeClass& N2 in LineClass, you can make them be std::reference_wrapper<NodeClass> N1 and std::reference_wrapper<NodeClass> N2.
std::reference_wrapper is a "fake reference", so to say: it emulates regular C++ references, but allows you to reassign it as you like. By making N1 and N2 not of type NodeClass& but std::reference_wrapper<NodeClass>, you keep reference semantics, while avoiding the non-reassignable constraint that comes with references.

In short: you need to be using references (because if you don't, your Line instances won't use the same Node instances that are stored in your Brain), but since regular references are too restrictive for your use case, we instead use a utility type that emulates convenient references.

Once you do that, you will need to adapt usage in ProcessBrain: everywhere where you do N1.Value, you need to do N1.get().Value instead (and likewise for N2).

#

and finally:

  • don't do float& Value in NodeClass, just keep it a normal float Value or you'll get unneeded trouble
  • float& RefValue in NodeClass constructor is pointless, just keep it a normal float Value too
  • same thing for const float& RefWeight, it's pointless to have it as a reference, just keep it a normal float
  • NodeClass RefN2 in the LineClass constructor needs to be NodeClass& RefN2
#

once you do the above, it should be working

#

And please... rename NodeClass to Node and do the same for both LineClass and BrainClass 🥲

merry atlas
#

what do i #include for line wrapper

humble trellis
#

you need to #include <functional>

merry atlas
#

dude the amount of thank you i have to give up for you is insane thank you so much as a beginner this means alot in my journey you have taught me more than the series i watched about refrences somehow

#

it works i can set the value add to it and it gives me the right thing after althought code never works the first try so just expept to see me in #cpp-help-text

glass coveBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

humble trellis
#

Sure, no worries cat_up

humble trellis
merry atlas
#

i am and im doing stuff like that rn

humble trellis
#

videos seem like a fine way to learn but there's too many things generally wrong about them, their content and their authors for it to be a decent learning medium

merry atlas
#

alright, where did that come from

humble trellis
#

the info can become out of date when it's not just plain wrong to begin with, the format tends to favor short introductions to subjects that would need hours to cover properly and that can in turn make you think about something the wrong way, etc

#

also you can't copy paste code from videos

merry atlas
#

i dont

humble trellis
#

of course you don't, you can't yamikek

#

just saying that it's handy to read an example, copy it in your IDE, and then mess around with to see how features interact

merry atlas
#

well im just saying if it was an optiojnim not doing it i wouldnt learn anything especially as a beginner i dont wanna do that i wanna take as many learning oppurtunitys as i can

humble trellis
#

yeah I'm not arguing in favor of beginners just blindly copy-pasting code in their IDE while having no idea about how it does what

merry atlas
#

ok but anyway thx for the help

humble trellis
#

that was the mandatory rant about videos that don't age too well, sorry about that 😛

merry atlas
#

i think my next project is gonna be a triple a game thats next gen - "said every confident person"

humble trellis
#

good luck with the next stuff, don't hesitate to ask questions again

merry atlas
#

ok

humble trellis
#

and also

glass coveBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
merry atlas
#
#include <iostream>
int main()
{
 cout << "Hello, World"
}
#

oh

#

dang

#

i forgot

#

back ticks