0
Tonnes of C++ errors which I have no clue how to tackle
I'm trying to make a code for datatypes Stack, queue and linkedlist. I've been trying to fix these errors for ages, I've fixed some of them (it had at least 10 errors and 4 warnings at the start) but I have a few which I don't know. Apparently sizeof() doesn't work for empty int arrays (which isn't true, it's a generic array). and I have a "flexible item" in the list (which once again, is an EMPTY generic list). What can I do? https://code.sololearn.com/cb09eM5L19ZE/?ref=app
9 odpowiedzi
+ 3
1. u need to set the size of array during declaration
2. Since u implementing a stack, know that array size cannot be changed once declared
3. In function view() when first loop will be ran, return statement will be encountered and so it will not loop further
+ 1
How are you going to add items to empty array? This array has to have fixed size, cuz it allocates memory at compile time.
You can use std::vector (which is probably not what you are trying to do), or reallocate memory, but then you must declare it as T pointer.
sizeof() operator returns size of variable in bytes, so you are not getting this size as 1 (number of elements), but as 2 or 4, depends on implementation. Probably sizeof(int) is giving you 4, so your for loop will fail trying to read memory which you have no access to
+ 1
Juan Pablo Segundo In python you can do
x = []
x.append(1)
print(x)
>> [1]
I wanted to do the same in C++.
As for sizeof, I was told by stackoverflow that you can use sizeof to get the length of the array.
+ 1
Clueless Coder you can, but you have to divide whole array by one of its elements. For example
sizeof(array)/sizeof(array[0]) will give you number of elements in the array
+ 1
Yes sizeof is used to get size of array
In c++ u gotta be careful more than in python since python has inbuilt methods to change size of array
+ 1
C++ doesn't have one inbuilt method to change size of array u need to implement one
0
kris If I set the size I don't think it will work. I'm making a stack list, meaning me (or the user) can add items to it. Meaning the size starts at zero and will constantly be changing
0
Clueless Coder if you want to change it's size at run time, you have to make it T pointer, and keep reallocating memory.
When you add an item, you have to make copy of whole array, allocate memory for the array equal to (sizeof(old_array)+sizeof(T)), paste old array to the new one, and add new item at the last position of new array
0
「HAPPY TO HELP」 Thanks, the recommendation for dynamic arrays helped a ton. Do you know how to print the items of a dynamic array? I tried size() but it throws an error, I tried using asterisks but that didn't work either.
When it did work (sizeof) it printed 30. I assume that's literally the size of the array in bytes. I'm struggling to actually display the array.
There is a stack overflow example but that uses Vectors and is really complicated.
Also, thanks for pointing out the return statement issue. I fixed that.