Reference cannot be initialized/expression must have type class












-1















I'm doing a quick binary tree program as a homework, but I am getting weird errors that I can't seem to figure out how to fix. Normally I'm programming in C#, and C is just slightly different but different enough for me to get confused.



Here is the code that is giving the error:



void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}


And here is the whole script:



#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

struct stub
{
char crown[51];//some custom name - can be empty
float b;//comparable value for tree sort
int c;//limited by maxint
stub *l;//left node(normally smaller "b" value than this node)
stub *r;//right node(normally bigger "b" value than this node)
};

stub *stubList[255];//empty list of a stub array with a safe casual amount
int arraySize = 0;

stub *head = NULL;//main stub(first)
stub *latest = NULL;//latest stub

stub *st = NULL;//used for creating and checking

FILE *fs = NULL;
char fileName[255];

int maxint = 20;//for var 'c'

void BTreeNodeToArray(stub *s) {
stubList[arraySize] = s;

arraySize++;
}

void Swap(stub &s1, stub &s2) {
stub temp = s2;

s1 = s2;
s2 = temp;
}

int MaxMin(int num) {
int clampedInt = num;
if (num < 0) clampedInt = 0;
else if (num > maxint) clampedInt = maxint;

return clampedInt;
}

//Create a completely new stub with crown, b, and c variables being filled here
void CreateElement() {
st = new stub;
printf("Adding information for node:n");
printf("Node name: "); gets_s(st->crown);
printf("n");

float f;
printf("Node float value (B): "); scanf_s("%f", &f);
st->b = f;
printf("n");

int d;
printf("Node integer value (C): "); scanf_s("%d", &d);
st->c = d;
printf("n");
st->c = MaxMin(st->c);

st->l = NULL;
st->r = NULL;
}

//creates the very first stub(root/head)
void CreateFirst() {
printf("First in tree. Adding root node...n");

CreateElement();
head = st;
latest = head;
BTreeNodeToArray(head);

printf("Added head stubncrown: %snValue: %fnExtra: %d", head->crown, head->b, head->c);
getchar();
getchar();
}

void AddStub() {
if (head == NULL) {
CreateFirst();
}
else {
CreateElement();//create newest node
latest = st;
st = head;
int depth = 0;
while (1) {
if ((latest->b <= st->b)) {//choose left if true
printf("Went leftn");
depth++;
if (st->l == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->l = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (left)n");
st = st->l;
}
}
else {//choose right
printf("Went rightn");
depth++;
if (st->r == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->r = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (right)n");
st = st->r;
}
}
}
}
}

void ViewArray() {
for (int i = 0; i < arraySize; i++)
{
printf_s("Node [%d]:ntCrown: %sntweight: %fntExta value(0 - %d): %dn", i, stubList[i]->crown, stubList[i]->b, maxint, stubList[i]->c);
}
getchar();
}

void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}

void ProcessArray() {
char c = ' ';
int found = 0;

printf("Process with character: n"); c = _getch();

if (arraySize <= 0) {
printf("No List!");
return;
}

char chkstr[5];
for (short i = 0; i < 5; i++)//simple assign to a 'string' 5 times
{
chkstr[i] = c;
}

for (int i = 0; i < arraySize; i++)
{
if (strstr(stubList[i]->crown, chkstr) != NULL) {
// contains
printf("B = %f at [%d] n", stubList[i]->b, i);
found++;
}
}

if (found > 0) {
printf("---Found: %d with character %c---", found, c);
}
else {
printf("No elements found with '%c'", c);
}
getchar();
}

void ExportArray() {
FILE *fs = NULL;
char fileName[255];
errno_t err;

printf("Save to file as: n"); gets_s(fileName);

if (strlen(fileName) == 0) {
printf("Failed to create file. File name empty!");
_getch();
_getch();
}

err = fopen_s(&fs, fileName, "wb");
if (err != 0) {
printf("Failed to create file!!!");
_getch();
_getch();
return;
}

for (int i = 0; i < arraySize; i++)
{
int written = fwrite(&stubList[i], sizeof(stub), 1, fs);
//fwrite(&gr[i], sizeof(student), 1, fs);
}
int numclosed = _fcloseall();

printf("Exported to file: %s", fileName);
getchar();
}

void CleanReset() {//reset all values and release memory (function used for import)
for (int i = 0; i < arraySize; i++)
{
delete stubList[i];
}

arraySize = 0;
st = NULL;
head = NULL;
latest = NULL;
}

