请问我这段代码这个报错怎么解决?
这是全部的代码:
#include
#include
#include // 引入 std::out_of_range 异常
template<typename T>
class MyVector {
public:
MyVector() : data(nullptr), size(0), capacity(0) {}
// 复制构造函数
MyVector(const MyVector& x) : data(nullptr), size(0), capacity(0) {
*this = x;
}
~MyVector() {
clear();
}
// 赋值运算符
MyVector& operator=(const MyVector& x) {
if (this == &x) return *this;
clear();
// 复制数据
capacity = x.capacity;
size = x.size;
data = new T[capacity];
for (unsigned i = 0; i < size; i++) {
data[i] = x.data[i];
}
return *this;
}
// 向量拼接
MyVector operator+(const MyVector& x) const {
MyVector result(*this);
for (unsigned i = 0; i < x.size; i++) {
result.push_back(x.data[i]);
}
return result;
}
// 输出流运算符
friend std::ostream& operator<<(std::ostream& out, const MyVector& x) {
for (unsigned i = 0; i < x.size; i++) {
out << x.data[i] << " ";
}
return out;
}
// 下标运算符
T& operator[](unsigned idx) {
return data[idx];
}
// 清除向量
void clear() {
delete[] data;
data = nullptr;
size = 0;
capacity = 0;
}
// 在尾部插入元素
void push_back(const T& t) {
if (size == capacity) {
reserve(capacity + 16);
}
data[size++] = t;
}
// 弹出尾部元素
void pop_back() {
if (size > 0) {
size--;
}
}
// 访问元素
T& at(unsigned idx) {
if (idx < size) {
return data[idx];
}
// 如果索引越界,抛出异常
throw std::out_of_range("Index out of range");
}
// 获取向量大小
unsigned int getSize() const {
return size;
}
// 获取向量容量
unsigned int getCapacity() const {
return capacity;
}
// 判断向量是否为空
bool empty() {
return (size == 0);
}
// 访问首元素
T& front() {
return data[0];
}
// 访问尾元素
T& back() {
return data[size - 1];
}
// 在指定位置插入元素
bool insert(unsigned pos, const T& t) {
if (pos > size) {
return false;
}
if (size == capacity) {
reserve(capacity + 16);
}
for (unsigned i = size; i > pos; i--) {
data[i] = data[i - 1];
}
data[pos] = t;
size++;
return true;
}
// 删除指定位置的元素
bool erase(unsigned pos) {
if (pos >= size) {
return false;
}
for (unsigned i = pos; i < size - 1; i++) {
data[i] = data[i + 1];
}
size--;
return true;
}
private:
// 重新分配内存
void reserve(unsigned newCapacity) {
if (newCapacity <= capacity) {
return;
}
T* newData = new T[newCapacity];
if (data) {
for (unsigned i = 0; i < size; i++) {
newData[i] = data[i];
}
delete[] data;
}
data = newData;
capacity = newCapacity;
}
private:
T* data;
unsigned size;
unsigned capacity;
};
// 自定义类型 Student
class Student {
public:
Student(const std::string& id, const std::string& name,
const std::string& schoolName, double discountRate)
: id(id), name(name), schoolName(schoolName), discountRate(discountRate) {}
std::string id;
std::string name;
std::string schoolName;
double discountRate;
};
// 重载输出流运算符
std::ostream& operator<<(std::ostream& out, const Student& s) {
out << "id: " << s.id << ", name: " << s.name
<< ", schoolName: " << s.schoolName
<< ", discountRate: " << s.discountRate;
return out;
}
// 测试函数
void test() {
// int 类型实例
MyVector<int> intVec;
for (int i = 0; i < 10; i++) {
intVec.push_back(i);
}
std::cout << intVec << std::endl;
// double 类型实例
MyVector<double> doubleVec;
for (int i = 0; i < 10; i++) {
doubleVec.push_back(i * 1.5);
}
std::cout << doubleVec << std::endl;
// 自定义类型 Student 实例
MyVector studentVec;
studentVec.push_back(Student("1001", "Tom", "PKU", 0.8));
studentVec.push_back(Student("1002", "Jerry", "THU", 0.9));
studentVec.push_back(Student("1003", "Jimmy", "Fudan", 0.7));
std::cout << studentVec << std::endl;
}
int main() {
test();
return 0;
}
报错在148行,有截图
给student写一个无参的构造函数。
编译报错不都说了吗 Student没有默认构造函数
不知道你这个问题是否已经解决, 如果还没有解决的话:#include<stdio.h>
#include<stdlib.h> //头文件
#define MaxSize 60 //线性表存储空间的大小
typedef char ElemType; //自定义类型语句
typedef struct{ //线性表的顺序存储表示
ElemType data[MaxSize]; //存储线性中的元素
int length; //存放线性表的长度
}SqList; //线性表顺序存储结构类型名
void CreatList_Sq(SqList *&L,ElemType a[],int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList)); //分配存放线性的空间
for(i=0;i<n;i++)
L->data[i]=a[i];
L->length=n; //令线性表L的长度为n
}
/*bool ListEmpty(SqList *L) //判断是否为空表
{
return(L->length==0);
}*/
void DispList(SqList *L) //输出线性表
{ int i;
/*if (ListEmpty(L))
return;*/
for (i=0;i<L->length;i++)
printf("%d",L->data[i]);
printf("\n");
}
int main()
{
SqList *L;
ElemType a[]={1,2,3,4,5,6,7,8};
CreatList_Sq(L,a,8);
printf("L:");
DispList(L);
return 0;
}