I am trying to build a Linked list application using C++ programming language & features such as inheritance etc.
I have split the interface & implementation in different files but not able to compile.
Below are the list of files
Interface files :- node.h , abstractList.h , singleLinkedList.h
Implementation files: singleLinkedList.cpp
node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
struct nodeType {
int data;
struct nodeType *next;
}listNode;
#endif
abstractList.h
#ifndef ABSTRACT_LIST_H
#define ABSTRACT_LIST_H
#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
class abstractList {
public:
virtual ~abstractList();
virtual bool isEmpty(Node* ) = 0;
virtual int get(const int&) = 0;
virtual int indexOf(const int& ) = 0;
virtual Node insert(const int& , const int& ) = 0;
virtual void delete(const int& ) = 0;
};
#endif
singleLinkedList.h
#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H
#include <iostream>
#include "node.h"
#include "abstractList.h"
class singleLinkedList : public abstractList {
public:
singleLinkedList();
~singleLinkedList();
Node populateList( );
private:
void checkIndex();
int data;
Node head;
};
#endif
So far i have just coded the populateList() function in the implentation file, here goes the implementation file.
singleLinkedList.cpp
#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
#include "abstractList.h"
Node singleLinkedList :: populateList()
{
Node temp;
int data;
temp = head;
char ch;
std::cout<<"Enter Data? (y/n) " << std::endl;
std::cin>>ch;
while(ch == 'Y' || ch == 'y')
{
std::cout<<"Enter the data that you would like to store.\n"<<std::endl;
std::cin>>data;
temp = new Node();
temp->data = data;
temp->next = head;
head = temp;
std::cout<<"Enter more data?"<<std::endl;
std::cin>>"\n">>ch;
}
return temp;
}
When i give g++ -c singleLinkedList.cpp , i am getting lot of errors. I am pretty sure i have done something stupid. Can anyone please pin point my error?
EDIT: Error Log With specfic issues.
struct nodeType {
int data;
struct nodeType *next;
}listNode;
virtual listNode *insert();
Is the above statement correct?
Thanks
Kelly
delete is a keyword in C++, you can't use it as a method name. You need to use a different name here:
class abstractList {
public:
//...
virtual void delete(const int& ) = 0;
//-----------^^^^^^ rename this.
};
The problem is in your typedef:
typedef listNode *Node;
means that all instances of Node will essentially replaced by listnode*
temp = new Node();
actually reads
temp = new listnode*();
But new Foo() would return a Foo* (because new returns a pointer to memory allocated for an object), meaning that new listnode*() would return a listnode**. temp being a listnode* has no Idea what a listnode** is and complains.
what you want to do is:
Node temp = new listnode();
or forget the typedef altogether:
listnode* temp = new listnode();
Related
I am just starting my implementation of my Skip List, and I have tried everything to get rid of this error. My Build Succeeds, but when I put "SList p" into my main program, it fails and said linked reference ld error. What is the problem?
Here is my .h file
#ifndef SLIST_H
#define SLIST_H
#include <string>
#include <fstream>
class SList {
public:
SList();
// overloaded assignment operator
SList& operator = (const SList&);
// copy constructor
SList(const SList&);
// destructor
~SList();
private:
const static int DUMMY_VALUE = -1000;
struct Node {
int data;
Node *next;
};
struct upperNode {
upperNode *next;
Node *down;
};
Node *head;
upperNode *upperHead;
int length;
};
#endif // SLIST_H
__
My .cpp file
#include "SList.h"
SList::SList() {
length = 0;
head->data = DUMMY_VALUE;
head->next = nullptr;
upperHead->down = head;
upperHead->next = nullptr;
}
and my main
#include "SList.h"
#include <iostream>
int main() {
SList p;
return 0;
}
What am I missing here? I feel like it is terribly obvious I just need a second set of eyes. Thank you.
You have declared a copy assignment operator, a copy constructor and a destructor, but you don't provide implementations.
So I have been working with linked lists and I am trying to assign temp variable to the first node in the list but it errors out
code where I set a temp node to list
Node *temp = NULL;
Node *found = NULL;
bool isfound = false;
temp = list;
the place where list defined in LinkedList class
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
class LinkedList{
private:
Node *list;
Node *createNode();
Node *searchLocation(int);
public:
LinkedList();
~LinkedList();
void insertNode();
void deleteNode(int);
void printList();
void searchNode();
};
#endif
node header
#ifndef NODE_H
#define NODE_H
class Node{
public:
char lastName[20];
char firstName[20];
int idNumber;
Node *next;
Node *head;
Node(char a[], char b[], int i);
void printNode();
};
#endif
node class
#include <iostream>
#include "Node.h"
using namespace std;
Node::Node(char a[], char b[], int i){
*lastName = *a;
*firstName = *b;
idNumber = i;
}
void Node::printNode(){
cout<<lastName<<", "<<firstName<<": "<<idNumber<<endl;
}
error says can't convert from LinkedList* to Node* in assignment temp = list;
You haven't shown where you declare the list object, but from the error I can see it is of type LinkedList*.
If you want to point to the first node, then you have to assign
temp = list->list.
Of course since list is a private member of LinkedList, you cannot do this outside of the LinkedList class.
So you need to provide an accessor function that returns this node object.
Assuming that list is a LinkedList *, then temp = list->list; should work.
ok, so i've decided to start brushing off the dust from my c++ knowledge, and started by doing some simple examples with linked lists, but i get some errors. my Node class is TestClass and my linked list is List; The problems, from what i see are syntax related.
Node Header:
#pragma once
class TestClass
{
public:
int x, y;
TestClass *next;
TestClass *prev;
TestClass();
TestClass(int,int);
~TestClass();
};
Node Base class
#include "stdafx.h"
#include "TestClass.h"
TestClass::TestClass()
{
}
TestClass::TestClass(int _x, int _y)
{
x = _x;
y = _y;
}
TestClass::~TestClass()
{
}
Header List class
#pragma once
class List
{
public:
TestClass *first;
TestClass *last;
List();
~List();
void AddNew(TestClass);
void PrintList();
};
List base class
#include "stdafx.h"
#include "List.h"
#include "TestClass.h"
#include <iostream>
using namespace std;
List::List()
{
first = last = NULL;
}
List::~List()
{
}
void List::AddNew(TestClass node)
{
if (!first && !last)
{
*first = *last = node;
//first = last = &node;
}
else
{
TestClass *temp;
temp = last;
last = &node;
temp->next = last;
}
}
void List::PrintList()
{
TestClass *p = first;
while (p != NULL)
{
cout << p->x << " ";
p = p->next;
}
}
I get around 16 errors, like:
undeclared identifiers first, last in List.cpp (base class)
syntax error : missing ';' before '*' -> List.h declaration of TestClass *last;
Can you please give a helping hand?
#include "TestClass.h in List.h before using the type.
Header List class shall include header Node Header because it referes declarations from it.
my code have error (error C2512: 'Node' : no appropriate default constructor available)
but i have default constructor why ???
my error location commented in code Please help me
Node.h
#pragma once
#include "stat.h"
#include "Automata.h"
#include <cstdlib>
class Node
{
friend class Automata;
friend class stat_a;
friend stat_a* makeauto(char *str);
friend int main();
private:
stat_a* mess;
char data;//harfi ke ba in masir estefadeh mishe :)
Node *next;//node badi dar araye node ha class stat_a :)
public:
Node()
{
mess = NULL;
next = NULL;
};
};
stat.h
#pragma once
#include "Node.h"
#include <iostream>
using namespace std;
class stat_a
{
friend class Automata;
friend class Node;
friend int main();
private:
bool is_final_stat_a; //aya final stat_a hast ???
int stat_a_num; //shomareh halat 0,1,2,...
Node *last; //akharin node dar araye node haye neshan dahande masir
Node *first; //Avalin node dar araye node haye neshan dahande masir
public:
void add(char d,stat_a * a)//ezafeh kardan masiri ke ba estefadeh
{ //az harf (char d ) be halat (stat_a a) miravad
if(first == NULL)
{
first = new Node;//error is here
first->data = d;
first->mess = a;
last=first;
}
else
{
last->next = new Node ;//erorr is here
last=last->next;
last->data=d;
last->next=NULL;
last->mess=a;
}
};
/***********************************************************************/
void print()
{
cout<<stat_a_num<<"========> is final_stat_a : "<<is_final_stat_a<<endl;
Node *a;
a=first;
while(a != NULL)
{
cout<<"========> By '"<<a->data<<"' go to stat "<<a->mess->stat_a_num<<endl;
a=a->next;
}
};
stat_a()
{
last=NULL;
first=NULL;
is_final_stat_a=false;
};
~stat_a(void);
};
I have default constructor available why error
It's a classical example of circular dependency. The header file Node.h depends on the header file stat.h which depends on Node.h and so on.
Since you only declare a pointer variable of type stat_h in Node, you don't need to include the header file for that, it's enough to declare the class stat_a:
#pragma once
#include "Automata.h"
#include <cstdlib>
class stat_a; // Declare the class, so the compiler know there's a class by this name
class Node
{
// ...
private:
stat_a* mess; // Works because you're only declaring a pointer
// ...
public:
// ...
};
Then in the stat.h header when you include Node.h there is no longer a circular dependency.
Replace
Node();
{
mess = NULL;
next = NULL;
}
with
Node()
{
mess = NULL;
next = NULL;
};
I'm trying to implement a linked list but get an error when compiling:
intSLLst.cpp:38: error: ‘intSLList’ has not been declared
intSLList looks like it's been declared to me though so I'm really confused.
intSLLst.cpp
#include <iostream>
#include "intSLLst.h"
int intSLList::deleteFromHead(){
}
int main(){
}
intSLLst.h
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
#include <cstddef>
class IntSLLNode{
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = NULL){
info = el; next = ptr;
}
};
class IntSLList{
public:
IntSLList(){
head = tail = NULL;
}
~IntSLList();
int isEmpty();
bool isInList(int) const;
void addToHead(int);
void addToTail(int);
int deleteFromHead();
int deleteFromTail();
void deleteNode(int);
private:
IntSLLNode *head, *tail;
};
#endif
You're using a lower case i
int intSLList::deleteFromHead(){
}
should be
int IntSLList::deleteFromHead(){
}
Names in c++ are always case sensitive.
intSLList isn't the same as IntSLList. This isn't Pascal. C++ is case sensitive.