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.
Related
This problem occurs in my main.cpp:
using namespace std;
#include <iostream>
#include "BST.h"
#include "Packet.h"
int main()
{
BST test; // It occurs on this line!
Packet one(1, "testPacket", 1, 1);
system("Pause");
}
The error on that line says:
argument list for class template "BST" is missing
I don't know how to fix it. I just want to initialize the BST. How can I fix this error? I'm not very experienced with templates. Please help. My priority is fixing this glaring problem right now. Can I get help?
For reference purposes:
BST.h:
#ifndef BST_H
#define BST_H
using namespace std;
template <typename T>
class Node {
public:
Node() : rlink(nullptr), llink(nullptr) {}
~Node() {}
private:
T data;
Node *rlink, *llink;
};
template <typename T>
class BST {
public:
BST();
void insert(T data);
private:
Node * root;
};
#endif
BST.cpp
#include "BST.h"
template <typename T>
BST<T>::BST() : root(nullptr) {}
template <typename T>
void BST<T>::insert(T data) {
if (root != nullptr) {
}
else {
cout << "NPTR" << endl;
}
}
Packet.h
#ifndef PACKET_H
#define PACKET_H
#include <string>
using namespace std;
class Packet {
public:
Packet(int partId, string description, double price, int partCount) :
partId(partId), description(description), price(price), partCount(partCount) {}
int getPartId() const { return partId; }
string getDescription() const { return description; }
double getPrice() const { return price; }
int getPartCount() const { return partCount; }
private:
int partId;
string description;
double price;
int partCount;
};
#endif
There are 2 problems.
The first is that Node needs to know what type T is, so you need to tell it when you use Node like this:
template <typename T>
class BST {
public:
BST();
void insert(T data);
private:
Node<T> * root;
};
Secondly, BST needs to know what its own type T is when you try to use it, so you need to do it like this:
BST<int> test; // Or whatever you are searching for in your tree. Doesn't have to be an int
P.S. Just heading this off now, you're probably going to need to implement BST in the header file. Failure to do so might cause linker problems.
P.P.S. I've been reading your comments on the original post, and what you actually probably need this instead:
BST<Packet> test; // Since you are searching for packets.
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.
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...