我创建了一个类,它创建了一个 int
数组并将它们存储为一个 vector 。我已经全部正常工作,但后来我不得不将它变成一个模板,以便我可以存储的不仅仅是 int
。我收到以下错误,但不确定如何解决它们:
- 下标不是整数类型(myvector.h 第 70 行)
- 这是一行 (vectorArray[vectorSize] = n;)
- 'initializing':从'double'到'unsigned int'的转换,可能丢失数据(myvector.h 第 88 行)
- 哪一行写着 (T *tempArray = new T[newCapacity];)
- 下标不是整数类型 porj08 (myvector.h line 98)
- 哪一行写着 (tempArray[i] = vectorArray[i];)
- 'initializing':从'double'到'unsigned int'的转换,可能丢失数据(myvector.h 第 102 行)
- 哪一行写着 (vectorArray = new T[newCapacity];)
- 下标不是整数类型 porj08(myvector.h 第 113 行)
- 哪一行写着 (vectorArray[vectorSize] = n;)
它显然与 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;
}
最佳答案
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
作为循环条件。这应该涵盖所有内容。
关于c++ - 将数组类切换为模板时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860894/