对于这个程序,我创建了一个名为Map的类,它通过存储一个名为MapItem的结构来像链接列表一样工作。我注意到当我调用复制构造函数并传入Map对象中没有MapItems时,程序不会崩溃。但是,如果我调用copy-construct并将其中带有MapItem的Map对象传入,程序将崩溃。因此,我认为问题可能在于我在下面附加的add函数,或者可能是我的析构函数,因为当我注释掉它的析构函数的时候它就起作用了。代码粘贴在下面并保持尽可能最小。谢谢。当使用复制构造函数时,C++程序崩溃,动态内存可能是罪魁祸首?
Map.h
#include <iostream>
using namespace std;
template <class keyType, class valueType>
struct MapItem
{
keyType key;
valueType value;
MapItem<keyType, valueType> *prev, *next;
};
template <class keyType, class valueType>
class Map
{
public:
Map(); // constructor for a new empty map
Map (const Map<keyType, valueType> & other); //constructor for a new map that is intialized with the values of other
~Map(); // destructor
void add (const keyType &key, const valueType &value);
/* Adds a new association between the given key and the given value.
If the key already has an association, it should do nothing.
*/
private:
/* The head and tail of the linked list you're using to store
all the associations. */
MapItem <keyType, valueType> *head, *tail;
int sizeList; //size of the list
int position; //key-value pair we are looking at, can be from 1 - sizeList using next() and first() function
};
Map.cpp
#include "Map.h"
template <class keyType, class valueType>
Map<keyType, valueType>::Map()
{
sizeList = 0; //set the size to 0
position = 1; //position is from 1 -> sizeList
head = NULL; //head points to NULL
tail = NULL; //tail points to NULL
}
template <class keyType, class valueType>
Map<keyType, valueType>::Map(const Map<keyType, valueType> &other) //copy constructor
{
head = NULL;
tail = NULL;
sizeList = other.sizeList; //assign the same size (shallow copy)
position = other.position; //assign the same position (shallow copy)
}
template <class keyType, class valueType>
Map<keyType, valueType>::~Map()
{
struct MapItem<keyType, valueType> *temp; //create temp variable to hold which item we are looking at in the list
temp = head; //start at the head
for(int i = 1; i <=sizeList; i++)
{
MapItem<keyType, valueType> *next = temp->next;
delete temp; //delete memory to pointed by temp
if(i != sizeList) //if we are not at the last node
temp = next;
}
}
template <class keyType, class valueType>
void Map<keyType, valueType>::add(const keyType &key, const valueType &value)
{
struct MapItem<keyType, valueType> *newItem; //create pointer to new map item
if(sizeList == 0) //if linked list is empty, make newItem the HEAD
{
newItem = new MapItem<keyType, valueType>; //dynamically allocate a new item on the heap
newItem->key = key; //assign the key
newItem->value = value; //assign the value
sizeList++; //increment size
head = newItem; //set the HEAD of the list to newItem
tail = newItem; //set the TAIL of the list to newItem
newItem->prev = head; //previous item is the head (itself)
newItem->next = head; //next item is the head (itself)
}
else //if the linked list is not empty, add it in
{
struct MapItem<keyType, valueType> *temp = head; //store the first element in the linked list in temp variable
if(sizeList == 1) //if there is only one element in the list, check if they equal eachother
{
if(head->key != key)
{
newItem = new MapItem<keyType, valueType>; //dynamically allocate a new item on the heap
newItem->key = key; //assign the key
newItem->value = value; //assign the value
tail = newItem; //assign newItem as the TAIL
head->next = tail; //assign the next of HEAD to the new map item
head->prev = tail; //assign the previous of HEAD as the newItem (tail)
tail->prev = head; //assign head to PREV of newItem (tail)
tail->next = head; //assign HEAD to NEXT of newItem (tail)
sizeList++; //increment size of list
}
}
else
{
bool sameKey = false; //boolean value to check if the same key already exists, and if it does it will stop the loop
int i = 1; //which item we are looking at in the list
while(i <= sizeList && !sameKey) //while not past the end of the list, keep checking if a similar key exists
{
if(temp->key == key)
sameKey = true;
else
{
temp = temp->next; //go to the next map item
i++;
}
}
if(!sameKey) //if the same key has not been found
{
newItem = new MapItem<keyType, valueType>; //dynamically allocate a new item on the heap
newItem->key = key; //assign the key
newItem->value = value; //assign the value
tail->next = newItem;
newItem->prev = tail;
newItem->next = head;
tail = newItem;
head->prev = tail;
sizeList++;
}
}
}
}
TEST.CPP
#include "Map.cpp"
int main()
{
Map<int, int> b;
b.add(1, 1); //if this line is commented out with the destructor intact, then the copy constructor call below works. if this line is NOT commented out with the destructor intact, the program crashes.
Map<int, int> a(b);
system("PAUSE");
return 0;
}
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) – NPE 2014-09-27 19:17:07
我也有一个赋值操作符,因为它没有被使用,所以我只是没有将它包含在这段代码中。 – Tim 2014-09-27 19:21:59