void ImportArray() {
FILE *fs = NULL;
char fname[255];
errno_t err;

printf("Open FIle: "); gets_s(fname);

err = fopen_s(&fs, fname, "rb");
if (err == 0) {
CleanReset();

fseek(fs, 0, SEEK_END);
long size = ftell(fs);
arraySize = size / sizeof(stub);//get amount of students saved
fseek(fs, 0, SEEK_SET);

int i = 0;

st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
while (!feof(fs)) {
i++;
st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
}

printf("File data imported. Size: %d", arraySize);
getchar();
return;
}

printf("Failed to import file!!!");
getchar();
}

void main()
{
system("chcp 65001");//use utf8

char izb = 'i';

while (izb != '0') {
system("cls");

printf_s("1. Add stubn");
printf_s("2. View Arrayn");
printf_s("3. Sort Arrayn");
printf_s("4. Processn");
printf_s("5. Export array to filen");
printf_s("6. Import array from filen");
printf_s("0. Exitnn");

printf_s("Choose action:n"); izb = _getch();

switch (izb)
{
case '1':
AddStub();
break;
case '2':
ViewArray();
break;
case '3':
SortArray();
break;
case '4':
ProcessArray();
break;
case '5':
ExportArray();
break;
case '6':
ImportArray();
break;
case '0':
//Exit();
break;
}
}
}









share|improve this question




















  • 1





    There are several things in your code, which are not C, but C++ (new, stub instead of struct stub and pass by reference). Are you sure that you write C code?

    – mch
    Nov 23 '18 at 10:50






  • 3





    What is the error?

    – mjcs
    Nov 23 '18 at 10:51






  • 5





    Request like this "help as soon as possible because im short on time." typically do not accelerate things, but provoke the opposite, so better take care.

    – alk
    Nov 23 '18 at 10:55






  • 1





    Show the error message with exact line numbers.

    – Nidhoegger
    Nov 23 '18 at 10:56






  • 3





    C++ is not "slightly" different from C#, and C is even more different – the similarities are mainly punctuation and the spelling of some keywords. I would say that they're similar enough to get you confused.

    – molbdnilo
    Nov 23 '18 at 11:34


















-1















I'm doing a quick binary tree program as a homework, but I am getting weird errors that I can't seem to figure out how to fix. Normally I'm programming in C#, and C is just slightly different but different enough for me to get confused.



Here is the code that is giving the error:



void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}


And here is the whole script:



#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

struct stub
{
char crown[51];//some custom name - can be empty
float b;//comparable value for tree sort
int c;//limited by maxint
stub *l;//left node(normally smaller "b" value than this node)
stub *r;//right node(normally bigger "b" value than this node)
};

stub *stubList[255];//empty list of a stub array with a safe casual amount
int arraySize = 0;

stub *head = NULL;//main stub(first)
stub *latest = NULL;//latest stub

stub *st = NULL;//used for creating and checking

FILE *fs = NULL;
char fileName[255];

int maxint = 20;//for var 'c'

void BTreeNodeToArray(stub *s) {
stubList[arraySize] = s;

arraySize++;
}

void Swap(stub &s1, stub &s2) {
stub temp = s2;

s1 = s2;
s2 = temp;
}

int MaxMin(int num) {
int clampedInt = num;
if (num < 0) clampedInt = 0;
else if (num > maxint) clampedInt = maxint;

return clampedInt;
}

//Create a completely new stub with crown, b, and c variables being filled here
void CreateElement() {
st = new stub;
printf("Adding information for node:n");
printf("Node name: "); gets_s(st->crown);
printf("n");

float f;
printf("Node float value (B): "); scanf_s("%f", &f);
st->b = f;
printf("n");

int d;
printf("Node integer value (C): "); scanf_s("%d", &d);
st->c = d;
printf("n");
st->c = MaxMin(st->c);

st->l = NULL;
st->r = NULL;
}

//creates the very first stub(root/head)
void CreateFirst() {
printf("First in tree. Adding root node...n");

CreateElement();
head = st;
latest = head;
BTreeNodeToArray(head);

printf("Added head stubncrown: %snValue: %fnExtra: %d", head->crown, head->b, head->c);
getchar();
getchar();
}

