我正在尝试从头开始构建一个 C++ 向量来完成作业,但我一整天都在挣扎。我的追加功能坏了。起初它不需要字符串输入,但我已经让它将一个字符串附加到动态列表中。但是,当我尝试将第二个字符串附加到列表中时,临时数组会附加该字符串,但是当我第二次删除现有数据属性数组(前一个临时数组)时,会出现分段错误。我已经包含了很多打印语句来指出这一点。
我尝试使用placement new 实例化一个对象块,因为字符串是对象。这是 DynamicList class.cpp 文件:
#include <iostream>
#include <string>
#include "DynamicList.hpp"
#include <algorithm>
//Constructor
template<typename T>
DynamicList<T>::DynamicList(){
size = 0;
data = new T[size];
}
//Destructor
template<typename T>
DynamicList<T>::~DynamicList(){
std::cout << "Deleting array of size: " << getSize() << std::endl;
delete[] data;
}
//Overloading operators
template<typename T>
DynamicList<T> DynamicList<T>::operator=(DynamicList<T>& obj){
obj.size = size;
std::copy(data, data + size, obj.data);
}
template<typename T>
bool DynamicList<T>::operator==(DynamicList<T>& obj){
if(size != obj.size){
return false;
}
else{
for(int i = 0; i < size; i++){
if(obj.data[i] != data[i]){
return false;
}
}
return true;
}
}
template<typename T>
bool DynamicList<T>::operator!=(DynamicList<T>& obj){
return !(DynamicList<T>::operator==(obj));
}
template<typename T>
T DynamicList<T>::operator[](int index){
for(int i = 0; i < size; i++){
if(i == index){
return data[i];
}
}
}
//Returns the size of the list
template<typename T>
int DynamicList<T>::getSize(){
return size;
}
//Add element to the end of list.
template<typename T>
bool DynamicList<T>::append(T item){
std::cout << data << std::endl;
std::cout << item << std::endl;
T* temp = static_cast<T*>(new T(sizeof(T) * (size + 1), alignof(T)));
if(size > 1){
for(int i = 0; i < size; i++){
T* somePtr = new (&temp[i]) T;
*somePtr = data[i];
}
}
T* itemPtr = new (&temp[size]) T;
*itemPtr = item;
std::cout << "Ptr made in array slot.\n";
std::cout << temp << std::endl;
size++;
std::cout << "Size increased.\n";
std::cout << data << std::endl;
std::cout << "AAAAAA\n";
delete[] data;
std::cout << "BBBBB\n";
data = temp;
std::cout << data[size - 1] << std::endl;
return true;
}
这是DynamicList的头文件:
#include <iostream>
#include <string>
#ifndef DYNAMICLIST_H
#define DYNAMICLIST_H
template<typename T>
class DynamicList{
private:
int size;
T* data;
public:
//constructor
DynamicList();
//destructor
~DynamicList();
//Operator overloaders
DynamicList operator=(DynamicList& obj);
bool operator==(DynamicList& obj);
bool operator!=(DynamicList& obj);
T operator[](int index);
//Returns the size of the data array
int getSize();
//Add an item to the end of the list
bool append(T item);
//Remove an item at an index
bool remove(int index);
};
#endif
这是主要文件:
#include <iostream>
#include <string>
#include "DynamicList.hpp"
#include "DynamicList.cpp"
int main(){
DynamicList<std::string> stringList;
stringList.append("test");
stringList.append("test2");
return 0;
}
这是 makefile:
all: DynamicList.hpp DynamicList.cpp main.cpp
g++ -c DynamicList.cpp
g++ -c main.cpp
g++ DynamicList.o main.o -o exec
run: all
./exec
clean:
rm -f *.o
rm -f exec
这是错误:
./exec
0x55c688824eb8
test
Ptr made in array slot.
0x55c6888252f0
Size increased.
0x55c688824eb8
AAAAAA
BBBBB
test
0x55c6888252f0
test2
Ptr made in array slot.
0x55c688824eb0
Size increased.
0x55c6888252f0
AAAAAA
make: *** [makefile:8: run] Segmentation fault (core dumped)
构造函数使用数组
new[]
创建数组,但追加函数使用对象 new
代替。使用数组 new
删除由对象 delete[]
创建的对象,反之亦然是未定义的行为。
您应该将
new
中的对象 append
替换为数组 new[]
:
template<typename T>
bool DynamicList<T>::append(T item) {
T* temp = new T[size + 1];
for (int i = 0; i < size; i++) {
temp[i] = data[i];
}
temp[size] = item;
size++;
delete[] data;
data = temp;
return true;
}