-3

I wrote a c++ code in visual studio 2013 it's working great there. I need it to work on Ubuntu to , but I get an error "Undefinded referance Node::Node()"

Node.h

#pragma once
#include "string"

class Node
{
public:
    double data;
    std::string key;
    Node *next;
    Node *prev;

    Node();
    Node(double data , std::string key);
};

Node.cpp

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


Node::Node(){
        data = 0;
        key = "";
        next = NULL;
        prev = NULL;
}

Node::Node(double data, std::string key){
    this->data = data;
    this->key = key;
}

MyLinkedList.h

#pragma once
#include "Node.h"
#include "string"

class MyLinkedList
{
public:


    Node *head;
    Node *tail;
    int size;

    MyLinkedList();
    MyLinkedList(const MyLinkedList &l);
    ~MyLinkedList();


    bool isEmpty();
    void printList();
    void add(const std::string key, const double val);
    int remove(std::string s);

    MyLinkedList& operator=( const MyLinkedList& l);
    bool isInList(const std::string key, double &data);
};

MyLinkedList.cpp

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

MyLinkedList::MyLinkedList()
{
    head = NULL;
    tail = NULL;
    size = 0;
}

void MyLinkedList::add(const std::string key, double val){

    Node *n = new Node(val , key);
    if (head == NULL){
        head = n;
        tail = head;
        tail->next = NULL;
    }
    else{
        n->prev = tail;
        n->next = NULL;
        tail->next = n;

        tail = n;

    }
    ++size;
}

MyLinkedList::MyLinkedList(const MyLinkedList &l){

    Node *temp = l.head;

    while (temp != NULL){
        this->add(temp->key, temp->data);
        temp = temp->next;
    }
}

MyLinkedList::~MyLinkedList()
{
    Node *temp = head;
    Node *toDelete = temp;

    while (temp != NULL)
    {
        temp = temp->next;
        delete toDelete;
        toDelete = temp;
    }

}

bool MyLinkedList::isEmpty()
{
    return head == NULL;
}

void MyLinkedList::printList(){

    if (head == NULL){
        std::cout << "Empty" << std::endl;
        return;
    }
    Node *temp = head;

    while (temp != NULL)
    {
        std::cout << temp->key <<","<< temp->data << std::endl;
        temp = temp->next;
    }

}

int MyLinkedList::remove(const std::string s){

    Node *p = head;
    Node *n = p->next;
    int count=0;

    while (size > 0 && !head->key.compare(s)){
        head = head->next;
        delete p;
        p = head;
        if (p!=NULL)
            n = p->next;

        --size;
        ++count;
    }
    while (size > 0 && n->next != NULL)
    {
        if (!s.compare(n->key)){
            p->next = n->next;
            n->next->prev = p;
            delete n;
            n = p->next;
            --size;
            ++count;
        }
        else{
            p = n;
            n = n->next;
        }
    }
    if (size > 0 && !n->key.compare(s)){
        n->prev->next = NULL;
        delete n;
        ++count;
        --size;
    }
    return count;

}

MyLinkedList& MyLinkedList::operator = (const MyLinkedList& l){

    if (this != &l) {
        Node *temp = head;
        Node *toDelete = temp;

        while (temp != NULL)
        {
            temp = temp->next;
            delete toDelete;
            toDelete = temp;
        }

        temp = l.head;

        while (temp != NULL){
            this->add(temp->key, temp->data);
            temp = temp->next;
        }

    }
    return *this;

}

bool MyLinkedList::isInList(const std::string key, double &data){
    Node *temp = head;

    while (temp != NULL){
        if (!temp->key.compare(key)){
            data = temp->data;
            return true;
        }
    }
    return false;

}

Simple checks for the MyLinkedList implementation

#include "MyLinkedList.h"
#include <iostream>
#include <string>


int main()
{

    MyLinkedList mylist;
    std::string firstWord = "aa";
    double firstVal = 1.5;
    std::string secondWord = "bb";
    double secVal = 2.2;
    std::string thirdWord = "ab";
    double thirdVal = 1.0;

    mylist.printList();
    std::cout << "Done print list" << std::endl << std::endl;

    mylist.add(firstWord, firstVal);
    mylist.add(secondWord, secVal);
    mylist.add(firstWord, thirdVal);
    mylist.add(thirdWord, firstVal);

    mylist.printList();
    std::cout << "Done print list" << std::endl << std::endl;

    return 0;
}

This is all my code. If there is any more differences between Visual Studio and Ubuntu, I would like to know.

copiled with : g++ -Wall -Werror -Wvla -g ListExample.cpp MyLinkedList.cpp -o ListExample

igor
  • 716
  • 1
  • 9
  • 27

1 Answers1

1

"Undefined reference" typically means you're missing a .o (object) file or library that contains one of the symbols that your program needs.

For example, if you do

g++ ListExample.cpp

then GCC will try to compile main.cpp straight to an executable and will require that it contain all of the symbols it references.

To compile object files then link multiple object files together, you should instead do something like

g++ -c ListExample.cpp
g++ -c Node.cpp
g++ -c MyLinkedList.cpp
g++ -o linked_list_test ListExample.o Node.o MyLinkedList.o

Writing a Makefile can streamline this for you.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • You right it's working , but in my project it says that it's should compile with this line : "g++ -Wall -Werror -Wvla -g ListExample.cpp MyLinkedList.cpp -o ListExample" . How can I get it work as requested? – igor Dec 12 '14 at 13:21
  • You could speak to whoever said that. – stefaanv Dec 12 '14 at 13:25
  • ok , now I understood the problem . Thanks for help – igor Dec 12 '14 at 13:27