使用Linux编译器时出现错误但失去意义



我使用代码块构建了我的程序,但对于学校来说,我们应该通过linux系统进行编译。我在ibut下面有很多错误,我有149的问题。我不知道它在抱怨什么。也许有人能帮我?

In file included from matrix.cpp:9:0:
matrixClass.h: In member function âT Matrix<T>::GetData(int, int) const [with T = int]â:
matrixClass.h:149:17:   instantiated from âstd::ostream& operator<<(std::ostream&, const Matrix<int>&)â
matrix.cpp:22:13:   instantiated from here
matrixClass.h:131:16: warning: converting to non-pointer type âintâ from NULL

我的代码在下面。

T GetData(int row, int column) const
{
    if (row>=0 && row<numrows() && column>=0 && column<numcols())
    {
        return pData[GetRawIndex(row, column)];
    }
    return NULL;
}
//Output matrix arrays here.
friend ostream& operator<<(ostream& os, const Matrix<T>& matrix)
{
    os << "[";
    for(int i = 0; i<matrix.numrows(); i++)
    {
        for(int j = 0; j < matrix.numcols(); j++)
            os << matrix.GetData(i,j) << " ";
        os << endl;
    }
    os << "]" <<endl;
    return os;
}

没有错误。这只是一个警告。线路告诉你:

  1. 哪个文件包含带有警告的文件
  2. 发生警告的模板函数
  3. 在哪个函数中实例化了该模板函数
  4. 实例化发生的行
  5. 警告本身

当函数的返回类型为intT = int)时,警告会告诉您返回NULL。虽然NULL只给了你0,但编译器很清楚NULL只应该与指针一起使用,并警告你可能做错了什么。

首先,代码是正确的,尽管可能不是你真正的意思,这就是编译器警告你的原因。

在C++中,NULL被定义为0(整数0),因此在Matrix<int>的实例化中,如果用户试图访问越界的元素,则会返回0(整数值0)。NULL用于指示不引用有效内存的指针,编译器在返回语句中看到您正在使用该指针。。。所以它想知道你是真的想返回指针还是值0…

这就引出了一个问题,为什么要返回NULL?你真的打算返回0吗因为如果你不这样做,编译器只会帮你找到一个错误。。。

相关内容

最新更新