void AddStub() {
if (head == NULL) {
CreateFirst();
}
else {
CreateElement();//create newest node
latest = st;
st = head;
int depth = 0;
while (1) {
if ((latest->b <= st->b)) {//choose left if true
printf("Went leftn");
depth++;
if (st->l == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->l = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (left)n");
st = st->l;
}
}
else {//choose right
printf("Went rightn");
depth++;
if (st->r == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->r = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (right)n");
st = st->r;
}
}
}
}
}

void ViewArray() {
for (int i = 0; i < arraySize; i++)
{
printf_s("Node [%d]:ntCrown: %sntweight: %fntExta value(0 - %d): %dn", i, stubList[i]->crown, stubList[i]->b, maxint, stubList[i]->c);
}
getchar();
}

void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}

void ProcessArray() {
char c = ' ';
int found = 0;

printf("Process with character: n"); c = _getch();

if (arraySize <= 0) {
printf("No List!");
return;
}

char chkstr[5];
for (short i = 0; i < 5; i++)//simple assign to a 'string' 5 times
{
chkstr[i] = c;
}

for (int i = 0; i < arraySize; i++)
{
if (strstr(stubList[i]->crown, chkstr) != NULL) {
// contains
printf("B = %f at [%d] n", stubList[i]->b, i);
found++;
}
}

if (found > 0) {
printf("---Found: %d with character %c---", found, c);
}
else {
printf("No elements found with '%c'", c);
}
getchar();
}

void ExportArray() {
FILE *fs = NULL;
char fileName[255];
errno_t err;

printf("Save to file as: n"); gets_s(fileName);

if (strlen(fileName) == 0) {
printf("Failed to create file. File name empty!");
_getch();
_getch();
}

err = fopen_s(&fs, fileName, "wb");
if (err != 0) {
printf("Failed to create file!!!");
_getch();
_getch();
return;
}

for (int i = 0; i < arraySize; i++)
{
int written = fwrite(&stubList[i], sizeof(stub), 1, fs);
//fwrite(&gr[i], sizeof(student), 1, fs);
}
int numclosed = _fcloseall();

printf("Exported to file: %s", fileName);
getchar();
}

void CleanReset() {//reset all values and release memory (function used for import)
for (int i = 0; i < arraySize; i++)
{
delete stubList[i];
}

arraySize = 0;
st = NULL;
head = NULL;
latest = NULL;
}

void ImportArray() {
FILE *fs = NULL;
char fname[255];
errno_t err;

printf("Open FIle: "); gets_s(fname);

err = fopen_s(&fs, fname, "rb");
if (err == 0) {
CleanReset();

fseek(fs, 0, SEEK_END);
long size = ftell(fs);
arraySize = size / sizeof(stub);//get amount of students saved
fseek(fs, 0, SEEK_SET);

int i = 0;

st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
while (!feof(fs)) {
i++;
st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
}

printf("File data imported. Size: %d", arraySize);
getchar();
return;
}

printf("Failed to import file!!!");
getchar();
}

void main()
{
system("chcp 65001");//use utf8

char izb = 'i';

while (izb != '0') {
system("cls");

printf_s("1. Add stubn");
printf_s("2. View Arrayn");
printf_s("3. Sort Arrayn");
printf_s("4. Processn");
printf_s("5. Export array to filen");
printf_s("6. Import array from filen");
printf_s("0. Exitnn");

printf_s("Choose action:n"); izb = _getch();

switch (izb)
{
case '1':
AddStub();
break;
case '2':
ViewArray();
break;
case '3':
SortArray();
break;
case '4':
ProcessArray();
break;
case '5':
ExportArray();
break;
case '6':
ImportArray();
break;
case '0':
//Exit();
break;
}
}
}









share|improve this question




















  • 1





    There are several things in your code, which are not C, but C++ (new, stub instead of struct stub and pass by reference). Are you sure that you write C code?

    – mch
    Nov 23 '18 at 10:50






  • 3





    What is the error?

    – mjcs
    Nov 23 '18 at 10:51






  • 5





    Request like this "help as soon as possible because im short on time." typically do not accelerate things, but provoke the opposite, so better take care.

    – alk
    Nov 23 '18 at 10:55






  • 1





    Show the error message with exact line numbers.

    – Nidhoegger
    Nov 23 '18 at 10:56






  • 3





    C++ is not "slightly" different from C#, and C is even more different – the similarities are mainly punctuation and the spelling of some keywords. I would say that they're similar enough to get you confused.

    – molbdnilo
    Nov 23 '18 at 11:34
















-1












-1








-1








