I was working this code previously but have a new issue with it the link to the old one is Bloodshed Dev-C++ compiler errors *Binary Trees.
I have one error that states in function 'int main()' no matching function for call to 'binaryTreeType::insert(int&)' candidates are: void binaryTreeType::insert () [with elemType = int] not really sure what this is. The code is posted below any help is appreciated thanks in advance.
#include <iostream>
using namespace std;
template <class elemType>
struct nodeType
{
int data;
nodeType *lLink;
nodeType *rLink;
};
template <class elemType>
class binaryTreeType //main class
{
public:
binaryTreeType(); //constructor
~binaryTreeType(); //destructor
void swapSubtreeNodes(); //declares swapSubtreeNodes
void swapSubtreeNodes(nodeType<elemType>*);
void insert();
void printTree();
private:
nodeType<elemType>*root; //declares root pointer
nodeType<elemType> *temp; //declares root pointer
};
template <class elemType>
void binaryTreeType<elemType>::swapSubtreeNodes()
{
swapSubtreeNodes(root); //displays new root
}
template <class elemType>
void binaryTreeType<elemType>::swapSubtreeNodes(nodeType<elemType> *p)
{
root = temp;
nodeType<elemType> *root; //pointer for root
nodeType<elemType> *temp; //pointer for temp
if (p == NULL) //checks for empty pointer
{
return;
}
else
{
swapSubtreeNodes(p->lLink); //do the subtrees
swapSubtreeNodes(p->rLink);
temp = p->lLink; //swap the pointers
p->lLink = p->rLink;
p->rLink = temp;
}
root = temp; //root set equal to temp
}
int main()
{
binaryTreeType<int> (tree);
int num;
cout << "This is how we swap'em" << endl;
cout << "Insert number (press enter after each one entered)." << endl;
cout << "Enter -999 to complete" << endl;
binaryTreeType<int> (insert);
cin >> num;
while (num != -999)
{
tree.insert(num);
cin >> num;
}
cout << "The unswapped binary tree looks like this: " << endl;
tree.printTree();
cout << endl;
cout << "The swapped binary tree looks like this: " << endl;
tree.swapSubtreeNodes();
tree.printTree();
cout << endl;
}
The template member function binaryTreeType<elemType>::insert() takes no arguments. The code tries to call it with an argument of type int.
Your insert method declaration doesn't take any parameters.
Related
#include <iostream>
#include <string.h>
using namespace std;
class MovieList {
private:
struct MovieNode {
string title;
struct MovieNode *next;
};
MovieNode *head;
public:
void appendNode(string var);
void displayList();
};
void MovieList::appendNode(string var) {
MovieNode *newNode, *nodePtr;
newNode = new MovieNode;
newNode->title = var;
newNode->next = NULL;
if (!head) {
head = newNode;
}
else {
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
cout << endl << "Input has been successfull!" << endl;
}
void MovieList::displayList() {
MovieNode *nodePtr;
if (head == NULL) {
cout << "The list is empty!" << endl;
}
else {
cout << "The nodes in the List are... " << endl;
nodePtr = head;
while (nodePtr) {
cout << nodePtr->title << endl;
nodePtr = nodePtr->next;
}
}
}
int main() {
MovieList list;
string var;
cout << " << Enter Movie >> " << endl << endl;
cout << "Enter a movie: ";
getline(cin, var);
list.appendNode(var);
list.displayList();
}
My question is why does the string variable won't show in the display void. The code is working at first when I try inputting a string but then it exits the program without displaying what I input. Is something wrong with how I put the string or is it in implementing the string into the node? please help, I'm new in linked list.
One issue is that MovieList.head is not initialized when you create a MovieList object.
Thus when MovieList.append() is called, this line:
if (!head)
causes the behavior of the program to be erratic due to the head pointer being whatever value it is, which you have no idea what the value is. It may be nullptr, thus the program works, but it may be any other values.
The bottom line is that you should initialize your member variables when you create an object. You can either create a default constructor that initialized head, or you can initialize head directly inline at the declaration point:
class MovieList
{
private:
struct MovieNode
{
string title;
struct MovieNode *next;
};
MovieNode *head;
public:
MovieList() : head(nullptr) {} // Using constructor
void appendNode(string var);
void displayList();
};
or:
class MovieList
{
private:
struct MovieNode
{
string title;
struct MovieNode *next;
};
MovieNode *head = nullptr; // Initialized here
public:
void appendNode(string var);
void displayList();
};
I write a LinkedList class and a test function to test my class. However, I met some problems while compiling and running my program. There are three files: test.cpp LinkedList.cpp and LinkedList.h.
test.cpp:
#include "LinkedList.cpp"
#include <string>
#include <iostream>
using namespace std;
// Loading Array into Linked List for testing
template <class T, size_t n>
void fillArrayIn(LinkedList<T> &list, T (&arr)[n])
{
unsigned int length = sizeof(arr) / sizeof(*arr);
for (unsigned int i = 0; i < length; i++)
{
list.append(arr[i]);
}
}
int main()
{
// Integer LinkedList
cout << "=====================================" << endl;
cout << "Test for Integer LinkedList" << endl
<< endl;
LinkedList<int> intList;
cout << "Is List Empty? " << (intList.isEmpty() ? "Yes" : "No") << endl
<< endl; // Test isEmpty()
int intArray[] = {1, 2, 5, 6, 9, 0, 1};
fillArrayIn(intList, intArray); // Test appending
cout << "Array Loaded" << endl;
cout << "Is List Empty? " << (intList.isEmpty() ? "Yes" : "No") << endl;
intList.printAll();
intList.insert(*(new int(100)), 3); // Test insertion
cout << endl
<< "Insert 100 to position 3" << endl;
intList.printAll();
cout << "Insert integer outside the List: "
<< (intList.insert(*(new int(100)), 100) ? "Success" : "Fail") << endl
<< endl; // Test illegal insertion
cout << "Find the position of integre 9: " << intList.positionOf(9) << endl; // Test get the position of element in List
cout << "Find the position of not existed element: " << intList.positionOf(20) << endl
<< endl; // Test get the position of not existed element
cout << "The element at position 0 is " << *(intList.get(0)) << endl; // Test get element
cout << "The element at position 7 is " << *(intList.get(7)) << endl;
cout << "The element at position 8 is " << intList.get(8) << endl
<< endl;
cout << "Deleting 100 from list: "
<< (intList.remove(*(new int(100))) ? "Sucess" : "Fail") << endl;
intList.printAll();
cout << "Deleting not existed element from list: "
<< (intList.remove(*(new int(20))) ? "Sucess" : "Fail") << endl;
intList.printAll();
}
LinkedList.cpp:
#include <string>
#include <iostream>
#include "LinkedList.h"
using namespace std;
template <class T>
LinkedList<T>::LinkedList()
{
head = new Node(NULL);
}
template <class T>
void LinkedList<T>::append(T &_data)
{
Node *current = head;
while (current->next)
{
current = current->next;
}
current->next = new Node(&_data);
// cout << _data <<" has been appended to the list."<<endl;
}
template <class T>
bool LinkedList<T>::insert(T &_data, const int position)
{
Node *current = head;
Node *previous = head;
for (int i = position + 1; i > 0; i--)
{
if (!current->next)
return false;
previous = current;
current = current->next;
}
previous->next = new Node(&_data);
previous->next->next = current;
return true;
}
template <class T>
T *LinkedList<T>::get(int position)
{
Node *current = head;
for (int i = position + 1; i > 0; i--)
{
if (!current->next)
return NULL;
current = current->next;
}
return current->data;
}
template <class T>
bool LinkedList<T>::isEmpty()
{
if (head->next)
return false;
return true;
}
template <class T>
bool LinkedList<T>::remove(const T &_data)
{
Node *current = head;
Node *previous = current;
while (current->next)
{
previous = current;
current = current->next;
if (*(current->data) == _data)
{
previous->next = current->next;
return true;
}
}
return false;
}
template <class T>
int LinkedList<T>::positionOf(const T &_data)
{
Node *current = head;
unsigned int position = -1;
while (current->next)
{
current = current->next;
position++;
if (*(current->data) == _data)
return position;
}
return -1;
}
template <class T>
void LinkedList<T>::printAll()
{
Node *current = head;
while (current->next)
{
current = current->next;
cout << *(current->data) << " ";
}
cout << endl;
}
template <class T>
class LinkedList<T>::Node
{
public:
Node(T *_data) : data(_data) {}
T *data;
Node *next;
};
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template <class T>
class LinkedList
{
public:
LinkedList();
void append(T &);
bool insert(T &, const int);
T *get(int);
bool isEmpty();
bool remove(const T &);
int positionOf(const T &);
void printAll();
private:
class Node;
Node *head;
};
#endif
When I compile my program with g++, it compiled successfully and give out the expected result:
=====================================
Test for Integer LinkedList
Is List Empty? Yes
Array Loaded
Is List Empty? No
1 2 5 6 9 0 1
Insert 100 to position 3
1 2 5 100 6 9 0 1
Insert integer outside the List: Fail
Find the position of integre 9: 5
Find the position of not existed element: -1
The element at position 0 is 1
The element at position 7 is 1
The element at position 8 is 0
Deleting 100 from list: Sucess
1 2 5 6 9 0 1
Deleting not existed element from list: Fail
1 2 5 6 9 0 1
When I try to compile my program with cl (VS2017), the compilation is also successful. However, when I run the program, it will be stuck at a certain point. Running result:
=====================================
Test for Integer LinkedList
Is List Empty? No
Later, I try to create a project in Visual Studio. Surprisingly, this time, it runs without any problem.
How can I make it correct to use cl to compile? Thank you if you could give me some hints! I am a new learner in C++.
B.T.W. The object file and executable generated by cl are more than 200KB, which is much more than the ones g++ generated which are around 14KB.
I suggest making the following changes to your code. I don't know whether that will change the behavior but it's worth a try.
Initialize head to nullptr in the constructor of List.
template <class T>
LinkedList<T>::LinkedList() : head(nullptr)
{
// Creating a Node with NULL does not make sense to me.
// head = new Node(NULL);
}
Initialize next to nullptr in the constructor of Node. Letting it be uninitialized can certainly lead to problems.
Node(T *_data) : data(_data), next(nullptr) {}
Your check for empty list uses the Node attribute next.
/* ... */
if (head->next)
/* ... */
After using the constructor with pointer parameter for Node, next is not explicitly initialised.
Node(T *_data) : data(_data) {}
These lines are all which are relevant for the first difference of output
Is List Empty? No
Is List Empty? Yes
I tend to be paranoid about initialising and would use the modified constructor with explicit initialisation:
Node(T *_data) : data(_data), next(NULL) {}
What I want is a rather simple binary search tree that via the template tag, allows for any numerical data to be used within it, but I'm having some rather obnoxious issues that I have no clue of how to get rid off, if anyone can help, it would be much appreciated. The error message that keeps popping up for me is "Invalid use of template-name 'BST' without an argument list" - and quite frankly I have no clue how to solve it. It occurs on line 31, 89, 105, 120, 130, 141 in the bst.cpp file:
Main.cpp
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
BST <int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}
bst.h
#ifndef BST_H
#define BST_H
template <class T>
class BST
{
struct node
{
T data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(T x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(T x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, T x);
void insert(T x);
void remove(T x);
void displayorder();
void search(T x);
};
#endif // BST_H
bst.cpp
#include <iostream>
#include "bst.h"
using namespace std;
template <class T>
void BST<T>::preorder(node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
template <class T>
void BST<T>::postorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
template <typename T>
BST::node* BST<T>::find(node* tree, T x) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
template <typename T>
BST<T>::BST()
{
root = NULL;
}
template <typename T>
BST<T>::~BST()
{
root = makeEmpty(root);
}
template <class T>
void BST<T>::insert(T x)
{
root = insert(x, root);
}
template <class T>
void BST<T>::remove(T x)
{
root = remove(x, root);
}
template <class T>
void BST<T>::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
template <class T>
void BST<T>::search(T x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
template <class T>
BST::node* BST<T>::insert(T x, node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
BST::node* BST::findMin(node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
BST::node* BST::findMax(node* tree) //AN ERROR OCCURS HERE
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
template <typename T>
BST::node* BST<T>::remove(T x, node* tree) //AN ERROR OCCURS HERE
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
template <class T>
void BST<T>::inorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
You need to qualify BST::node too, as BST<T>::node. Also, keep in mind that all the template code will need to live in bst.h because it will need to be instantiated by each source file that uses it (like main.cpp).
Add the
Add the BST::node in your function definitions. In addition include the .ipp (cpp file) as shown in line 27 in order to have the class methods in the header and the implementation in a .ipp file. You don't have to place all your template code in the header file if you don't wan't to.
Another tip is the fact that you must not pass the root node to your functions because anyone outside the class can have access to your tree root. Aim for something like the following code
1 #ifndef BST_H
2 #define BST_H
3
4 #include <memory>
5
6 template<typename T>
7 class node;
8
9 template <typename T>
10 class BST
11 {
12 public:
13 BST();
14 BST(std::initializer_list<T> init);
15 ~BST();
16
17 void make_empty();
18 bool is_empty() const noexcept;
19 bool is_full() const;
20 void insert(const T&);
21 void remove(const T&);
22 void print_tree() const noexcept;
23 private:
24 std::unique_ptr<node<T>> root;
25 };
26
27 #include "binary_search_tree.ipp"
28
29 #endif
What I want is a rather simple binary search tree that via the template tag, allows for any numerical data to be used within it, but I'm having some rather obnoxious issues that I have no clue of how to get rid off, if anyone can help, it would be much appreciated. The error message that keeps popping up for me is "Invalid use of template-name 'BST' without an argument list" - and quite frankly I have no clue how to solve it. It occurs on line 31, 89, 105, 120, 130, 141 in the bst.cpp file. Given that I'm not as proficient when it comes to binary search trees, I'd prefer as conclusive of an answer as possible (even going as far as to mention exactly where and what needs to be changed):
Main.cpp
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
BST <int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();
int number;
int Inputnumber;
while (true){
cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl << "3# Search" << endl << "4# Delete" << endl << endl << endl;
cin >> Inputnumber;
if (Inputnumber==1){
cout << endl << "Enter the number you want inserted: ";
cin >> number;
tree.insert(number);
cout << endl << endl << endl;
}
if (Inputnumber==2){
cout<<"Display Orders: " << endl;
tree.displayorder();
cout << endl << endl << endl;
}
if (Inputnumber==3){
cout<<"Enter the number you want to search for: ";
cin >> number;
tree.search(number);
cout << endl << endl << endl;
}
if (Inputnumber==4){
cout << "Enter the number you want to remove: ";
cin >> number;
tree.remove(number);
cout << endl << endl << endl;
}
}
}
BST.cpp
#include <iostream>
#include "bst.h"
using namespace std;
template <class T>
void BST<T>::preorder(node* tree)
{
if(tree == NULL){
return;
}
cout << tree->data << " ";
inorder(tree->left);
inorder(tree->right);
}
template <class T>
void BST<T>::postorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
inorder(tree->right);
cout << tree->data << " ";
}
template <typename T>
BST::node* BST<T>::find(node* tree, T x) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(x < tree->data)
return find(tree->left, x);
else if(x > tree->data)
return find(tree->right, x);
else
return tree;
}
template <typename T>
BST<T>::BST()
{
root = NULL;
}
template <typename T>
BST<T>::~BST()
{
root = makeEmpty(root);
}
template <class T>
void BST<T>::insert(T x)
{
root = insert(x, root);
}
template <class T>
void BST<T>::remove(T x)
{
root = remove(x, root);
}
template <class T>
void BST<T>::displayorder()
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
postorder(root);
cout << endl << endl;
}
template <class T>
void BST<T>::search(T x)
{
if(root = find(root, x)){
cout << endl << "Found!" << endl;
}
else{
cout << endl << "Not Found!" << endl;
}
}
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
{
makeEmpty(tree->left);
makeEmpty(tree->right);
delete tree;
}
return NULL;
}
template <class T>
BST::node* BST<T>::insert(T x, node* tree) //ERROR HERE
{
if(tree == NULL)
{
tree = new node;
tree->data = x;
tree->left = tree->right = NULL;
}
else if(x < tree->data)
tree->left = insert(x, tree->left);
else if(x >= tree->data)
tree->right = insert(x, tree->right);
return tree;
}
BST::node* BST::findMin(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(tree->left == NULL)
return tree;
else
return findMin(tree->left);
}
BST::node* BST::findMax(node* tree) //ERROR HERE
{
if(tree == NULL)
return NULL;
else if(tree->right == NULL)
return tree;
else
return findMax(tree->right);
}
template <typename T>
BST::node* BST<T>::remove(T x, node* tree) //ERROR HERE
{
node* temp;
if(tree == NULL)
return NULL;
else if(x < tree->data)
tree->left = remove(x, tree->left);
else if(x > tree->data)
tree->right = remove(x, tree->right);
else if(tree->left && tree->right)
{
temp = findMin(tree->right);
tree->data = temp->data;
tree->right = remove(tree->data, tree->right);
}
else
{
temp = tree;
if(tree->left == NULL)
tree = tree->right;
else if(tree->right == NULL)
tree = tree->left;
delete temp;
}
return tree;
}
template <class T>
void BST<T>::inorder(node* tree)
{
if(tree == NULL){
return;
}
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
BST.h
#ifndef BST_H
#define BST_H
template <class T>
class BST
{
struct node
{
T data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* tree);
node* insert(T x, node* tree);
node* findMin(node* tree);
node* findMax(node* tree);
node* remove(T x, node* tree);
void inorder(node* tree);
void preorder(node* tree);
void postorder(node* tree);
public:
BST();
~BST();
node* find(node* tree, T x);
void insert(T x);
void remove(T x);
void displayorder();
void search(T x);
};
#endif // BST_H
By example, in
BST::node* BST<T>::find(node* tree, T x)
you forget the <T> component for the first BST.
Should be
// vvv
BST<T>::node* BST<T>::find(node* tree, T x)
All other errors are of the same type.
A class template like the BST you are apparently using is not a type. It's a recipe for creating a class type. What the error message is trying to tell you is that (almost) anywhere you use the name BST, you need to supply template arguments immediately after it inside <angle brackets>.
For example, in
template <class T>
BST::node* BST<T>::makeEmpty(node* tree) //ERROR HERE
the compiler is complaining about the first instance of BST in the return type, not the one that correctly specifies BST<T> as the class type. This should probably be:
template <class T>
BST<T>::node* BST<T>::makeEmpty(node* tree)
[There are at least two exceptions to this general rule. One is that the name of a template alone can be used as a template argument to another template which expects a template instead of a type or value.
The other is called the "injected class name": Inside the scope of a class template, including a class template member, you can use just the name of the template as an alias to the "current" specialization.
So in fact you could also use a trailing return type and do:
template <class T>
auto BST<T>::makeEmpty(node* tree) -> BST::node*
In the above, since the return type now comes after the BST<T>:: and not before, it is now in the scope of the class template, so you are allowed to use just BST as an alias for BST<T>.]
class Node {
public:
Node();
void setNext(Node*);
private:
void* item;
Node* next;
};
void Node::setNext(Node* n)
{
next = n;
}
class List {
public:
List();
void addFirst(void*);
void reset();
void* getCurItem();
private:
Node* head;
Node* current;
};
void List::addFirst(void* obj)
{
Node *newNode = new Node(obj);
newNode->setNext(head);
head = newNode;
}
void List::reset()
{
current = head;
}
void* List::getCurItem()
{
return current->getItem();
}
Polynomial::Polynomial(std::ifstream& input){
List *polyCo = new List();
List *polyEx = new List();
bool exit = false;
double coefficient=0;
int exponent=0;
while(!exit && input.good()){
input >> coefficient;
if(coefficient != -9999.99){
input >> exponent;
cout << "Exponent before: " << exponent << endl;
cout << "Coefficient before: " << coefficient << endl;
int *ExPtr = &exponent;
double *CoPtr = &coefficient;
cout << "Exponent: " << *(ExPtr) << endl;
cout << "Coefficient: " << *(CoPtr) << endl;
polyCo->addFirst(ExPtr);
polyEx->addFirst(CoPtr);
cout << polyCo->getCurItem() << endl; //SEG FAULT
}
}
polyEx->reset();
polyCo->reset();
}
I am reading numbers from a file into two separate linked lists. I am getting a segmentation fault when attempting to access the value in either of the linked list.
First, I'm going to outline how I think my addFirst() functions works, because I could just be implementing it incorrectly. When addFirst() is called, a new node is created, and the data being read in is set equal to the member of the new node. The existing node in the list is pushed forward, and the new node created becomes the head. The two nodes are also linked.
There could be something else entirely wrong besides my implementation of the function, but at least it is a good place to start, along with the source of the segmentation fault.