0

I am new to c++ and am really struggling with getting something simple working. When I use g++ I get one of the two type of errors shown above. I have looked around the forums and have gathered that it is a common question but when I fix with one answer I get the other error when using g++. I am interested in understanding this better.

#ifndef ORDEREDLIST_H_
#define ORDEREDLIST_H_

#include <iostream>
using namespace std;
class Node
{
public:
double item; // data item
Node *next; // pointer to next node
};
class OrderedList
{

// friend operator functions
friend ostream &operator<<(ostream &, const OrderedList &);
friend istream &operator>>(istream &, OrderedList &);

public:
// constructors
OrderedList():_head(NULL),_size(0) {};
OrderedList(const OrderedList &); // copy constructor

// destructor
~OrderedList();
// operator functions
OrderedList operator+(OrderedList&); // merges two OrderedLists
double operator[](int) const; // subscript operator returns rvalue
const OrderedList &operator=(const OrderedList &); // assignment

void insert(double); // inserts an item
bool remove(int); // remove i th item
void pop(); // pop the first item

void tester();

private:
Node *_head;
int _size;
};



#endif /* ORDEREDLIST_H_ */

The class functions are here:(NB I have only included a part of the code)

#include <iostream>
#include "OrderedList.h"
using namespace std;

OrderedList::OrderedList():_head(NULL),_size(0) {};
//OrderedList::OrderedList(const OrderedList &); // copy constructor
OrderedList::~OrderedList(){
    //nothing
};

void OrderedList::pop()
{
    //nothing yet
}

And then the simple test call to the pop function which currently does nothing doesn't work. I am just trying to get the code below to recognise the pop() function so that I can continue to build my program incrementally.

#include <iostream>
#include "OrderedList.h"

using namespace std;


int main()
{
    OrderedList* List_A = new OrderedList;
    cout<<"Hello"<<endl;
    List_A->pop();

    return 0;
};

The exact error I get is: In function main': ass2q1.cpp:(.text+0x46): undefined reference toOrderedList::pop()' collect2: error: ld returned 1 exit status

1 Answers1

0

You need to compile (and compile only) both .cpp files and then link everything together:

# compile source files to object files
g++ -std=c++11 OrderedList.cpp -c -o OrderedList.o
g++ -std=c++11 ass2q1.cpp -c -o ass2q1.o

# link the object files to an executable
g++ ass2q1.o OrderedList.o -o ass2q1

(of course that's only one (common) option)

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • Thanks Daniel. (I obviously have lots of bugs to fix but this will help heaps). Thanks for your comments and effort. – user3551662 Apr 19 '14 at 13:09