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...
Related
I'm having trouble wrapping my head around why this code is not compiling. I am implementing a stack as a doubly linked list. I cannot get my AddToHead() to work. More specifically, the program wont compile if I try to dynamically create a CharNode object. I thought by having #include "charlist.h" would give the program access to the CharNode class, since it resides in charlist.h
I compile with: g++ -ansi -pedantic -Wall charlist.cxx -o clist
This is the error I get:
/tmp/ccHzaOmz.o: In function `CharList::AddToHead(char)':
charlist.cxx:(.text+0xe9): undefined reference to `CharNode::CharNode(char, CharNode*, CharNode*)'
collect2: error: ld returned 1 exit status
I know that undefined reference means that the CharNode resources can't be found by the linker. I just don't know why it is happening here.
Here is charlist.h
#ifndef __CharList__
#define __CharList__
#include <iostream>
#include <string>
using namespace std;
class CharList;
//CharNode class is clearly here in charlist.h
class CharNode
{
private:
char value;
CharNode* prev;
CharNode* next;
public:
CharNode(char value, CharNode* prev = NULL, CharNode* next = NULL);
friend class CharList;
};
class CharList
{
private:
CharNode* h;
CharNode* t;
public:
CharList();
~CharList();
bool IsEmpty() const;
char GetHead() const; //FUNCTION CAUSING ERROR
char GetTail() const;
void AddToHead(char v);
void AddToTail(char v);
};
#endif //__CharList__
Here is charlist.cxx
#include <iostream>
#include <string>
#include <sstream>
#include <cassert>
#include <stdlib.h>
#include "charlist.h"
using namespace std;
CharList::CharList()
{
h = t = NULL;
}
bool CharList::IsEmpty() const
{
return (h == NULL);
}
//All other member functions excluded for relevancy
void CharList::AddToHead(char v){
CharNode* newHead;
newHead = new CharNode(v); //Why cant I do this? Error Line.
newHead->prev = NULL;
newHead->next = h;
if (IsEmpty()){
t = newHead;
h = newHead;
} else {
h->prev = newHead;
h = newHead;
}
}
Below, you have a declaration of a constructor. This is a promise that you will define a constructor, somewhere.
CharNode(char value, CharNode* prev = NULL, CharNode* next = NULL);
Change it to also include the definition, and you will not get that undefined error.
CharNode(char value, CharNode* prev = NULL, CharNode* next = NULL)
: value(value)
, prev(prev)
, next(next)
{
}
Because you have not defined CharNode::CharNode() anywhere yet.
Fill in the following in your charlist.cxx and it should build and link:
CharNode::CharNode(char value, CharNode* prev = NULL, CharNode* next = NULL)
{
// Your code here...
}
I'm trying to read a text file containing the letters "farming" into a linked list of nodes. I've made a class named NumberList that has the structure for the nodes. Here's the header.
#ifndef NUMBERLIST
#define NUMBERLIST
#include <iostream>
using namespace std;
class NumberList
{
protected:
//declare a class for the list node
//constructor to initialize nodes of list
struct ListNode
{
char value;
ListNode *next;
// Constructor
ListNode(char value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; //pointer to head of the list
public:
NumberList() { head = NULL; } //constructor
~NumberList(); //destructor
void displayList() const; //print out list
void reverse();
};
#endif
Where I'm running into a problem is trying to read the text file into a linked list in main().
Here's what I have in main:
#include "Numberlist.h"
#include "ReliableNumberList.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ListNode *letterList = nullptr; //create a linked list
char letter;
//This is where I read the file into the list
//open the file
ifstream letterFile("linkedText.txt");
if (!letterFile)
{
cout << "Error in opening the file of letters.";
exit(1);
}
//read the file into a linked list
while (letterFile >> letter)
{
//create a node to hold this letter
letterList = new ListNode(letter, letterList);
//missing a move to the next node?
}
return 0;
}
This read file sample came from my text book but the structure its reading into was not located in a separate class. For the life of me I cannot figure out how I reference ListNode struct in the NumberList class. Visual Studio is stating that ListNode and letterList are undefined. I know its because I'm not referencing them properly from the NumberList class.
Any help would be greatly appreciated.
A quick solution to your problem could be this:
//------------------------------NumberList.hpp-----------------------------
#ifndef NUMBERLIST_HPP
#define NUMBERLIST_HPP
#include <iostream>
class NumberList{
protected:
//Protected Members can't be used outside the class
struct ListNode{
char value;
ListNode *next;
ListNode(char value1, ListNode *next1 = NULL){
value = value1;
next = next1;
}
};
ListNode *head, *tail; //class members
//head always points at the 1st letter, tail is used for quick adding at the end
public:
NumberList() { head = NULL; tail = NULL; }
~NumberList(); //don't forget to deallocate space properly at the end
void displayList() const; //print out list
void reverse();
void add(char newchar) {
//allocate a new node using the ListNode constructor, by default, next1 will be null
ListNode *newNode = new ListNode(newchar); //equvalent to ListNode(newchar, NULL);
if (tail == NULL) { //if no elements in the list, both show to newNode
tail = newNode;
head = newNode;
}else{
tail->next = newNode; //make last node -> next pointer, point to newNode (new last node)
tail = tail->next; //make current last node be the actual last node
}
}
};
#endif
//------------------------------Main.cpp-----------------------------
#include "Numberlist.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream letterFile("linkedText.txt");
if (!letterFile){
cout << "Error in opening the file of letters.";
exit(-1);
}
NumberList numberList;
char letter;
while (letterFile >> letter) numberList.add(letter);
}
It slightly alters your logic, as you no longer add list nodes to your list,
but I suspect that you don't want to, anyway. Instead, better add the characters
directly to the list, and let the list handle its nodes (makes sense, since the node struct is protected).
Certainly the class needs more refining, but this should solve your initial problem.
I'm trying to write simple binary tree program in C++ using VS 2012.Even all paths are set it give me link error as shown in attached and when i comment the comment-out inside the insert function, it compiles without error.
// C++ code
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout <<" Simple Binary Tree Examples";
getchar();
return 0;
}
struct node
{
int data;
node *left;
node *right;
};
public class BinaryTree
{
public :
BinaryTree();
~BinaryTree();
void insert(int value);
/*{
if(root==NULL)
{
insert(value,root);
}
else
{
root = new node;
root->data=value;
}
}*/
void delete_tree();
private:
node *root;
void insert(int value,node *leaf);
};
BinaryTree::BinaryTree()
{
root=NULL;
}
BinaryTree::~BinaryTree()
{
delete_tree();
}
void BinaryTree::insert(int value)
{
// If i un-comment the below code.. it gives link error.
/* if(root==NULL)
{
insert(value,root);
}
else
{
root = new node;
root->data=value;
}*/
}
really dont know what could be wrong and hence shared the entire code.
Your code doesn't contain a definition of delete_tree, which is mentioned in your destructor.
It shouldn't compile neither with that code commented, nor with it.
I feel this question may be a bit trivial, but I simply cannot wrap my head around it. I currently have a class, Node, which is trying to point to what node occurs before the current node using the pointer prevNode. However I seem unable to access any variables within prevNode.
When running Main.cpp from the following code, it prints the result '15340756'. Where am I going wrong? Appologies as Im still a bit new to C++.
Node.h
#include "stdafx.h"
class Node
{
public:
Node();
void setPrevNode(Node n);
Node getPrevNode();
int i;
private:
Node *prevNode;
};
Node.cpp
#include "stdafx.h"
#include "Node.h"
Node::Node(){
i = 0;
}
void Node::setPrevNode(Node n){
prevNode = &n;
}
Node Node::getPrevNode(){
return *prevNode;
}
Main.cpp
#include "stdafx.h"
#include "Node.h"
int _tmain(int argc, _TCHAR* argv[])
{
Node nodes[] = {Node(), Node()};
nodes[0].i = 1;
nodes[1].setPrevNode(nodes[0]);
printf("%i", nodes[1].getPrevNode().i);
while(true){
}
return 0;
}
void setPrevNode(Node n);
Here setPrevNode is declared to take a copy of the node passed as an argument, and point to such node. After the function returns, the pointed to node no longer exist and what you get is undefined behavior.
What you want is to take the Node either as a reference or a pointer instead:
void setPrevNode(Node& n)
{
prevNode = &n;
}
void setPrevNode(Node* n)
{
prevNode = n;
}
On the same line, getPrevNode is defined to return a copy of the previous node. You most certainly want to return a reference here instead, although you can also return a pointer:
Node& getPrevNode()
{
return *prevNode;
}
Node* getPrevNode()
{
return prevNode;
}
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();