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.
Related
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.
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 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();
I keep getting this error message every time I try to compile, and I cannot find out what the problem is. any help would be greatly appreciated:
C:\DOCUME~1\Patrick\LOCALS~1\Temp/ccL92mj9.o:main.cpp:(.txt+0x184): undefined reference to 'List::List()'
C:\DOCUME~1\Patrick\LOCALS~1\Temp/ccL92mj9.o:main.cpp:(.txt+0x184): undefined reference to 'List::add(int)'
collect2: ld returned 1 exit status
code:
//List.h
#ifndef LIST_H
#define LIST_H
#include <exception>
//brief Definition of linked list class
class List
{
public:
/**
\brief Exception for operating on empty list
*/
class Empty : public std::exception
{
public:
virtual const char* what() const throw();
};
/**
\brief Exception for invalid operations other than operating on an empty list
*/
class InvalidOperation : public std::exception
{
public:
virtual const char* what() const throw();
};
/**
\brief Node within List
*/
class Node
{
public:
/** data element stored in this node */
int element;
/** next node in list */
Node* next;
/** previous node in list */
Node* previous;
Node (int element);
~Node();
void print() const;
void printDebug() const;
};
List();
~List();
void add(int element);
void remove(int element);
int first()const;
int last()const;
int removeFirst();
int removeLast();
bool isEmpty()const;
int size()const;
void printForward() const;
void printReverse() const;
void printDebug() const;
/**
enables extra output for debugging purposes
*/
static bool traceOn;
private:
/** head of list */
Node* head;
/** tail of list */
Node* tail;
/** count of number of nodes */
int count;
};
#endif
//List.cpp I only included the parts of List.cpp that might be the issue
#include "List.h"
#include <iostream>
#include <iomanip>
using namespace std;
List::List()
{
//List::size = NULL;
head = NULL;
tail = NULL;
}
List::~List()
{
Node* current;
while(head != NULL)
{
current = head-> next;
delete current->previous;
if (current->next!=NULL)
{
head = current;
}
else
{
delete current;
}
}
}
void List::add(int element)
{
Node* newNode;
Node* current;
newNode->element = element;
if(newNode->element > head->element)
{
current = head->next;
}
else
{
head->previous = newNode;
newNode->next = head;
newNode->previous = NULL;
return;
}
while(newNode->element > current->element)
{
current = current->next;
}
if(newNode->element <= current->element)
{
newNode->previous = current->previous;
newNode->next = current;
}
}
//main.cpp
#include "List.h"
#include <iostream>
#include <string>
using namespace std;
//void add(int element);
int main (char** argv, int argc)
{
List* MyList = new List();
bool quit = false;
string value;
int element;
while(quit==false)
{
cin>>value;
if(value == "add")
{
cin>>element;
MyList->add(element);
}
if(value=="quit")
{
quit = true;
}
}
return 0;
}
I'm doing everything I think I'm suppose to be doing. main.cpp isn't complete yet, just trying to get the add function to work first. Any help will be greatly appreciated.
Describe your build process. It looks as though you're not building List.cpp, or else not linking it with main.cpp.
You're not compiling List.cpp. Add it to the command line.
In main.cpp, it's seeing (from List.h) "Hey, this class with this functionality will exist", but since you're not actually building/linking with List.cpp, it can't find the functions it's looking for.
Your command line should look something like g++ -o test.exe main.cpp List.cpp.
The key feature being to include both main.cpp and List.cpp.
There are other ways to do this, but this should get you started.
Your problem is not including all the different files in your command line arg compiler
Correct format:
get in correct directory
gcc -o list main.cpp List.cpp List.h
then you won't get anymore undefined references to functions
Good luck on building your 3 or 4 year old program...
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.