VS2013和Ubuntu c ++之间的差异

时间:2014-12-12 13:03:36

标签: c++ visual-studio ubuntu

我在visual studio 2013中编写了一个c ++代码,它在那里工作得很好。 我需要它在Ubuntu上工作,但我收到一个错误 “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;

}

简单检查MyLinkedList实现

#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;
}

这是我的所有代码。如果Visual Studio和Ubuntu之间存在任何差异,我想知道。

副本: g ++ -Wall -Werror -Wvla -g ListExample.cpp MyLinkedList.cpp -o ListExample

1 个答案:

答案 0 :(得分:1)

“未定义的引用”通常表示您缺少包含程序所需符号之一的.o(对象)文件或库。

例如,如果你这样做

g++ ListExample.cpp

然后GCC将尝试将main.cpp直接编译为可执行文件,并要求它包含它引用的所有符号。

要编译目标文件然后将多个目标文件链接在一起,您应该执行类似

的操作
g++ -c ListExample.cpp
g++ -c Node.cpp
g++ -c MyLinkedList.cpp
g++ -o linked_list_test ListExample.o Node.o MyLinkedList.o

编写Makefile可以为您精简这一点。