I'm doing a quick binary tree program as a homework, but I am getting weird errors that I can't seem to figure out how to fix. Normally I'm programming in C#, and C is just slightly different but different enough for me to get confused.



Here is the code that is giving the error:



void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}


And here is the whole script:



#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

struct stub
{
char crown[51];//some custom name - can be empty
float b;//comparable value for tree sort
int c;//limited by maxint
stub *l;//left node(normally smaller "b" value than this node)
stub *r;//right node(normally bigger "b" value than this node)
};

stub *stubList[255];//empty list of a stub array with a safe casual amount
int arraySize = 0;

stub *head = NULL;//main stub(first)
stub *latest = NULL;//latest stub

stub *st = NULL;//used for creating and checking

FILE *fs = NULL;
char fileName[255];

int maxint = 20;//for var 'c'

void BTreeNodeToArray(stub *s) {
stubList[arraySize] = s;

arraySize++;
}

void Swap(stub &s1, stub &s2) {
stub temp = s2;

s1 = s2;
s2 = temp;
}

int MaxMin(int num) {
int clampedInt = num;
if (num < 0) clampedInt = 0;
else if (num > maxint) clampedInt = maxint;

return clampedInt;
}

//Create a completely new stub with crown, b, and c variables being filled here
void CreateElement() {
st = new stub;
printf("Adding information for node:n");
printf("Node name: "); gets_s(st->crown);
printf("n");

float f;
printf("Node float value (B): "); scanf_s("%f", &f);
st->b = f;
printf("n");

int d;
printf("Node integer value (C): "); scanf_s("%d", &d);
st->c = d;
printf("n");
st->c = MaxMin(st->c);

st->l = NULL;
st->r = NULL;
}

//creates the very first stub(root/head)
void CreateFirst() {
printf("First in tree. Adding root node...n");

CreateElement();
head = st;
latest = head;
BTreeNodeToArray(head);

printf("Added head stubncrown: %snValue: %fnExtra: %d", head->crown, head->b, head->c);
getchar();
getchar();
}

void AddStub() {
if (head == NULL) {
CreateFirst();
}
else {
CreateElement();//create newest node
latest = st;
st = head;
int depth = 0;
while (1) {
if ((latest->b <= st->b)) {//choose left if true
printf("Went leftn");
depth++;
if (st->l == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->l = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (left)n");
st = st->l;
}
}
else {//choose right
printf("Went rightn");
depth++;
if (st->r == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->r = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (right)n");
st = st->r;
}
}
}
}
}

void ViewArray() {
for (int i = 0; i < arraySize; i++)
{
printf_s("Node [%d]:ntCrown: %sntweight: %fntExta value(0 - %d): %dn", i, stubList[i]->crown, stubList[i]->b, maxint, stubList[i]->c);
}
getchar();
}

void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}

void ProcessArray() {
char c = ' ';
int found = 0;

printf("Process with character: n"); c = _getch();

if (arraySize <= 0) {
printf("No List!");
return;
}

char chkstr[5];
for (short i = 0; i < 5; i++)//simple assign to a 'string' 5 times
{
chkstr[i] = c;
}

for (int i = 0; i < arraySize; i++)
{
if (strstr(stubList[i]->crown, chkstr) != NULL) {
// contains
printf("B = %f at [%d] n", stubList[i]->b, i);
found++;
}
}

if (found > 0) {
printf("---Found: %d with character %c---", found, c);
}
else {
printf("No elements found with '%c'", c);
}
getchar();
}

void ExportArray() {
FILE *fs = NULL;
char fileName[255];
errno_t err;

printf("Save to file as: n"); gets_s(fileName);

if (strlen(fileName) == 0) {
printf("Failed to create file. File name empty!");
_getch();
_getch();
}

err = fopen_s(&fs, fileName, "wb");
if (err != 0) {
printf("Failed to create file!!!");
_getch();
_getch();
return;
}

for (int i = 0; i < arraySize; i++)
{
int written = fwrite(&stubList[i], sizeof(stub), 1, fs);
//fwrite(&gr[i], sizeof(student), 1, fs);
}
int numclosed = _fcloseall();

printf("Exported to file: %s", fileName);
getchar();
}

void CleanReset() {//reset all values and release memory (function used for import)
for (int i = 0; i < arraySize; i++)
{
delete stubList[i];
}

arraySize = 0;
st = NULL;
head = NULL;
latest = NULL;
}

