C++: Binary Heap [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm working on a C++ implementation of a Binary Heap, but I'm having some issues getting started. Here's a snippet of my code:
class binaryHeap {
public:
// Constructor
binaryHeap(int _capacity)
{
// initializes the binary heap with a capacity, size, and space in memory
_size = 0;
_n = ceil(pow(2, log10(_capacity)/log10(2)));
_heap = new int[_n];
}
~binaryHeap(void)
{
delete[] _heap;
}
/* Omitted: insert, remove, size, capacity functions
Not necessary to the issue I'm having */
private:
int _size;
int _capacity;
int _n;
int *_heap;
};
In the main.cpp file, when I write the following line:
struct BinaryHeap heap(10);
I get the error: Variable has incomplete type 'struct BinaryHeap'. Any ideas what is causing this?

I think this is a typo problem. Your binary heap class is binaryHeap, while in your main function, you are saying struct BinaryHeap heap(10);, which in the compiler's POV is a completely different type.

Related

C++ how to use strcpy for a 2d array in a struct [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My compiler gives me an error when I do this:
strcpy(skin.conclusion[0], "Mel");
My struct looks like this:
struct list{
char conclusion[10][4] = {};
}skin;
What am I doing wrong or is there something else other than strcpy that I'm supposed to use.
Here is the full code that worked for me. Of course, this is C style C++ and not modern C++, there are many reasons to prefer std::string
#include <iostream>
#include <cstring>
struct list{
char conclusion[10][4] = {};
}
skin;
int main()
{
strcpy(skin.conclusion[0], "Mel");
std::cout<<skin.conclusion[0];
return 0;
}

Heap corruption detected when deleting char* [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am tring to copy a const char* to a char* and below is what I have written:
int main()
{
const char* string = "Hello";
int size = strlen(string) + 1; // add '/0' in the end
char* data = new char(size);
memcpy(data, string, size);
delete data;
}
unfortunately I am getting a error with "Heap corruption detected: after normal block (#77)".
I have no idea what the problem is.
I am complying my code using MSVC under visual studio 2019.
new char(size) is not allocating an array of characters but allocating single character whose value is size.
You should use new char[size] to allocate an array and delete[] data; to delete the array.

why pointer->data works , whereas *double_pointer->data doesn't? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a class 'node' which has two members data and *next. When I am accessing data using pointer then it is working fine whereas when i am accessing it using double pointer , it is throwing an error . Can anyone explain me why this is happening?
class node{
public:
int data;
node *next;
};
push(&a,8);
void push(node **p , int x){
*p->data = 11;
}
Why this p->data showing error:
request for member ‘data’ in ‘ p’, which is of pointer type ‘node*’
Because *p->data is *(p->data), not (*p)->data.
Read about operator precedence.

Getting iterator for an array with a constant length in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);

Heap corruption detected when use delete [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i run this code and got this exception in visual studio:
int main ()
{
int * x=new int(23);
for(int i=0;i<9;i++)
{
x[i]=i;
}
delete []x;
return(0);
}
thanks for your help ;)
int * x = new int(23); does not do what you think. It allocates the memory for a int and gives it the value of 23.
What you want to do is this:
int * x = new int[23];
Although, I would recommend you use std::vector if the size change or std::array if the size is fix.
If you use new you need to use delete. If you use new[] then you need to use delete []. new() is not the same as new[]. new(someval) sets the new object to someval. It does not make someval number of objects.