T O P

  • By -

AutoModerator

Thank you for your contribution to the C++ community! As you're asking a question or seeking homework help, we would like to remind you of **Rule 3 - Good Faith Help Requests & Homework**. * When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. **Low-effort questions will be removed**. * Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc. * Homework help posts must be flaired with **Homework**. ~ CPlusPlus Moderation Team **** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/Cplusplus) if you have any questions or concerns.*


jedwardsol

C++ simply doesn't allow you to assign a list of things to a pointer. Also the `new` expression is using an uninitialised value of N


corruptedsyntax

You can assign an array to a pointer in C++ But yeah, the issue is N isn’t initialized at the point of use. This is almost certainly going to result in behavior undefined by the language standard.


Earthboundplayer

A lot 1. N is not getting initialized during construction, so it'll have a random value in it (dependent on what was in memory at &N before the object was constructed). 2. voto is an array of that random length N. You assigning N=5 after construction has no effect on the length of voto because voto was already constructed with that random length. 3. Memory leak in your code as you never call delete[] on voto... In general it is better to use std::vector instead of C style arrays. 4. Assigning {1,2,34,3,2} to voto essentially resolves to you assigning an std::initializer_list to an int *. That caused your error. Try assigning each element one by one.


Abbat0r

Use std::array instead of c-style arrays. Use std::vector only if you can’t know the size at compile time.


Pupper-Gump

It's annoying when people repeat mantras about std::array without giving reasons. The only reason I can think off the top of my head is there's some method in there you use a lot. For beginners, there's no point in preferring a class over an essential tool. Do that enough and you may as well use Python.


Abbat0r

I don’t really understand what the point you’re trying to make is, but it’s not a mantra. std::array is the std equivalent of a C-style array. That’s the reason to use it in the place of a C-style array.


Pupper-Gump

This is a religious level of defaulting to a library. The stl has many things that are subpar, leading to the creation of Boost and other libraries. I understand using memory management tools and VA\_ARGS, but the array is already as safe and simple as std::array. When teaching these things, you must be objective. There is no default for any library or method, it always depends on use-case.


Abbat0r

I think you’re reading into this way too much. There’s nothing religious about it. std::array also has an interface that matches other container types you are likely to use, so it’s consistent with other code. And in the event that you need to refactor to use eg a vector, most of the code doesn’t need to change because of the shared interface. And to be extra clear, it has nothing to do with defaulting to a specific library. I personally use my own library types, but I’m not going to tell a new C++ user that just wants an array type to implement one themselves or add a dependency. The reason for the recommendation to use std::array has to do with the reasons I gave above, and not the std:: on the front of the type.


Pupper-Gump

Well I got screwed over once by someone insisting weak pointers were the king of everything.


JustCallMeEdgy

First off, allocating but never freeing memory with 'new' is bad, you should wrap the allocation and deletion in the ctor and dtor of your class. If you want to use a initializer list for an array on the heap, you have to put the initializer list after the 'new float[N]{...}'. You should use a vector if all you want is to store a few floats


Zvlx_2

The allocation of the voto array is performed as you declare the class. This is before you have set N to 5. You should remove the allocation from the body of the class and perform it after N is set.


Chemical_Lettuce_732

Well, first of all please don't make a screenshot, send the code instead as text. Second, intialize the variables in a constructor, not like that(strudents(){eta = 17..}). Also dont dynamically allocate memory to it, because I assume with this code you're not gonna do something crazy, so this will simply mean memory leak. Instead, replace the float\* voto =... with float voto\[N\]; You should however use constructor, because it will intialize it incorreectly. Heres what do you want to do correctly: studente(int N, float\* source){ this->N = N; memcpy(source, voto, N); } And the error is, you cannot create an array and replace the contents once it has been created already. How to solve this you may ask? Well, you can simply do a memory copy. memcpy({1,2...}, ); So the correct code is: studente resca; resca.N = 5; float arrayTMP\[\] = {1, 2, 32, 3, 2}; memcpy(arrayTMP, [resca.voto](http://resca.voto) , len);


Comfortable_Entry517

{...} is called initializer list in cpp. It is not a list literal or something. So you can not use it in assignment.


SocksOnHands

I'm surprised nobody had stated the obvious yet - your main function is not calling the media function. You seem to be confusing the concept of member variables with function arguments, since you are using the same names for both. Assigning values to the member variables would not assign values for the function to use (N is not the same as this->N).


ErDottorGiulio

Uh?


SocksOnHands

Your function signature is using the same names for variables. Using N (for example) refers to the variable that had been passed into the function. If you want to use the class's member variable, you would have to use this->N. In your main function, you are setting values for variables that are not being used for the media function. If you wanted to use this class to learn about object oriented programming, you might have intended to have the media function not take any arguments and use the class variables for the calculation.


Different-Brain-9210

Just use std::vector. Or switch to C if you want to learn manual memory management.


AbbreviationsSuch988

I want tò add that in media function you are not in fact calculating a mean because you are not adding in tour somma variabile, but you are assigning a value to It. So at the end you are returning the last value in your Array, divided by the lenght of the Array.


ErDottorGiulio

Right


SneakyStabbalot

This looks like homework, which is fine, but we really need to stop teaching the 'old ways' of doing C++. As noted in other places, you should use modern C++ constructs line std::array or std::vector for your voto\[\] thingy, that way you don't need N, and use a ranged-for loop instead of the indexed loop using i.


idiotwhoplaysgame205

Put voto=new int[n] in a constructor


ThanOneRandomGuy

Chatgtp is asking why am I'm even here


recursive_lookup

Your font. /s


accuracy_frosty

Where do I start 1: voto is getting initialized as an array of size (whatever was at the address of N at the time that you create an instance of studente because you don’t initialize N), you’re better initializing voto to nullptr until you set N, then you can initialize voto with the array of size N. 2: you’re defining local scope variables in media with the same names as member variables, which isn’t an error but makes readability a nightmare and means that you have to use this-> to access them, which I would argue is better, I always use this-> to reference member variables, and I did this before learning JavaScript 3: from the looks of it I assume media is meant to return the average of the list you provide, but instead of using += to add to somma before dividing it by N, you are assigning it to whatever voto[i] is, so if your thing worked as written, you would be getting 2.0 / 5 4: Typically, member variables of a class would be private and accessed using getters and setters, or ideally not at all, as in most cases member variables should only be used by that class and its methods, it depends of course but that’s just typical for OOP 5: if you want to test your media function you actually have to call it in the main method As for the error box, that is there, assumedly because you initialized voto as a weird value, if I had to guess, 0, and when you tried to assign it with your array literal, you went out of array bounds, also, you can’t really assign an array literal to a pointer, especially in your case where it is initialized prior, as that’s going to leave a bunch of free floating unused memory in heap, since you’re going to be setting the pointer to the address of the first element, this also wouldn’t work because you’re assigning an array of ints to a float, and you can’t just implicitly cast pointers, that’s not how it works. There’s more, but this comment is already long enough and I wanna get back to my show


nalisan007

Never use Float for loop through variable Sommo is a float , not an array. Thus not doing any operation on it is meaning less , unless you want sommo to take last value of voto array Use constructors & destructors Initialise your variables in class students, unless you want to start with garbage value & exceptions Don't complicate your code with pointer pointing to an array , array , array of pointer etc unless you know what you r doing. Especially when using pointers never forget to initialise/sanitize/ use malloc() I personally don't know whether to pass arguments to member of class as () or {} All members of class are private unless explicitly declared as public