void ImportArray() {
FILE *fs = NULL;
char fname[255];
errno_t err;

printf("Open FIle: "); gets_s(fname);

err = fopen_s(&fs, fname, "rb");
if (err == 0) {
CleanReset();

fseek(fs, 0, SEEK_END);
long size = ftell(fs);
arraySize = size / sizeof(stub);//get amount of students saved
fseek(fs, 0, SEEK_SET);

int i = 0;

st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
while (!feof(fs)) {
i++;
st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
}

printf("File data imported. Size: %d", arraySize);
getchar();
return;
}

printf("Failed to import file!!!");
getchar();
}

void main()
{
system("chcp 65001");//use utf8

char izb = 'i';

while (izb != '0') {
system("cls");

printf_s("1. Add stubn");
printf_s("2. View Arrayn");
printf_s("3. Sort Arrayn");
printf_s("4. Processn");
printf_s("5. Export array to filen");
printf_s("6. Import array from filen");
printf_s("0. Exitnn");

printf_s("Choose action:n"); izb = _getch();

switch (izb)
{
case '1':
AddStub();
break;
case '2':
ViewArray();
break;
case '3':
SortArray();
break;
case '4':
ProcessArray();
break;
case '5':
ExportArray();
break;
case '6':
ImportArray();
break;
case '0':
//Exit();
break;
}
}
}









share|improve this question
















I'm doing a quick binary tree program as a homework, but I am getting weird errors that I can't seem to figure out how to fix. Normally I'm programming in C#, and C is just slightly different but different enough for me to get confused.



Here is the code that is giving the error:



void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}


And here is the whole script:



#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

struct stub
{
char crown[51];//some custom name - can be empty
float b;//comparable value for tree sort
int c;//limited by maxint
stub *l;//left node(normally smaller "b" value than this node)
stub *r;//right node(normally bigger "b" value than this node)
};

stub *stubList[255];//empty list of a stub array with a safe casual amount
int arraySize = 0;

stub *head = NULL;//main stub(first)
stub *latest = NULL;//latest stub

stub *st = NULL;//used for creating and checking

FILE *fs = NULL;
char fileName[255];

int maxint = 20;//for var 'c'

void BTreeNodeToArray(stub *s) {
stubList[arraySize] = s;

arraySize++;
}

void Swap(stub &s1, stub &s2) {
stub temp = s2;

s1 = s2;
s2 = temp;
}

int MaxMin(int num) {
int clampedInt = num;
if (num < 0) clampedInt = 0;
else if (num > maxint) clampedInt = maxint;

return clampedInt;
}

//Create a completely new stub with crown, b, and c variables being filled here
void CreateElement() {
st = new stub;
printf("Adding information for node:n");
printf("Node name: "); gets_s(st->crown);
printf("n");

float f;
printf("Node float value (B): "); scanf_s("%f", &f);
st->b = f;
printf("n");

int d;
printf("Node integer value (C): "); scanf_s("%d", &d);
st->c = d;
printf("n");
st->c = MaxMin(st->c);

st->l = NULL;
st->r = NULL;
}

//creates the very first stub(root/head)
void CreateFirst() {
printf("First in tree. Adding root node...n");

CreateElement();
head = st;
latest = head;
BTreeNodeToArray(head);

printf("Added head stubncrown: %snValue: %fnExtra: %d", head->crown, head->b, head->c);
getchar();
getchar();
}

void AddStub() {
if (head == NULL) {
CreateFirst();
}
else {
CreateElement();//create newest node
latest = st;
st = head;
int depth = 0;
while (1) {
if ((latest->b <= st->b)) {//choose left if true
printf("Went leftn");
depth++;
if (st->l == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->l = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (left)n");
st = st->l;
}
}
else {//choose right
printf("Went rightn");
depth++;
if (st->r == NULL) {//node free, assign here
printf("Node assigned at depth %dn", depth);
st->r = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (right)n");
st = st->r;
}
}
}
}
}

void ViewArray() {
for (int i = 0; i < arraySize; i++)
{
printf_s("Node [%d]:ntCrown: %sntweight: %fntExta value(0 - %d): %dn", i, stubList[i]->crown, stubList[i]->b, maxint, stubList[i]->c);
}
getchar();
}

void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;

do {
flag = 1;

for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);

printf("Sorted by value of variable 'b'"); _getch(); _getch();
}

