VS2013和Ubuntu c++; 我在Visual Studio 2013中编写了一个C++代码,它在那里工作得很好。 我需要它在Ubuntu上工作,但我得到一个错误 “未定义的引用节点::节点()”
Node.hVS2013和Ubuntu c++; 我在Visual Studio 2013中编写了一个C++代码,它在那里工作得很好。 我需要它在Ubuntu上工作,但我得到一个错误 “未定义的引用节点::节点()”,c++,visual-studio,ubuntu,C++,Visual Studio,Ubuntu,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::N
#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.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.h”
#包括
MyLinkedList::MyLinkedList()
{
head=NULL;
tail=NULL;
尺寸=0;
}
void MyLinkedList::add(常量std::字符串键,双val){
Node*n=新节点(val,key);
if(head==NULL){
水头=n;
尾=头;
tail->next=NULL;
}
否则{
n->prev=尾部;
n->next=NULL;
tail->next=n;
尾=n;
}
++大小;
}
MyLinkedList::MyLinkedList(常量MyLinkedList&l){
节点*温度=左封头;
while(temp!=NULL){
此->添加(临时->键,临时->数据);
温度=温度->下一步;
}
}
MyLinkedList::~MyLinkedList()
{
节点*温度=头部;
节点*toDelete=temp;
while(temp!=NULL)
{
温度=温度->下一步;
删除toDelete;
toDelete=温度;
}
}
bool MyLinkedList::isEmpty()
{
返回头==NULL;
}
void MyLinkedList::printList(){
if(head==NULL){
std::cout next;
删除p;
p=水头;
如果(p!=NULL)
n=p->next;
--大小;
++计数;
}
while(大小>0&&n->next!=NULL)
{
如果(!s.compare(n->key)){
p->next=n->next;
n->next->prev=p;
删除n;
n=p->next;
--大小;
++计数;
}
否则{
p=n;
n=n->next;
}
}
如果(大小>0&&!n->键比较){
n->prev->next=NULL;
删除n;
++计数;
--大小;
}
返回计数;
}
MyLinkedList&MyLinkedList::operator=(常量MyLinkedList&l){
如果(此!=&l){
节点*温度=头部;
节点*toDelete=temp;
while(temp!=NULL)
{
温度=温度->下一步;
删除toDelete;
toDelete=温度;
}
温度=l.水头;
while(temp!=NULL){
此->添加(临时->键,临时->数据);
温度=温度->下一步;
}
}
归还*这个;
}
bool MyLinkedList::isInList(常量std::字符串键、双精度和数据){
节点*温度=头部;
while(temp!=NULL){
如果(!temp->key.compare(键)){
数据=临时->数据;
返回true;
}
}
返回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;
}
#包括“MyLinkedList.h”
#包括
#包括
int main()
{
MyLink列表mylist;
std::string firstWord=“aa”;
双firstVal=1.5;
std::string secondWord=“bb”;
双secVal=2.2;
std::string thirdWord=“ab”;
双三分位=1.0;
mylist.printList();
std::cout“未定义引用”通常意味着您缺少包含程序所需符号之一的.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可以为您简化此过程。听起来好像您没有将Node.cpp
链接到程序中。您是如何编译和链接此文件的?g++-Wall-Werror-Wvla-g listedexample.cpp MyLinkedList.cpp-oListExample@igor这是您的问题,Node.cpp不在其中。请将Node.cpp
添加到该命令中的源列表中。你说得对,它正在工作,但在我的项目中,它说它应该用这行代码编译:“g++-Wall-Werror-Wvla-g ListExample.cpp MyLinkedList.cpp-o ListExample”。我怎样才能让它按要求工作?你可以和说这句话的人谈谈。好的,现在我理解了这个问题。谢谢你的帮助
g++ -c ListExample.cpp
g++ -c Node.cpp
g++ -c MyLinkedList.cpp
g++ -o linked_list_test ListExample.o Node.o MyLinkedList.o