Variable undeclared in function - c++

Hi I'm having a problem with global pointer being underclared in function.
Here is my code
#include <iostream>
using namespace std;
void push_l(int n);
struct elem{
int key;
elem *next;
} *left=NULL,*right=NULL;
void push_l(int n){
elem *p=left;
left=new elem;
left->key=n;
left->next=p;
if (right==NULL)right=left;
}
int main(){
push_l(5);
system "pause";
return 0;
}
This is one of the error messages I get - In function void push_l(int) left underclared

This is what you get for using namespace std; (std has a left too). And you don't even need iostream. The reference to left is ambiguous.
Do this:
#include <cstdlib>
struct elem{
int key;
elem *next;
} *left=NULL,*right=NULL;
void push_l(int n){
elem *p=left;
left=new elem;
left->key=n;
left->next=p;
if (right==NULL)right=left;
}
int main(){
push_l(5);
std::system("pause");
return 0;
}

Related

Why `&` is not allowed in C, but in C++ in this case? Is there any defference? [duplicate]

This question already has answers here:
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
(3 answers)
Closed 2 years ago.
Here is my C code, I use void InitList(List &L); However Code::Blocks`s buildlog has an error:
expected ';', ',' or ')' before '&' token
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef int ElementType;
struct SeqList;
typedef struct SeqList List;
void InitList(List &L);
struct SeqList
{
ElementType *data;
int CurLength;
};
/*----------------------------------*/
void InitList(List &L)
{
(&L)->data = malloc(sizeof(ElementType)*MaxSize);
(&L)->CurLength = 0;
}
int main()
{
List L;
InitList(&L);
return 0;
}
But I tried in C++, there is no error:
#include <iostream>
using namespace std;
#define InitSize 100
typedef int ElementType;
struct SeqList;
typedef struct SeqList List;
void InitList(List &L);
struct SeqList
{
ElementType *data;
int CurLength;
};
/*----------------------------------*/
void InitList(List &L)
{
L.data = new ElementType[InitSize]; //L.data = malloc(...)
L.CurLength = 0;
}
int main()
{
List L;
InitList(L);
return 0;
}
You can't pass that in a function declaration in C. You need to be using * if you want to reference the pointer to that data.
void InitList(List *L)
{
L->data = malloc(sizeof(ElementType)*MaxSize);
L->CurLength = 0;
}
And then
List *L;
InitList (&L);
In void InitList(List &L);, L is being passed by reference. C does not support references, that is a C++ feature. In C, you need to pass L by pointer instead (which is exactly what your main() is trying to do when using List L; InitList(&L);, but the declaration of InitList() is wrong for that), eg:
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef int ElementType;
struct SeqList;
typedef struct SeqList List;
void InitList(List *L);
void CleanupList(List *L);
struct SeqList
{
ElementType *data;
int CurLength;
};
/*----------------------------------*/
void InitList(List *L)
{
L->data = malloc(sizeof(ElementType)*MaxSize);
L->CurLength = 0;
}
void CleanupList(List *L)
{
free(L->data);
L->CurLength = 0;
}
int main()
{
List L;
InitList(&L);
...
CleanupList(&L);
return 0;
}

Instance of class only allows 1 method, or program crashes

I am learning classes and OOP, so I was doing some practice programs, when I came across the weirdest bug ever while programming.
So, I have the following files, beginning by my class "pessoa", located in pessoa.h:
#pragma once
#include <string>
#include <iostream>
using namespace std;
class pessoa {
public:
//constructor (nome do aluno, data de nascimento)
pessoa(string newname="asffaf", unsigned int newdate=1996): name(newname), DataN(newdate){};
void SetName(string a); //set name
void SetBornDate(unsigned int ); //nascimento
string GetName(); //get name
unsigned int GetBornDate();
virtual void Print(){}; // print
private:
string name; //nome
unsigned int DataN; //data de nascimento
};
Whose functions are defined in pessoa.cpp
#include "pessoa.h"
string pessoa::GetName ()
{
return name;
}
void pessoa::SetName(string a)
{
name = a;
}
unsigned int pessoa::GetBornDate()
{
return DataN;
}
void pessoa::SetBornDate(unsigned int n)
{
DataN=n;
}
A function, DoArray, declared in DoArray.h, and defined in the file DoArray.cpp:
pessoa** DoArray(int n)
{
pessoa* p= new pessoa[n];
pessoa** pointer= &p;
return pointer;
}
And the main file:
#include <string>
#include <iostream>
#include "pessoa.h"
#include "DoArray.h"
#include <cstdio>
using namespace std;
int main()
{
//pessoa P[10];
//cout << P[5].GetBornDate();
pessoa** a=DoArray(5);
cerr << endl << a[0][3].GetBornDate() << endl;
cerr << endl << a[0][3].GetName() << endl;
return 0;
}
The weird find is, if I comment one of the methods above, "GetBornDate" or GetName, and run, the non-commented method will run fine and as supposed. However, if both are not commented, then the first will run and the program will crash before the 2nd method.
Sorry for the long post.
Let's look into this function:
int *get()
{
int i = 0;
return &i;
}
what is the problem with it? It is returning pointer to a local variable, which does not exist anymore when function get() terminates ie it returns dangling pointer. Now your code:
pessoa** DoArray(int n)
{
pessoa* p= new pessoa[n];
return &p;
}
do you see the problem?
To clarify even more:
typedef pessoa * pessoa_ptr;
pessoa_ptr* DoArray(int n)
{
pessoa_ptr p= whatever;
return &p;
}
you need to understand that whatever you assign to p does not change lifetime of p itself. Pointer is the same variable as others.

function of adding a new element to list

I have a problem when trying to implement a program which adds a new element to an existing list. Here it is:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct person{ int v;
struct person *next;
};
void add(struct person *head, int n)
{ struct person *nou;
nou=(person*)malloc(sizeof(struct person));
nou->next=head;
head=nou;
nou->v=n;
}
int main()
{
struct person *head,*current,*nou;
head=NULL;
nou=(person*)malloc(sizeof(struct person));
nou->next=head;
head=nou;
nou->v=10;
add(head,14);
current=head;
while(current!=NULL)
{ cout<<current->v<<endl;
current=current->next;
}
return 0;
}
When i run it, it appears that there is only the element with the value 10 in it. What is the problem?
You need to pass in a pointer to the head pointer so that its value can be changed.
void add(struct person **head, int n)
{
struct person *nou;
nou=(person*)malloc(sizeof(struct person));
nou->next=*head;
*head=nou;
nou->v=n;
}
Call it like this:
add(&head,14);

Pushing to custom stack class freezing .exe

I'm using a stack class, however every time I push something to the stack, the executable freezes and stops working once the line of code pushing is reached.
Could I please get some help on as to why?
My stack.h:
#ifndef STACK_H
#define STACK_H
#include <cassert>
namespace standard
{
class Stack
{
public:
static const int CAPACITY = 30;
void stack() {used=0;};
void push (const char entry);
void pop();
bool empty() const;
int size() const;
char top() const;
private:
char data[CAPACITY];
int used;
};
}
#endif
My stack.cpp:
#include "stack.h"
namespace standard
{
void Stack::push(const char entry)
{
assert(size() < CAPACITY);
data[used] = entry;
++used;
}
void Stack::pop()
{
assert(!empty());
--used;
}
char Stack::top() const
{
assert(!empty());
return data[used-1];
}
int Stack::size() const
{
return used;
}
bool Stack::empty() const
{
if (size() == 0)
return true;
else
return false;
}
}
My calc.cpp:
#include "stack.h"
#include <iostream>
#include <fstream>
using namespace std;
using namespace standard;
void main()
{
Stack myStack;
ifstream input;
input.open("tests.txt");
if (input.fail())
{
cerr << "Could not open input file." << endl;
exit(0);
}
char i;
input >> i;
cout << i;
myStack.push(i); // This is where things go wrong.
cin.get();
}
Thanks for any help!
It looks like you are not initializing used, you have something that may look like a constructor here but it is not:
void stack() {used=0;};
this is what it should look like:
Stack() { used=0;};
So without a constructor used is going to be some indeterminate value and will probably end up with you attempting to access data way out of bounds. Also main should always return int.
void stack() {used=0;};
should this be capitalised? & remove the void!
Stack myStack;
should this be
Stack myStack = new Stack();
if you don't initialise it, the variable myStack will be a "null pointer".
I think you wrote this function wrong:
void stack() {used=0;};
//^^extra ; here
should be
Stack() {used = 0;}
//^^Note that constructor has no return type
You have never really used the stack member function which return void. This results in the fact that used was never initialized. You probably mean the constructor of Stack. Meanwhile, you should use constructor initialization list:
Stack(): used(0) {}

I get an 'identifier not found' error

This is my first attempt to create a basic list (i need this at school) and i get a strange error.
This is the script:
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
using namespace System;
using namespace System::Threading;
struct nod
{
int info;
nod *leg;
};
int n, info;
nod *v;
void main()
{
....
addToList(v, info); //I get the error here
showList(v); //and here
}
void addToList(nod*& v, int info)
{
nod *c = new nod;
c->info=info;
c->leg=v;
v=c;
}
void showList(nod* v)
{
nod *c = v;
while(c)
{
cout<<c->info<<" ";
c=c->leg;
}
}
The exact error is:
error C3861: 'addToList': identifier not found
I dont know why I get this... sorry if it is a stupid question but i am very new at this. Thanks for understanding.
you need to put a forward declaration to use a method before it's implementation. Put this before main :
void addToList(nod*& v, int info);
In C/C++ a method should be used only after it's declaration. To allow recursive call between different methods you can use forward declarations in order to allow the use of a function/method that will be forward implemented.
Identifiers must be declared before they are used. Move your declaration and definition of addToList earlier in the text file.
Thus:
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
using namespace System;
using namespace System::Threading;
struct nod
{
int info;
nod *leg;
};
int n, info;
nod *v;
void addToList(nod*& v, int info)
{
nod *c = new nod;
c->info=info;
c->leg=v;
v=c;
}
void showList(nod* v)
{
nod *c = v;
while(c)
{
cout<<c->info<<" ";
c=c->leg;
}
}
void main()
{
....
addToList(v, info); //No more error here
showList(v); //and here
}
Try declaring addToList above main:
void addToList(nod*& v, int info);
Similarly for showList. The compiler needs to see a declaration of the function before it can use it.
Try placing the declarations of showList() and addToList() before main().