从 T 转换为无符号 int,可能会丢失数据
Conversion from T to unsigned int, possible loss of data
我正在尝试构建一个动态数组模板类,到目前为止一切都进展顺利,直到我尝试使用双精度类型的新对象实例,它抛出错误如下:
警告 C4244"正在初始化":从"T"转换为"无符号 int",可能会丢失数据
和
错误 C2108 下标不是整数类型
我的问题挂在矢量(T aSize(函数中
我环顾四周,我可以理解这些错误,但尝试应用修复程序似乎不起作用,任何帮助都会很棒。
#include "pch.h"
#include <iostream>
#include "Vector.h"
int main()
{
std::cout << "Hello World!n";
Vector<int> test = Vector<int>(5);
Vector<double> test2 = Vector<double>(10);
test.insert(1, 8);
test.insert(3, 22);
test.getPosition(0);
test.getSize();
test.print();
test[2] = 4;
test[10] = 0;
test.print();
test2.insert(0, 50.0);
}
#include <iostream>
#include <string>
using namespace std;
template <class T>
class Vector
{
public:
Vector();
Vector(T aSize);
~Vector();
int getSize();
int getPosition(T position);
void print() const;
void insert(T position, T value);
void resize(int newSize);
int &operator[](int index);
private:
T size;
int vLength;
T* data;
};
template <class T>
Vector<T>::Vector()
{
Vector::Vector(vLength);
}
template <class T>
Vector<T>::~Vector()
{
delete[] data;
}
template <class T>
Vector<T>::Vector(T aSize)
{
size = aSize;
data = new T[size];
for (T i = 0; i < size; i++)
{
data[i] = 0;
}
}
template <class T>
void Vector<T>::print() const
{
for (int i = 0; i < size; i++)
{
cout << data[i] << endl;
}
}
template <class T>
int Vector<T>::getPosition(T position)
{
return data[position];
}
template <class T>
void Vector<T>::insert(T position, T value)
{
data[position] = value;
cout << "value: " << value << " was inserted into position: " << position << endl;
}
template <class T>
int Vector<T>::getSize()
{
cout << "the array size is: " << size << endl;
return size;
}
template <class T>
int &Vector<T>::Vector::operator[](int index)
{
if ((index - 1) > size) {
resize(index + 1);
}
return data[index];
}
template <class T>
void Vector<T>::resize(int newSize)
{
T *temp;
temp = new T[newSize];
for (int i = 0; i < newSize; i++)
{
temp[i] = data[i];
}
delete[] data;
data = temp;
size = newSize;
cout << "the array was resized to: " << newSize << endl;
}
不应使用泛型类型T
来设置动态数组的大小。此外,您不应使用 T
循环遍历容器。你应该改用整型(int
、long
等(。
相关文章:
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- PyTorch C++将数据转换为张量失败
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 一种将 Dart 中的字节数据转换为 C++ 中的无符号字符*的有效方法?
- 将整数的二进制数据转换为浮点数
- 更改CV_32FC1以CV_64FC1导致不正确的数据转换
- 将JPEG CHAR数据转换为OpenCV垫子
- C++:如何将 32 位数据转换为有符号整数
- 如何将JSON文件中的数据转换为值(double,int ..)
- 如何将 boost::beast 中的序列化数据转换为字符串,以便我可以以 FIFO 方式处理它?
- 将字符串中的二进制数据转换为char
- 如何将提取的字符串数据转换为字符数组
- 如何将XML节点数据转换为QT中的字符串
- Qt:模型/视图框架上的数据转换
- 将Kinect数据转换为OpenCV矩阵
- C - 将UINT8_T*将图像数据转换为双**图像数据
- 数据转换解决方案
- 将点云2数据转换为opencv图像
- 如何将数组中的数据转换为字符串 c++
- C++目录树数据转换为矢量