as a beginner, I was practicing C++ programming by building a program with one header file but I have been stuck in one problem for several hours.
My program has very simple structure:
main.cpp, which includes function, int main()
chain.hpp, which includes declaration of classes and functions
chain.cpp, which includes detailed definition of each functions
// main.cpp
#include <iostream>
#include "chain.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
Chain<int> linkedlist;
linkedlist.insertNode(0, 123);
}
// Chain.hpp
#ifndef chain_hpp
#define chain_hpp
#include <stdio.h>
template<class T>
class ChainNode{
public:
ChainNode<T>* next;
T data;
ChainNode(){next=NULL; data=NULL;}
ChainNode(ChainNode<T>* next, T data);
~ChainNode();
private:
};
template<class T>
class Chain{
private:
ChainNode<T>* first;
public:
Chain(){first=NULL;}
~Chain();
int indexOf(const T& element) const;
void deleteNode(int index);
void insertNode(int index, const T& element);
bool isEmpty() const {return first==0;}
};
#endif /* chain_hpp */
// chain.cpp
#include "chain.hpp"
template<class T>
ChainNode<T>::ChainNode(ChainNode<T>* next, T data){
this->next = next;
this->data = data;
}
template<class T>
ChainNode<T>::~ChainNode(){
delete this;
}
template<class T>
Chain<T>::~Chain(){
while(first!=NULL){
ChainNode<T>* next = first->next;
delete first;
first = next;
}
}
template<class T>
int Chain<T>::indexOf(const T& element) const{
ChainNode<T>* current = first;
int index = 0;
while(current!=NULL && current->data){
current = current->next;
index++;
}
if(current==NULL) return -1;
return index;
}
template<class T>
void Chain<T>::deleteNode(int index){
if(index<0) throw "Negative index cannot be input";
if(first ==0) throw "Empty chain";
ChainNode<T>* deleteNode = first;
ChainNode<T>* beforeNode = first;
if(index==0){
first = first->next;
}
else{
for(int i=0;i<index-1;i++){
if(beforeNode==NULL) throw "Index is bigger than length of the chain";
beforeNode = beforeNode->next;
}
deleteNode = beforeNode->next;
beforeNode->next = beforeNode->next->next;
}
delete deleteNode;
}
template<class T>
void Chain<T>::insertNode(int index, const T& element){
if(index<0) throw "Negative index cannot be input";
if(first==0) throw "Empty chain";
ChainNode<T>* newnode = new ChainNode<T>();
if(index==0){
newnode->next = first;
first = newnode;
}
else{
ChainNode<T>* beforenode = first;
for(int i=0;i<index-1;i++){
if(beforenode==NULL) throw "Index is bigger than length of the chain";
beforenode = beforenode->next;
}
newnode->next = beforenode->next;
beforenode->next = newnode;
}
}
And error messages are as below
Undefined symbols for architecture x86_64:
"Chain<int>::insertNode(int, int const&)", referenced from:
_main in main.o
"Chain<int>::~Chain()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I know myself that there might be further problems to be fixed, but due to this build time error, I cannot step to that level.
Since I was using Xcode for my programming, I suspected Xcode itself might be the reason of this problem, but even when I tried compiling through my terminal and g++ *.cpp command, the same error message was annoying me. Is there any expert who can help me stop wasting my time?