void ProcessArray() {
char c = ' ';
int found = 0;

printf("Process with character: n"); c = _getch();

if (arraySize <= 0) {
printf("No List!");
return;
}

char chkstr[5];
for (short i = 0; i < 5; i++)//simple assign to a 'string' 5 times
{
chkstr[i] = c;
}

for (int i = 0; i < arraySize; i++)
{
if (strstr(stubList[i]->crown, chkstr) != NULL) {
// contains
printf("B = %f at [%d] n", stubList[i]->b, i);
found++;
}
}

if (found > 0) {
printf("---Found: %d with character %c---", found, c);
}
else {
printf("No elements found with '%c'", c);
}
getchar();
}

void ExportArray() {
FILE *fs = NULL;
char fileName[255];
errno_t err;

printf("Save to file as: n"); gets_s(fileName);

if (strlen(fileName) == 0) {
printf("Failed to create file. File name empty!");
_getch();
_getch();
}

err = fopen_s(&fs, fileName, "wb");
if (err != 0) {
printf("Failed to create file!!!");
_getch();
_getch();
return;
}

for (int i = 0; i < arraySize; i++)
{
int written = fwrite(&stubList[i], sizeof(stub), 1, fs);
//fwrite(&gr[i], sizeof(student), 1, fs);
}
int numclosed = _fcloseall();

printf("Exported to file: %s", fileName);
getchar();
}

void CleanReset() {//reset all values and release memory (function used for import)
for (int i = 0; i < arraySize; i++)
{
delete stubList[i];
}

arraySize = 0;
st = NULL;
head = NULL;
latest = NULL;
}

void ImportArray() {
FILE *fs = NULL;
char fname[255];
errno_t err;

printf("Open FIle: "); gets_s(fname);

err = fopen_s(&fs, fname, "rb");
if (err == 0) {
CleanReset();

fseek(fs, 0, SEEK_END);
long size = ftell(fs);
arraySize = size / sizeof(stub);//get amount of students saved
fseek(fs, 0, SEEK_SET);

int i = 0;

st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
while (!feof(fs)) {
i++;
st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
}

printf("File data imported. Size: %d", arraySize);
getchar();
return;
}

printf("Failed to import file!!!");
getchar();
}

void main()
{
system("chcp 65001");//use utf8

char izb = 'i';

while (izb != '0') {
system("cls");

printf_s("1. Add stubn");
printf_s("2. View Arrayn");
printf_s("3. Sort Arrayn");
printf_s("4. Processn");
printf_s("5. Export array to filen");
printf_s("6. Import array from filen");
printf_s("0. Exitnn");

printf_s("Choose action:n"); izb = _getch();

switch (izb)
{
case '1':
AddStub();
break;
case '2':
ViewArray();
break;
case '3':
SortArray();
break;
case '4':
ProcessArray();
break;
case '5':
ExportArray();
break;
case '6':
ImportArray();
break;
case '0':
//Exit();
break;
}
}
}






c++ reference






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 20:28









halfer

14.4k758109




14.4k758109










asked Nov 23 '18 at 10:44









NevaranNevaran

196




196








  • 1





    There are several things in your code, which are not C, but C++ (new, stub instead of struct stub and pass by reference). Are you sure that you write C code?

    – mch
    Nov 23 '18 at 10:50






  • 3





    What is the error?

    – mjcs
    Nov 23 '18 at 10:51






  • 5





    Request like this "help as soon as possible because im short on time." typically do not accelerate things, but provoke the opposite, so better take care.

    – alk
    Nov 23 '18 at 10:55






  • 1





    Show the error message with exact line numbers.

    – Nidhoegger
    Nov 23 '18 at 10:56






  • 3





    C++ is not "slightly" different from C#, and C is even more different – the similarities are mainly punctuation and the spelling of some keywords. I would say that they're similar enough to get you confused.

    – molbdnilo
    Nov 23 '18 at 11:34
















  • 1





    There are several things in your code, which are not C, but C++ (new, stub instead of struct stub and pass by reference). Are you sure that you write C code?

    – mch
    Nov 23 '18 at 10:50






  • 3





    What is the error?

    – mjcs
    Nov 23 '18 at 10:51






  • 5





    Request like this "help as soon as possible because im short on time." typically do not accelerate things, but provoke the opposite, so better take care.

    – alk
    Nov 23 '18 at 10:55






  • 1





    Show the error message with exact line numbers.

    – Nidhoegger
    Nov 23 '18 at 10:56






  • 3





    C++ is not "slightly" different from C#, and C is even more different – the similarities are mainly punctuation and the spelling of some keywords. I would say that they're similar enough to get you confused.

    – molbdnilo
    Nov 23 '18 at 11:34










