Function not declared in scope
Apr 29, 2018 at 2:34am Apr 29, 2018 at 2:34am UTC
im getting an error saying prepend was not declared in the scope.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <iostream>
#include <cstring> // for getline
#include <cstdlib> // for the system command
#include <cctype> // For the letter checking functions, e.g. toupper( )
#include <fstream> // For file input and output
#include <ctime>
using namespace std;
class Node{
public :
int data;
Node *pNext;
void prepend(Node *pHead1, int numbers){
Node *pTemp;
pTemp=new Node;
pTemp->data=numbers;
pTemp->pNext=pHead1;
pHead1=pTemp;
}
void display(Node *pHead1){
Node *pTemp=new Node;
pTemp=pHead1;
while (pTemp!= NULL){
if (pTemp->pNext==NULL){
cout<< pTemp -> data;
pTemp=pTemp->pNext;
}
else {
cout<< pTemp -> data <<"->" ;
pTemp=pTemp->pNext;
}
}
}
};
int main()
{
Node *pHead=NULL;
int x=2;
prepend(pHead,x);
display(pHead);
return 0;
}
Apr 29, 2018 at 2:49am Apr 29, 2018 at 2:49am UTC
You could maybe do it something like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
using namespace std;
class Node{
public :
int data;
Node *next;
Node(int num, Node *nxt=nullptr ) : data(num), next(nxt) {}
static void prepend(Node*& head, int numbers){
head = new Node(numbers, head);
}
void display() {
cout << data;
if (next) {
cout << "->" ;
next->display();
}
}
};
int main() {
Node *head=nullptr ; // or 0 (not NULL) if pre-C++11
for (int i = 0; i < 7; i++)
Node::prepend(head, i);
head->display();
cout << '\n' ;
return 0;
}
But usually in C++ you take advantage of classes and make a List object to hold the head pointer (and maybe the tail pointer for fast insertion at the end, and maybe the size).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <random>
using namespace std;
class Rnd {
random_device rd;
default_random_engine eng;
public :
Rnd() : eng(rd()) {}
int operator ()(int high) { return eng() % high; }
};
Rnd rnd;
class List {
struct Node {
int m_value;
Node *m_next;
Node(int value, Node *next) : m_value(value), m_next(next) {}
};
Node *m_head;
public :
List() : m_head(nullptr ) {}
~List() {
for (Node *nd = m_head, *next = nullptr ; nd; nd = next) {
next = nd->m_next;
delete nd;
}
}
void prepend(int value) {
m_head = new Node(value, m_head);
}
void print() {
for (Node *nd = m_head; nd; nd = nd->m_next) {
cout << nd->m_value;
if (nd->m_next) cout << "->" ;
}
cout << '\n' ;
}
};
int main() {
List list;
for (int i = 0; i < 21; i++)
list.prepend(rnd(100));
list.print();
}
Last edited on Apr 29, 2018 at 3:20am Apr 29, 2018 at 3:20am UTC
Apr 29, 2018 at 3:33am Apr 29, 2018 at 3:33am UTC
i dont think its that complicated
Apr 30, 2018 at 11:29am Apr 30, 2018 at 11:29am UTC
im getting an error saying prepend was not declared in the scope.
Have a look at the line your compiler points out: prepend() is declared inside ‘Node’, so you can’t access it the way you do. You need an object to call methods on (or follow tpb’s advice and make the method static, but it can have its downside).
Topic archived. No new replies allowed.