【问题标题】:Conversion from T to unsigned int, possible loss of data从 T 转换为 unsigned int,可能会丢失数据
【发布时间】:2019-04-08 05:13:14
【问题描述】:

我正在尝试构建一个动态数组模板类,到目前为止一切都很好,直到我尝试使用 double 类型的新对象实例,它会引发如下错误:

警告 C4244 'initializing':从 'T' 转换为 'unsigned int',可能丢失数据

错误C2108下标不是整数类型

我的问题在于 Vector(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;
}

【问题讨论】:

  • 未来盔甲:熟悉Rule of Three and its friends
  • 好的,谢谢你
  • 在调整大小时,你应该去size 而不是newSize 来阅读data 超出范围。 getPosition 应该是:T Vector&lt;T&gt;::getPosition(int position),因为您返回的值包含在 data 中。

标签: c++ templates dynamic-arrays


【解决方案1】:

您不应该使用泛型类型T 来设置动态数组的大小。此外,您不应使用 T 循环通过容器。您应该改用整数类型(intlong 等)。

【讨论】:

  • 请记住,在您的类定义中,size 的类型仍然是 T,因此声明 size = aSize; 是不正确的。
  • @Getwrong 不要将T 用于任何索引。 T 是数据的类型。所有大小和索引都必须是整数。建议在编译和测试周期之间编写更少的代码。你会发现一旦你发现了一个错误,你就不太可能重蹈覆辙。 Lateo,我建议扩展答案以涵盖其他错误系列。
  • 乍一看,没看出有什么问题,所以我将代码复制到我的电脑上。
  • 现在代码中没有错误。我正在恢复编辑。
  • 你的代码对我来说编译得很好。您确定您仍然收到所需的错误吗?
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多