#在创建模板类时遇到的问题,我会先用框架写出代码结构,但是为了方便找问题,我还是会提供源码(虽然知道有点烂)
template<class T>
class A
{
private: 数据成员这里不介绍了
public:
1.类的各种构造函数
2.类的复制构造函数A(A&ano);
3.一个函数记作func1
4.一个函数记作func2
}
template<class T>
A<T>::A(A<T>&ano){};
template<class T>
A<T> A<T>::func1(){A<T> example; return example}
template<>
void A<Int>::func2(){A<int> a=func1();}//这里出现错误,说A<int>没有合适的赋值构造函数
源码
#pragma once
#define EXPAND_ONCE 100
#include <iostream>
#include <sstream>
#include<cstring>
using namespace std;
struct single
{
int coe;//系数
int ind;//指数
single(int Coe = 0, int Ind = 0) :coe(Coe), ind(Ind) {}
bool operator>(const single& other) const
{
return ind > other.ind;
}
bool operator<(const single& other) const
{
return ind < other.ind;
}
};
template<class T>
class MyLinearList
{
private:
int len;//有效元素数
int innersize;//内部数组长度
public:
T* first;
T* last;
bool sorted = false;
MyLinearList(int num = EXPAND_ONCE);
MyLinearList(T arr[], int size);
MyLinearList(MyLinearList<T>& rhs);
int size();
void expand(const int& size);
void append(MyLinearList<T>& rhs);
void push_back(T& ele);
void Quick_Sort(T* arr, int begin, int end);
void sort();
MyLinearList merge(MyLinearList<T>& ano);
MyLinearList operator+(MyLinearList<T>& ano);
string getstr();
~MyLinearList() { delete[]first; }
};
template<class T>
MyLinearList<T>::MyLinearList(int num)
{
first = new T[num + 1];
last = first;
len = 0;
innersize = num + 1;
}
template<class T>
MyLinearList<T>::MyLinearList(T arr[], int size)
{
if (size <= 0)throw "out_of_range";
first = new T[size + 1];
for (int i = 0; i < size; ++i)
{
first[i] = arr[i];
}
last = first + size;
len = size;
innersize = size + 1;
}
template<class T>
MyLinearList<T>::MyLinearList(MyLinearList<T>& rhs)
{
if (this == &rhs)
return;
first = new T[rhs.size() + 1];
memcpy(first, rhs.first, sizeof(T) * rhs.size());
last = first + rhs.size();
len = rhs.size();
innersize = len + 1;
sorted = rhs.sorted;
}
template<class T>
int MyLinearList<T>::size()
{
return len;
}
template<class T>
void MyLinearList<T>::expand(const int& size)
{
if (len + size <= innersize - 1) return;
int nowinner = innersize;
while (len + size > nowinner - 1)
nowinner += EXPAND_ONCE;
T* newfirst = new T[nowinner];
memmove(newfirst, first, sizeof(T) * len);
delete[]first;
first = newfirst;
last = first + len;
innersize = nowinner;
}
template<class T>
void MyLinearList<T>::append(MyLinearList<T>& rhs)
{
int addsize = rhs.size();
expand(addsize);//扩增空间
memmove(last, rhs.first, sizeof(T) * addsize);
last += addsize;
len += addsize;
}
template<class T>
void MyLinearList<T>::push_back(T& ele)
{
expand(1);
*last = ele;
++last;
++len;
}
template<class T>
void MyLinearList<T>::Quick_Sort(T* arr, int begin, int end)
{
if (begin > end)
return;
T tmp = *(arr + begin);
int i = begin;
int j = end;
while (i != j) {
while (!(*(arr + j) < tmp) && j > i)
j--;
while (!(*(arr + i) > tmp) && j > i)
i++;
if (j > i) {
T t = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = t;
}
}
*(arr + begin) = *(arr + i);
*(arr + i) = tmp;
Quick_Sort(arr, begin, i - 1);
Quick_Sort(arr, i + 1, end);
}
template<class T>
void MyLinearList<T>::sort()
{
Quick_Sort(first, 0, len - 1);
sorted = true;
}
template<class T>
MyLinearList<T> MyLinearList<T>::merge(MyLinearList<T>& ano)
{
MyLinearList<T> merged = *this;
merged.append(ano);
merged.sort();
return merged;
}
template<class T>
MyLinearList<T> MyLinearList<T>::operator+(MyLinearList<T>& ano) { }
template<>
MyLinearList<single> MyLinearList<single>::operator+(MyLinearList<single>& ano)
{
MyLinearList<single> merged = merge(ano); //这里就是对应出错的地方!!!
MyLinearList<single> plused;
single* anopoin = merged.first;
single* baby = new single;
*baby = *anopoin;
int nowind = anopoin->ind;
for (int i = 0; i < ano.size(); ++i)
{
if ((anopoin + i)->ind == baby->ind)
baby->coe += (anopoin + i)->coe;
else
{
if (baby->coe != 0)
plused.push_back(*baby);
baby->ind = (anopoin + i)->ind;
baby->coe = (anopoin + i)->ind;
}
}
if (baby->coe != 0)
plused.push_back(*baby);
delete baby;
return plused;
}
template<>
string MyLinearList<single>::getstr()
{
string str;
stringstream ss;
single* tmp = first;
if (tmp->coe < 0)
{
if (tmp->coe == -1)ss << '-';
else ss << tmp->coe;
}
else
{
if (tmp->coe != 1)ss << tmp->coe;
}
ss << 'x' << '^' << tmp->ind;
//在处理好第一个元素后开始进入循环
for (++tmp; tmp != last; ++tmp)
{
if (tmp->coe < 0)
{
if (tmp->coe == -1)ss << '-';
else ss << tmp->coe;
}
else
{
ss << "+";
if (tmp->coe != 1)ss << tmp->coe;
}
ss << 'x' << '^' << tmp->ind;
}
//不要现在删除tmp,现在delete tmp;但是tem==first,现在delete了,数据也不复存在了
getline(ss, str);
return str;
}