我在Windows上编写了这段代码并且运行正常,但在Ubuntu中使用g ++我得到了这个 错误:
在myList.h中
#ifndef myList
#define myList
#include <iostream>
using namespace std;
class List {
private:
class Node; // Forward declaration
Node *tail;
Node *head;
public:
List();
List(const List &src);
~List();
void newTail(const double num);
void newHead(double num);
void insert(const int pos, const double num);
void print() const;
void printReverse() const;
double popHead();
double popAt(const int pos);
double dataHead() const;
double dataAt(const int pos) const;
bool isEmpty() const;
int size() const;
List& operator = (const List & rhs);
List& operator += (const List & rhs);
};
#endif
在myList.cpp中(不是全部)
#include "myList.h"
class List::Node {
private:
double data;
Node *next;
Node *prev;
public:
friend class List;
Node(Node *argNode, double num) : next(argNode), data(num) // Initilize this node's next pointer to first arg and data to second arg
{
if (argNode != NULL) // If this node isn't to be head
{
this->prev = argNode->prev; // Initilize this node's prev pointer to next's node prev pointer
if (argNode->prev != NULL) // If the next node wasn't tail
argNode->prev->next = this; // Set the next's node's previous node's next pointer to this node
argNode->prev = this; // Set the next node's prev pointer to this node
}
}
};
List::List(){}
List::List(const List &list)
{
*this = list;
}
List::~List()
{
while (tail != NULL)
{
Node *temp = tail;
tail = temp->next;
delete temp;
}
head = NULL;
}
double List::popAt(const int pos)
{
if (pos >= List::size())
cout << "No node at position: " << pos << endl;
double data = 0;
int nodeCounter = 0;
for (Node *p = tail; p; p = p->next, nodeCounter++)
{
if (nodeCounter == pos)
{
data = p->data;
if (p == head && p == tail)
{
List::~List();
break;
}
if (p != head && p != tail)
{
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
break;
}
if (p == tail)
{
p->next->prev = NULL;
tail = p->next;
delete p;
break;
}
if (p == head)
{
p->prev->next = NULL;
head = p->prev;
delete p;
break;
}
}
return data;
}
我已经阅读了其他有关此错误的帖子,但他们都没有提到析构函数,我不能因为爱我而理解为什么它不会特别针对Ubuntu。
感谢您的帮助和时间。
答案 0 :(得分:1)
正如评论所指出的,调用List :: ~List()是一个坏主意。
function f(){
var ar=[],i;
for(i=0;i<3;i++){
//this time instead of passing i, the funciton has a local value called
x
arr[i]=(function(x)){
return function(){
return x;
};
}(i));
}
return arr;
}
更好的主意。 感谢您的所有帮助,我想我得到了应得的待遇。