我正在使用模板
制作通用矢量类我几乎完成了它们
但是这个 vector.push_back(element); 行让构造函数告诉我这是不明确的。
编辑:好的,所以我把整个代码
问题如下:
(222):注意:没有构造函数可以采用源类型,或者构造函数重载决策是不明确的(我把它放在那行前面的push_back函数(222))
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
template <typename T>
class BasicVector
{
private:
int vector_size;
int vector_capacity;
T* data;
void resize();
public:
BasicVector() {};
BasicVector(int);
~BasicVector();
T& at(int);
T& operator[](int);
T& front();
T& back();
void push_back(T);
void insert(int, T);
void pop_back();
int size();
int capacity();
void print();
};
template <typename T>
void BasicVector<T>::resize()
{
T* temp = data;
T* newDataCap = new T[vector_capacity * 2];
this->data = newDataCap;
for (int i = 0; i < vector_size; i++)
{
data[i] = temp[i];
}
vector_capacity *= 2;
delete[] temp;
}
template <typename T>
BasicVector<T>::BasicVector(int capacity)
{
if (capacity < 16)
{
capacity = 16;
}
else if (capacity >= 16 && capacity % 2 == 1)
{
capacity = capacity + 1;
}
vector_capacity = capacity;
data = new T[vector_capacity];
vector_size = 0;
}
template <typename T>
BasicVector<T>::~BasicVector()
{
for (int i = 0; i < vector_size; i++)
{
data[i] = 0;
}
delete[] data;
}
template <typename T>
T& BasicVector<T>::at(int elemNumber)
{
if (elemNumber > vector_size)
{
exit(1);
}
else
{
return data[elemNumber];
}
}
template <typename T>
T& BasicVector<T>::operator[](int elemNumber)
{
if (elemNumber > vector_size)
{
exit(1);
}
else
{
return data[elemNumber];
}
}
template <typename T>
T& BasicVector<T>::front()
{
return data[0];
}
template <typename T>
T& BasicVector<T>::back()
{
return data[vector_size];
}
template <typename T>
void BasicVector<T>::push_back(T element)
{
if (vector_size == vector_capacity)
{
resize();
}
data[vector_size] = element;
vector_size++;
}
template <typename T>
void BasicVector<T>::insert(int elemNum, T element)
{
if (vector_size == vector_capacity)
{
resize();
}
int* temp = new int[vector_size];
for (int i = 0; i < vector_size; i++)
{
temp[i] = data[elemNum + i - 1];
}
for (int j = 0; j < vector_size; j++)
{
data[elemNum + j] = temp[j];
}
data[elemNum - 1] = element;
vector_size++;
delete[] temp;
}
template <typename T>
void BasicVector<T>::pop_back()
{
data[vector_size] = 0;
vector_size--;
}
template <typename T>
int BasicVector<T>::size()
{
return vector_size;
}
template <typename T>
int BasicVector<T>::capacity()
{
return vector_capacity;
}
template <typename T>
void BasicVector<T>::print()
{
cout << "elements(" << vector_size << "):";
for (int i = 0; i < vector_size; i++)
{
cout << " " << data[i];
}
cout << endl;
}
template <typename T>
void theCoolFunction(BasicVector<T> &vector)
{
cout << "Now accepting commands (quit to exit program): "<< endl;
string choice;
while (choice != "quit")
{
cin >> choice;
if (choice == "get")
{
int elemNum;
cin >> elemNum;
cout << vector[elemNum] << endl;
}
else if (choice == "at")
{
int elemNum;
cin >> elemNum;
cout << vector.at(elemNum) << endl;
}
else if (choice == "front")
{
cout << "The first element of the vector is: " << vector.front() << endl;
}
else if (choice == "back")
{
cout << "The last element of the vector is: " << vector.back() << endl;
}
else if (choice == "insert")
{
int elemNum, element;
cin >> elemNum >> element;
vector.insert(elemNum, element);
}
else if (choice == "push")
{
int element;
cin >> element;
(222)vector.push_back(element);
}
else if (choice == "pop")
{
vector.pop_back();
}
else if (choice == "size")
{
cout << vector.size() << endl;
}
else if (choice == "capacity")
{
cout << vector.capacity() << endl;
}
else if (choice == "print")
{
vector.print();
}
else if (choice == "quit")
{
cout << "Exiting Program." << endl;
exit(0);
}
else
{
cout << "invalid input" << endl;
}
}
}
int main()
{
int dataType;
cout << "Specify what data type to store in vector:" << endl;
cout << "1) int" << endl;
cout << "2) float" << endl;
cout << "3) dou! ble" << endl;
cout << "4) string" << endl;
cout << "5) bool" << endl;
cin >> dataType;
int capacity;
cout << "Enter starting capacity of vector: ";
cin >> capacity;
cout << endl;
if (dataType == 1)
{
BasicVector<int>vector(capacity);
theCoolFunction<int>(vector);
}
else if (dataType == 2)
{
BasicVector<float>vector(capacity);
theCoolFunction<float>(vector);
}
else if (dataType == 3)
{
BasicVector<double>vector(capacity);
theCoolFunction<double>(vector);
}
else if (dataType == 4)
{
BasicVector<string>vector(capacity);
theCoolFunction<string>(vector);
}
else if (dataType == 5)
{
BasicVector<bool>vector(capacity);
theCoolFunction<bool>(vector);
}
return 0;
}