我创建了一个创建int
数组的类,并将它们存储为矢量。我把它全部工作了,但后来我不得不把它变成一个模板,以便我可以存储的不只是int
。我收到以下错误,不知道如何解决它们:
这显然与vectorArray有关但我无法弄清楚我做错了什么。
MyVector.h
#pragma once
#include <iostream>
#include "stdafx.h"
using namespace std;
template <class T>
class MyVector
{
private:
T vectorSize;
T vectorCapacity;
T *vectorArray;
public:
MyVector() {
vectorArray = new T[10];
}
T size();
T capacity();
void clear();
void push_back(T n);
T at(T n);
friend ostream& operator<<(ostream& os, MyVector vt);
MyVector operator=(MyVector&);
};
/*
* TEMPLATE FUNCTIONS
*/
//Return array size
template<class T>
T MyVector<T>::size()
{
return vectorSize;
}
// Return array capacity
template<class T>
T MyVector<T>::capacity()
{
return vectorCapacity;
}
// clear array values
template<class T>
void MyVector<T>::clear()
{
for (int i = 0; i < sizeof(vectorArray); i++)
{
vectorArray[i] = '\0';
}
vectorSize = 0;
vectorCapacity = 2;
}
// Add number to array and double array size if needed
template<class T>
void MyVector<T>::push_back(T n)
{
int test = 100;
if (vectorCapacity > vectorSize)
{
vectorArray[vectorSize] = n;
vectorSize++;
}
else {
if (vectorCapacity == 0) {
vectorArray = new T[4];
vectorArray[0] = n;
vectorCapacity = 4;
vectorSize++;
}
else {
T newCapacity = vectorCapacity * 2;
// Dynamically allocate a new array of integers what is somewhat larger than the existing array.An algorithm that is often used is to double the size of the array.
T *tempArray = new T[newCapacity];
// Change capacity to be the capacity of the new array.
vectorCapacity = newCapacity;
// Copy all of the numbers from the first array into the second, in sequence.
for (T i = 0; i < MyVector::size(); i++)
{
tempArray[i] = vectorArray[i];
}
delete[] vectorArray;
vectorArray = new T[newCapacity];
for (int i = 0; i < MyVector::size(); i++)
{
vectorArray[i] = tempArray[i];
}
delete[] tempArray;
// Add the new element at the next open slot in the new array.
vectorArray[vectorSize] = n;
// Increment the size;
vectorSize++;
}
}
}
// Return Value and given point in array
template<class T>
T MyVector<T>::at(T n)
{
return vectorArray[n];
}
Main.cpp的
#include "stdafx.h"
#include <string>
#include "MyVector.h"
const double FRACTION = 0.5;
int main()
{
cout << "\nCreating a vector of doubles named Sam\n";
MyVector<double> sam;
cout << "\nPush 12 values into the vector.";
for (int i = 0; i < 12; i++)
sam.push_back(i + FRACTION);
cout << "\nHere is sam: ";
cout << sam;
cout << "\n---------------\n";
cout << "\nCreating an empty vector named joe";
MyVector<double> joe;
// test assignment
joe = sam;
cout << "\nHere is joe after doing an assignment:\n ";
cout << joe;
cout << "\n---------------\n";
// test the copy constructor
MyVector<double> bill = sam;
cout << "\nHere is bill after creating it using the copy constructor:\n ";
cout << bill;
cout << "\n---------------\n";
cout << endl;
system("PAUSE");
return 0;
}
答案 0 :(得分:3)
T vectorSize;
T vectorCapacity;
例如,如果T
为float
,则显然会float
。你不想要那个。这些成员的类型不应该依赖于元素的T
类型,而是将其设为std::size_t
,例如:
std::size_t vectorSize;
std::size_t vectorCapacity;
然后,beware sizeof(pointer)
(您在clear
中使用了一个),使用vectorSize
或vectorCapacity
代替循环条件。这应该涵盖所有。