1




1





There are several things in your code, which are not C, but C++ (new, stub instead of struct stub and pass by reference). Are you sure that you write C code?

– mch
Nov 23 '18 at 10:50





There are several things in your code, which are not C, but C++ (new, stub instead of struct stub and pass by reference). Are you sure that you write C code?

– mch
Nov 23 '18 at 10:50




3




3





What is the error?

– mjcs
Nov 23 '18 at 10:51





What is the error?

– mjcs
Nov 23 '18 at 10:51




5




5





Request like this "help as soon as possible because im short on time." typically do not accelerate things, but provoke the opposite, so better take care.

– alk
Nov 23 '18 at 10:55





Request like this "help as soon as possible because im short on time." typically do not accelerate things, but provoke the opposite, so better take care.

– alk
Nov 23 '18 at 10:55




1




1





Show the error message with exact line numbers.

– Nidhoegger
Nov 23 '18 at 10:56





Show the error message with exact line numbers.

– Nidhoegger
Nov 23 '18 at 10:56




3




3





C++ is not "slightly" different from C#, and C is even more different – the similarities are mainly punctuation and the spelling of some keywords. I would say that they're similar enough to get you confused.

– molbdnilo
Nov 23 '18 at 11:34







C++ is not "slightly" different from C#, and C is even more different – the similarities are mainly punctuation and the spelling of some keywords. I would say that they're similar enough to get you confused.

– molbdnilo
Nov 23 '18 at 11:34














1 Answer
1






active

oldest

votes


















0














The error is that you want to pass a stub * to a function that wants a stub &. The first one is a pointer, the second one is a reference.



You have two options.



First option (imho recommended):



Change your swap function to accept pointers instead of references.



void Swap(stub *s1, stub *s2) {
stub temp = *s2;

*s1 = *s2;
*s2 = temp;
}


Second option:



Dereference the parameters before passing to swap:



Swap(*(stubList[i]), *(stubList[i + 1]));


You have to undestand that the datatype in your list is a pointer to stub, the function Swap wants a reference, which are different things in C++.



For a better understanding I recommend reading this: https://www.geeksforgeeks.org/pointers-vs-references-cpp/






share|improve this answer
























  • Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

    – Nevaran
    Nov 23 '18 at 12:50











  • Use a debugger to figure that out.

    – Nidhoegger
    Nov 23 '18 at 13:03











  • Did try it, im not sure what to look for..

    – Nevaran
    Nov 23 '18 at 13:08











  • Then you did not put any effort into it...

    – Nidhoegger
    Nov 23 '18 at 13:10






  • 1





    If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

    – Mark Plotnick
    Nov 23 '18 at 19:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445166%2freference-cannot-be-initialized-expression-must-have-type-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The error is that you want to pass a stub * to a function that wants a stub &. The first one is a pointer, the second one is a reference.



You have two options.



First option (imho recommended):



Change your swap function to accept pointers instead of references.



void Swap(stub *s1, stub *s2) {
stub temp = *s2;

*s1 = *s2;
*s2 = temp;
}


Second option:



Dereference the parameters before passing to swap:



Swap(*(stubList[i]), *(stubList[i + 1]));


You have to undestand that the datatype in your list is a pointer to stub, the function Swap wants a reference, which are different things in C++.



For a better understanding I recommend reading this: https://www.geeksforgeeks.org/pointers-vs-references-cpp/






share|improve this answer
























  • Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

    – Nevaran
    Nov 23 '18 at 12:50











  • Use a debugger to figure that out.

    – Nidhoegger
    Nov 23 '18 at 13:03











  • Did try it, im not sure what to look for..

    – Nevaran
    Nov 23 '18 at 13:08











  • Then you did not put any effort into it...

    – Nidhoegger
    Nov 23 '18 at 13:10






  • 1





    If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

    – Mark Plotnick
    Nov 23 '18 at 19:41
















0














The error is that you want to pass a stub * to a function that wants a stub &. The first one is a pointer, the second one is a reference.



You have two options.



First option (imho recommended):



Change your swap function to accept pointers instead of references.



void Swap(stub *s1, stub *s2) {
stub temp = *s2;

*s1 = *s2;
*s2 = temp;
}


Second option:



Dereference the parameters before passing to swap:



Swap(*(stubList[i]), *(stubList[i + 1]));


