Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Let me first start by saying I do not have access to debuggers and I'm using Nano as my editor
Currently, with my calculator, I am beating my head against a brick wall(segmentation fault). I've tried going through my pointers to discover what my problem is, but my lack of experience/knowledge has only gotten me so far. Let me explain what works so far in my program. Currently, I am able to store hexadecimal numbers in a linked list and add them together. The problem comes from my multiplication method.Somehow leftNode is becoming NULL midway through the multiplication method throwing a segmentation fault. I'm wondering at what point does leftNode become NULL?
Multiplication Method:
LList Calculator::multiply(LList& left, LList& right) {
LList prodSum;
listnode *leftNode = (left.next());
int zeros = 0;
for(;;) {
if(leftNode == NULL) break;
int lval = leftNode->data;
LList curList;
for(int i = 0; i < zeros; i++) {
curList.insertTail(0);
}
right.reset();
listnode *rightNode = (right.next());
int carry = 0;
while(rightNode != NULL) {
int rval = rightNode->data;
int product = lval * rval + carry;
carry = product / 16;
product %= 16;
curList.insertTail(product);
rightNode = (right.next());
}
while(carry) {
curList.insertTail(carry % 16);
carry /= 16;
}
prodSum = *add(prodSum, curList);
leftNode = (left.next()); // eventually causes a segmentation fault
leftNode->data << endl;
++zeros;
}
return prodSum;
}
Classes related to multiplication:
class listnode {
public:
element data;
listnode * next;
};
class LList {
private:
listnode * head;
listnode * tail;
listnode * view;
public:
LList();
~LList();
void read();
listnode* next();
void reset();
void print();
void insertTail(element val);
void clean();
element deleteHead();
};
class Calculator {
public:
Calculator();
//inline LList* add(LList& left, LList& right); works
inline LList multiply(LList& left, LList& right);
};
Calculator::Calculator() {
};
Other methods related to traversing nodes:
listnode* LList::next() {
listnode* temp = view;
if(temp != NULL)
view = view->next;
if(view == NULL) {
}
return temp;
};
void LList::reset() {
view = head;
}
LList::LList(){
head = NULL;
view = NULL;
};
void LList::insertTail(element val) {
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = NULL;
if(head == NULL) {
head = temp;
view = head;
}
else
tail -> next = temp;
tail = temp;
};
void LList::clean() {
while(head != NULL)
deleteHead();
};
element LList::deleteHead() {
listnode * temp;
temp = head;
head = head -> next;
delete temp;
return temp -> data;
};
LList::~LList(){
delete head;
};
it's me again.
One exception occurs after the line you marked: // eventually causes a segmentation fault, there seems to be a partially-formed line for sending leftNode->data to cout, but on the final iteration through left's nodes, leftNode = (left.next()); will set leftNode to NULL, so a dereference here might be causing the fault.
One other problem is that no copy constructor or assignment operator is defined for LList, so this line: prodSum = *add(prodSum, curList); will give prodSum a set of list nodes that will be deleted right after.
However, LList's destructor only seems to delete the head node, not the whole list, so there's a grab-bag of invalid and valid going on.
Also, multiply returns prodSum, so the lack of a copy constructor will make something similar happen.
I'm including a version of your code that seems to work. I had to make my own add function, just because I don't see it here.
I made the destructor delete all of the LList's nodes.
I marked the default copy constructor and assignment operator =delete because the default implementations do the wrong thing.
In order to pass LList objects around by value, I added a move constructor and a move assignment operator. These pass allocated nodes from one object to another, and only one object is allowed to keep one set of nodes, so you don't have to worry about double-destruction.
#include <iostream>
#include <string>
typedef int element;
class listnode {
public:
element data;
listnode * next;
};
class LList {
listnode *head, *tail, *view;
public:
LList() { head = view = tail = NULL; }
LList(LList&& src) : head(src.head), tail(src.tail), view(src.view) { src.head = src.tail = src.view = nullptr; }
LList(const LList&) = delete;
~LList() { clean(); }
LList& operator = (LList&& src) {
clean();
/* OK here */
head = src.head;
tail = src.tail;
view = src.view;
src.head = src.tail = src.view = nullptr;
return *this;
}
LList& operator = (const LList&) = delete;
listnode* next() {
listnode* temp = view;
if(temp) view = view->next;
return temp;
}
void reset() { view = head; }
void print();
void insertTail(element val) {
listnode* temp = new listnode;
temp->data = val;
temp->next = NULL;
if(!head) { view = head = temp; }
else { tail->next = temp; }
tail = temp;
}
void clean() { while(head) deleteHead(); }
element deleteHead() {
listnode* temp = head;
head = head->next;
const element data = temp->data;
delete temp;
return data;
}
};
LList add(LList& left, LList& right) {
LList sum;
int carry = 0;
left.reset();
right.reset();
for(;;) {
const listnode* leftNode = left.next();
const listnode* rightNode = right.next();
if(!leftNode && !rightNode) break;
if(leftNode) carry += leftNode->data;
if(rightNode) carry += rightNode->data;
sum.insertTail(carry % 16);
carry /= 16;
}
if(carry) sum.insertTail(carry);
return sum;
}
LList multiply(LList& left, LList& right) {
LList prodSum;
listnode *leftNode = left.next();
int zeros = 0;
for(;;) {
if(!leftNode) break;
int lval = leftNode->data;
LList curList;
for(int i = 0; i < zeros; i++) {
curList.insertTail(0);
}
right.reset();
listnode *rightNode = right.next();
int carry = 0;
while(rightNode) {
int rval = rightNode->data;
int product = lval * rval + carry;
carry = product / 16;
product %= 16;
curList.insertTail(product);
rightNode = right.next();
}
while(carry) {
curList.insertTail(carry % 16);
carry /= 16;
}
prodSum = add(prodSum, curList);
leftNode = left.next(); // eventually causes a segmentation fault
//std::cout << leftNode->data << std::endl;
++zeros;
}
return prodSum;
}
LList string_to_list(std::string hex_string) {
LList list;
for(size_t i=hex_string.length()-1; i+1; --i) {
char c = hex_string[i] | 0x20;
if (c >= '0' && c <= '9') list.insertTail(c - '0');
else if(c >= 'a' && c <= 'f') list.insertTail(c - 'a' + 10);
}
return list;
}
std::string list_to_string(LList& list) {
std::string hex_string;
list.reset();
for(;;) {
listnode* node = list.next();
if(!node) return hex_string;
static const char digits[] = "0123456789abcdef";
hex_string = digits[node->data] + hex_string;
}
}
int main() {
//LList list = string_to_list("1234aBcd");
//std::string s = list_to_string(list);
//std::cout << s << '\n';
LList left = string_to_list("111");
LList right = string_to_list("333");
LList prod = multiply(left, right);
std::cout << list_to_string(prod) << '\n';
}
Related
I am implementing a linked list with a merge sort function for a class project. My program compiles, but when I try to run it I get segmentation fault(core dumped). I debugged my program using GDB, and found that the segfault happens with the pointer frontRef and backRef in my listSplit() function (line 98 in the code below).
Can someone please help me? For the life of me I can't figure out why I am getting a segfault. I would greatly appreciate help with this.
#include "orderedList.h"
orderedList::orderedList() {
listLength = 0;
traversalCount = 0;
head = nullptr;
tail = nullptr;
}
void orderedList::add(int n) {
listLength++;
struct node* point = new node;
point->value = n;
point->next = nullptr;
if (head == nullptr) {
head = point;
tail = point;
}
else {
point->next = head;
head = point;
}
}
void orderedList::merge(struct node** headRef) {
struct node *listHead = *headRef;
struct node *a;
struct node *b;
if ((listHead == nullptr) || (listHead->next == nullptr)) {
return;
}
listSplit(listHead, &a, &b);
merge(&a);
merge(&b);
*headRef = sortedMerge(a, b);
}
orderedList::node* orderedList::sortedMerge(struct node* a, struct node *b)
{
struct node* result = nullptr;
if (a == nullptr) {
return (b);
}
if (b == nullptr) {
return (a);
}
if (a->value <= b->value) {
result = a;
result->next = sortedMerge(a->next, b);
}
else {
result = b;
result->next = sortedMerge(a, b->next);
}
return (result);
}
void orderedList::print() {
struct node* temp = head;
while (temp != nullptr) {
std::cout << temp->value << " ";
temp = temp->next;
}
delete(temp);
}
int orderedList::search(int key) {
int traversals = 1;
struct node* current = head;
struct node* previous = nullptr;
while (current != nullptr) {
if (current->value == key) {
if (previous != nullptr) {
previous->next = current->next;
current->next = head;
head = current;
return traversals;
}
}
previous = current;
current = current->next;
traversals ++;
}
return 1;
}
void orderedList::listSplit(struct node* source, struct node** frontRef, struct node** backRef) { // <--- Line 98
struct node* current = source;
int hopCount = ((listLength - 1) / 2);
for (int i = 0; i < hopCount; i++) {
current = current->next;
}
*frontRef = source;
*backRef = current->next;
current->next = nullptr;
}
You made *backRef point to current->next and then you let current->next = nullptr. This makes *backRef pointing to a nullptr. Did you later try to do something with the returned backRef, aka a node variable in your caller code?
I had to implement a Linked HashTable for a project. Now I have to come up with an excercise and a solution to it using my hashtable. Everything works just fine, except I get random Segfault errors.
By Random I mean: It is the same line of code that causes it, but always at different times, calls.
I tested my code in Atom, Codeblocks and in Visual Studio Code. Both Atom and CB threw SegFault error, but VS Code ran it just fine without a problem.
NOTE: THIS IS NOT THE FULL/REAL CODE. It's part of a header file that is included in the main.cpp file which is then compiled and ran.
The Code:
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
//List:
template<class T>
struct Node
{
string data;
Node *next;
};
class List
{
private:
Node *head, *tail;
int length;
friend class HashTable;
public:
List();
List(const List &L);
//~List() {delete this;};
List& operator =(List L);
int find(string);
void insert(string value);
void remove_head();
void remove_poz(int);
void remove_tail();
void clear();
void display();
};
List::List()
{
head = NULL;
tail = NULL;
length = 0;
}
template<>
string List<string>::findByIndex(int ind)
{
int i = 0;
Node<string>* temp = new Node<string>;
temp = head;
while (temp != NULL)
{
i++;
if (i == ind) return temp->data;
temp = temp->next;
}
delete temp;
return "-1";
}
template<class T>
void List<T>::remove_head()
{
Node<T>* temp = new Node<T>;
temp = head;
head = head->next;
delete temp;
length--;
}
template<class T>
void List<T>::remove_pos(int pos)
{
int i;
Node<T>* curr = new Node<T>;
Node<T>* prev = new Node<T>;
curr = head;
for (i = 1; i < pos; ++i)
{
prev = curr;
curr = curr->next;
}
if (curr)
{
prev->next = curr->next;
length--;
}
else cout << "Error" << endl;
}
template<class T>
void List<T>::remove_tail()
{
Node<T>* curr = new Node<T>;
Node<T>* prev = new Node<T>;
curr = head;
while (curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
tail = prev;
prev->next = NULL;
delete curr;
length--;
}
//HashTable:
class HashTable
{
private:
List *table;
float load, stored;
int slots;
friend class List;
public:
HashTable();
HashTable(int);
~HashTable();
int hashFunc(string key);
int findTable(string);
int findList(string);
HashTable& operator =(const HashTable&);
void resize(); //I need this one
void insert(string);
void remove(string);
void clear(int);
void clear();
void display();
};
HashTable::HashTable()
{
stored = 0;
load = 0.00;
slots = 15;
table = new List[slots];
}
int HashTable::hashFunc(string key)
{
int g, h = 0;
unsigned int i;
for (i = 0; i < key.size(); ++i)
{
h = (h << 4) + (int)(key[i]);
g = h & 0xF0000000L;
if (g != 0)
{
h = h ^ (g >> 24);
}
h = h & ~g;
}
return h % slots;
}
template<class T>
void HashTable<T>::remove(T value)
{
int ind = hashFunc(value);
int findInd = table[ind].findByValue(value);
if (findInd == 0)
table[ind].remove_head();
else if (findInd < table[ind].length)
table[ind].remove_pos(findInd);
else table[ind].remove_tail();
if (table[ind].isEmpty()) occupied--;
stored--;
load = stored / slots;
}
The function that would cause the segfault:
(This would be called over and over again in a loop till I don't have more elements in my table)
string reakcio(HashTable<string>& HT, int tarolok)
{
const int anyagszam = rand() % 4 + 2; //Min 2, Max 5 anyag hasznalodik
int i = 0, j;
string anyagok[5];
string eredmeny;
for(j = 0; j < tarolok && i < anyagszam; ++j) //elemek kivetele
{
while(!HT.table[j].isEmpty())
{
anyagok[i++] = HT.table[j].findByIndex(1); //This line right here is the culprit :(
HT.remove(anyagok[i-1]);
}
}
const int siker = rand() % 4 + 0; //75% esely a sikerre
if (siker)
{
eredmeny = anyagok[0];
for(i = 1; i < anyagszam; ++i)
eredmeny += " + " + anyagok[i];
}
else
eredmeny = "Sikertelen reakcio";
return eredmeny;
}
(Note: only the functions that might be needed are shown here)
Every element of my hashtable, or of my lists is a 10 character long random string value.
srand(time(NULL)) is used before the function call in main.cpp
Any help or advice would be much appreciated, as I'm stuck at this and I really need to move on to the next portion of my exercise, but I can't without this.
The main.cpp file:
#include <iostream>
//#include "LinkedHash.h"
#include "functions.cpp"
int main()
{
HashTable<string> Anyagok;
int tarolok;
tarol(Anyagok); //Stores the data from file, no problem here, functions.cpp
tarolok = Anyagok.getSlots();
srand(time(NULL));
int i = 1;
while (Anyagok.getStored() > 5 )
cout<<reakcio(Anyagok, tarolok)<<" "<<i++<<endl;
return 0;
}
The LinkedHash.h contains the hashtable and the list, the functions.cpp contains the problematic function.
EDIT:
By suggestion I changed out the
Node<string>* temp = new Node<string>;
temp = head;
part to
Node<string>* temp = head;
Also removed the delete line.
But my problem is still the same :/
Everything works just fine, except I get random Segfault errors
Then nothing works at all.
A first review show little care to the cornercases in the list class. You need to define a correct behavior for
operation on empty lists
operation on first and last element
key not found during search
Notable errors found:
remove_head, remove_tail will segfault on empty list. head is NULL. head->next is invalid memory access. Similar errors are all over the implementation.
HashTable<T>::remove(T value) will always remove something. Even if the value argument is not in the hashtable. This is deeply flawed
findByIndex returning "-1" make no sense. "-1" is a valid input.
Node<T>* temp = new Node<T>;temp = head;. You just leaked memory. You need a pointer to manipulate node addresses. You should not instantiate Nodes to get a pointer. This is not an issue (ie not noticeable) for a small projet, but unacceptable for a real implementation.
This is my code for an implementation of a doubly linked list that inherits previous code from a single linked list, I am currently having trouble with a linker error and surfed the web for the past hour looking for an answer to my problem and found nothing so far to help me. This is my last resor can anyone help?
Specifically the error i get when i try to use g++ to link my .o files is:
DoublyList.o:DoublyList.cpp:(.text+0xf): undefined reference to
`LinkedList::LinkedList()'
collect2.exe: error: ld returned 1 exit status
I have found very similar questions asked but none of the answers helped me or at least I do not know how to implement them in my code specifically, any help will be apprectiated.
My LinkedList class
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
using namespace std;
struct node
{
float value;
node *next;
};
class LinkedList
{
private:
node *first;
public:
LinkedList();
virtual void insert(float val);
virtual void del(float val);
virtual void read();
virtual int search(float val);
};
#endif
My LinkedList class definition
#include <iostream>
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList()
{
this->first = NULL;
}
void LinkedList::insert(float val)
{
if(this->first==NULL or this->first->value >= val)
{
node* a_node = new node();
a_node->value = val;
this->first = a_node;
return;
}
node* n = new node();
n = this->first;
node* new_node = new node();
new_node->value = val;
while(n->next != NULL and n->next->value < new_node->value)
{
n = n->next;
}
new_node->next = n->next;
n->next = new_node;
}
void LinkedList::del(float val)
{
node* n = this->first;
node* prev = new node();
prev = n;//in case if it is the first value
int i = this->search(val);
if(this->first->value == val)
{
this->first = this->first->next;
return;
}
if(i != -1)
{
for(int j = 0; j < i; j++)
{
prev = n;
n = n->next;
}
}
//one last check
if(n->value == val)
{
prev->next = n->next;
}
}
void LinkedList::read()
{
node* n = this->first;
int i = 1;
while(n != NULL)
{
cout << i << ". " << n->value << endl;
n = n->next;
i++;
}
}
int LinkedList::search(float val)
{
int i = 0;
node* n = this->first;
while(n != NULL)
{
if(n->value == val)
return i;
else
{
n = n->next;
i++;
}
}
return -1;
}
My doublylist class
#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H
#include "LinkedList.h"
class DoublyList: public LinkedList
{
public:
struct node
{
float value;
node * next;
node * prev;
};
node * first;
DoublyList();
void insert(float val);
void del(float val);
void read();
int search(float val);
};
#endif
My Doubly List definiton
#include <cstddef>
#include "DoublyList.h"
#include "LinkedList.h"
using namespace std;
//constructor
DoublyList::DoublyList()
{
first = NULL;
}
//Insert a node into the correct position in the doubly linked list
void DoublyList::insert(float val)
{
//if linked list is empty or val <= the first node
if(this->first == NULL or this->first->value >= val)
{
node * a_node = new node();
a_node->value = val;//set node's value
//begin replacing and assigning pointers
a_node->next = this->first;
a_node->prev = NULL;
this->first = a_node;
return;
}
node * n = new node();
n = this->first;
node * new_node = new node();
new_node->value = val;
node * prev_node = new node();
while(n->next != NULL and n->next->value < new_node->value)
{
prev_node = n;
n = n->next;
}
prev_node->next = new_node;
new_node->next = n->next;
new_node->prev = prev_node;
n->next = new_node;
}
void DoublyList::del(float val)
{
node * n = this->first;
int i = this->search(val);
//if first node
if(this->first->value == val)
{
this->first = this->first->next;
this->first->prev = NULL;
return;
}
//if value found
if(i != -1)
{
for(int j = 0; j < i; j++)
{
n = n->next;
}
//if a middle node
if(n->value == val and n->next != NULL)
{
n->prev->next = n->next;
return;
}
//if last node
if(n->prev != NULL)
{
n->prev->next = n->next;
}
}
return;//value not found so return
}
void DoublyList::read() { }
int DoublyList::search(float val) { }
Edit: Forgot to mention this error specifically happens aruond line 8 of DoublyList.cpp, this was from previous trials to link the .o files.
The command I used to call the linker is
g++ -g main2.cpp DoublyList.o
Where main2.cpp is the code that contains my main function to test the code.
Thanks to xskxzr the solution was to also link LinkedList.o along with all the rest of the .o files. If anyone ever has the same problem this is the answer.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I know the segmentation fault is occurring in this while loop: (while(temp != NULL){temp = temp->next;}), but I have no idea why.
#include<iostream>
using namespace std;
class zDepthList {
typedef struct node {
int data;
node* next;
node* prev;
} Node;
public:
zDepthList() {
head = NULL;
}
zDepthList(int array[], int length) {
Node *temp, *ptr;
int i = 0;
while(i != length - 1) {
temp = head;
ptr = new Node;
ptr->data = array[i];
i++;
ptr->next = NULL;
if(head == NULL) {
head = ptr;
ptr->prev = NULL;
}
else {
while(temp != NULL) {
temp = temp->next;
}
}
temp->next = ptr;
ptr->prev = temp;
}
}
void out(const char order) {
cout << head->data << endl;
return;
}
private:
Node *head;
};
For starters you have to initialize the head to NULL.
And after this while loop
else {
while(temp != NULL) {
temp = temp->next;
}
}
temp->next = ptr;
ptr->prev = temp;
the pointer temp is equal to NULL because it is the condition to interrupt the loop. Thus this statement
temp->next = ptr;
results in undefined behavior.
If you have a double-linked list it is natural to introduce also data member tail that it could be easy to append new nodes.
So you should include
class zDepthList {
//...
private:
Node *head, *tail;
};
In this case the constructors can look the following way
zDepthList() : head( nullptr ), tail( nullptr )
{
}
zDepthList( const int a[], size_t n ) : head( nullptr ), tail( nullptr )
{
for ( size_t i = 0; i < n; i++ )
{
Node *tmp = new Node { a[i], nullptr, tail };
tail == nullptr ? head = tmp : tail->next = tmp;
tail = tmp;
}
}
Here is a demonstrative program
#include <iostream>
class zDepthList {
typedef struct node {
int data;
node* next;
node* prev;
} Node;
public:
zDepthList() : head(nullptr), tail(nullptr)
{
}
zDepthList(const int a[], size_t n) : head(nullptr), tail(nullptr)
{
for (size_t i = 0; i < n; i++)
{
Node *tmp = new Node{ a[i], nullptr, tail };
tail == nullptr ? head = tmp : tail->next = tmp;
tail = tmp;
}
}
std::ostream & out( std::ostream &os = std::cout ) const
{
for (Node *current = head; current; current = current->next)
{
os << current->data << ' ';
}
return os;
}
private:
Node *head, *tail;
};
int main()
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
zDepthList l(a, sizeof(a) / sizeof(*a));
l.out() << std::endl;
}
The program output is
0 1 2 3 4 5 6 7 8 9
You never set head but you access it. This means it is uninitialized and this is an UB.
You have 2 ctors and you initialize head only then, when it is called without any parameter.
Here is the header file code, stack.h:
#include <iostream>
using namespace std;
//template <class T> struct stackNode;
//template <class T>
struct stackNode {
int item;
stackNode *Next;
stackNode *Prev;
//stackNode *Temp;
};
class stackClass {
public:
stackClass();
stackClass(const stackClass &right);
~stackClass();
stackClass &operator=(const stackClass &right);
int counter;
int StackIsEmpty(void);
//stackNode *origptr;
void push( int item, bool &success);
int pop(void);
int GetStackTop(void);
protected:
stackNode *GetStackTopPtr(void);
private:
stackNode *Top;
stackNode *Current;
//stackNode *origptr;
//stackNode<T> *Next;
};
Here is the implementation file, stack.cpp:
#include "stack.h"
//constructor
stackClass::stackClass(){
Top = NULL;
int counter = 0;
}
//copy constructor
stackClass::stackClass(const stackClass &right){
if (right.Top == NULL){
Top = NULL;
counter = 0;
}
else {
stackNode * origptr = right.GetStackTopPtr;
stackNode * currptr;
Top = new stackNode;
currptr = Top;
while(origptr->Next != NULL){
currptr->item = origptr->item;
origptr = origptr->Next;
currptr->Next = new stackNode;
currptr->Next->item = origptr->item;
currptr = currptr-> Next;
}
currptr->Next = NULL;
}
}
//Destructor
stackClass::~stackClass(){
while (Top != NULL){
pop();
}
}
//push
void stackClass::push(int item, bool &success){
success = false;
if (Top == NULL){
Top = new stackNode;
Top -> item = item;
Top -> Next = NULL;
Top -> Prev = NULL;
Current = Top;
counter++;
success = true;
}
else {
stackNode * Temp = new stackNode;
Current -> Next = Temp;
Temp -> Prev = Current;
Temp -> Next = NULL;
Current = Temp;
Current -> item = item;
counter++;
success = true;
}
}
int stackClass::pop(void){
if (Top == NULL){
cout<<"Stack is empty"<<endl;
}
else if (counter == 1){
Top = NULL;
delete Current;
counter--;
}
else if(counter > 1){
Current -> Prev -> Next = NULL;
Current = Current -> Prev;
stackNode * Temp = Current -> Next;
delete Temp;
counter--;
}
}
int stackClass::StackIsEmpty(void){
if (counter == 0)
return 1;
else
return 0;
}
int stackClass::GetStackTop(void){
int item = Top->item ;
return item;
}
stackNode *stackClass::GetStackTopPtr(void){
return Top;
}
and here is the issue,
stack.cpp: In copy constructor ‘stackClass::stackClass(const stackClass&)’:
stack.cpp:19:31: error: cannot convert ‘stackClass::GetStackTopPtr’ from type ‘stackNode* (stackClass::)()’ to type ‘stackNode*’
I have no idea why the compiler complains, it appears that in the line in question, in stack.cpp, line 19, right.GetStackTopPrt would return a stackNode * type, which should assign nicely to the stackNode* on the left side.
Please help if you know what the issue is. Thanks!
You're trying to set
stackNode * origptr = right.GetStackTopPtr;
where the left hand side is a stackNode pointer, and the right hand side is the function GetStackTopPtr. I think you meant to write:
stackNode * origptr = right.GetStackTopPtr(); // Note the parentheses!
Remember that the compiler is very good at figuring out types, and is almost always right about these things. Don't assume the compiler is wrong, look for a mistake that you made first!