You have to undestand that the datatype in your list is a pointer to stub, the function Swap wants a reference, which are different things in C++.



For a better understanding I recommend reading this: https://www.geeksforgeeks.org/pointers-vs-references-cpp/






share|improve this answer
























  • Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

    – Nevaran
    Nov 23 '18 at 12:50











  • Use a debugger to figure that out.

    – Nidhoegger
    Nov 23 '18 at 13:03











  • Did try it, im not sure what to look for..

    – Nevaran
    Nov 23 '18 at 13:08











  • Then you did not put any effort into it...

    – Nidhoegger
    Nov 23 '18 at 13:10






  • 1





    If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

    – Mark Plotnick
    Nov 23 '18 at 19:41














0












0








0







The error is that you want to pass a stub * to a function that wants a stub &. The first one is a pointer, the second one is a reference.



You have two options.



First option (imho recommended):



Change your swap function to accept pointers instead of references.



void Swap(stub *s1, stub *s2) {
stub temp = *s2;

*s1 = *s2;
*s2 = temp;
}


Second option:



Dereference the parameters before passing to swap:



Swap(*(stubList[i]), *(stubList[i + 1]));


You have to undestand that the datatype in your list is a pointer to stub, the function Swap wants a reference, which are different things in C++.



For a better understanding I recommend reading this: https://www.geeksforgeeks.org/pointers-vs-references-cpp/






share|improve this answer













The error is that you want to pass a stub * to a function that wants a stub &. The first one is a pointer, the second one is a reference.



You have two options.



First option (imho recommended):



Change your swap function to accept pointers instead of references.



void Swap(stub *s1, stub *s2) {
stub temp = *s2;

*s1 = *s2;
*s2 = temp;
}


Second option:



Dereference the parameters before passing to swap:



Swap(*(stubList[i]), *(stubList[i + 1]));


You have to undestand that the datatype in your list is a pointer to stub, the function Swap wants a reference, which are different things in C++.



For a better understanding I recommend reading this: https://www.geeksforgeeks.org/pointers-vs-references-cpp/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 12:20









NidhoeggerNidhoegger

2,28311939




2,28311939













  • Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

    – Nevaran
    Nov 23 '18 at 12:50











  • Use a debugger to figure that out.

    – Nidhoegger
    Nov 23 '18 at 13:03











  • Did try it, im not sure what to look for..

    – Nevaran
    Nov 23 '18 at 13:08











  • Then you did not put any effort into it...

    – Nidhoegger
    Nov 23 '18 at 13:10






  • 1





    If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

    – Mark Plotnick
    Nov 23 '18 at 19:41



















  • Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

    – Nevaran
    Nov 23 '18 at 12:50











  • Use a debugger to figure that out.

    – Nidhoegger
    Nov 23 '18 at 13:03











  • Did try it, im not sure what to look for..

    – Nevaran
    Nov 23 '18 at 13:08











  • Then you did not put any effort into it...

    – Nidhoegger
    Nov 23 '18 at 13:10






  • 1





    If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

    – Mark Plotnick
    Nov 23 '18 at 19:41

















Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

– Nevaran
Nov 23 '18 at 12:50





Ahh thanks a ton! So I missed to change the Swap function to a pointer. I found one more bug when I was messing around in the console: The ImportArray function breaks the array list, saying memory violation. Might it be because it is a pointer stub inside the stub and the pointer after import is just pointing at the wrong memory spot? If so, any way to fix it?

– Nevaran
Nov 23 '18 at 12:50













Use a debugger to figure that out.

– Nidhoegger
Nov 23 '18 at 13:03





Use a debugger to figure that out.

– Nidhoegger
Nov 23 '18 at 13:03













Did try it, im not sure what to look for..

– Nevaran
Nov 23 '18 at 13:08





Did try it, im not sure what to look for..

– Nevaran
Nov 23 '18 at 13:08













Then you did not put any effort into it...

– Nidhoegger
Nov 23 '18 at 13:10





Then you did not put any effort into it...

– Nidhoegger
Nov 23 '18 at 13:10




1




1





If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

– Mark Plotnick
Nov 23 '18 at 19:41





If you don't learn the debugger for this program, you'll need to for the next program. Virtually everyone starting out using pointers in C makes mistakes.

– Mark Plotnick
Nov 23 '18 at 19:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445166%2freference-cannot-be-initialized-expression-must-have-type-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Sphinx de Gizeh

Dijon

Guerrita