错误:将'long int(*)[4]'分配给'long int [4] [4]'时类型不兼容 [英] error: incompatible types in assignment of 'long int (*)[4]' to 'long int [4][4]'

查看:62
本文介绍了错误:将'long int(*)[4]'分配给'long int [4] [4]'时类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建自己的Matrix类型,该类型作用于具有多维数组的标准C矩阵行.到目前为止,这是我的实现:

I am trying to build my own Matrix type which acts line an standard C matrix with multidimensional arrays. So far, this is my implementation:

#include <iostream>

/**
 * To build it use:
 *     g++ -std=c++11 test_template_initicialization.cpp -o main
 */
template <int width, int height>
struct Matrix
{
  long int _data[height][width];

  Matrix()
  {
  }

  Matrix(long int matrix[height][width]) : _data(matrix)
  {
  }

  /**
   * Overloads the `[]` array access operator, allowing you to access this class objects as the
   * where usual `C` arrays.
   *
   * @param  line the current line you want to access
   * @return      a pointer to the current line
   */
  long int* operator[](int line)
  {
    return this->_data[line];
  }

  /**
   * Prints a more beauty version of the matrix when called on `std::cout<< matrix << std::end;`
   */
  friend std::ostream &operator<<( std::ostream &output, const Matrix &matrix )
  {
    int i, j;

    for( i=0; i < height; i++ )
    {
      for( j=0; j < width; j++ )
      {
        output << matrix._data[i][j] << ", ";
      }

      output << matrix._data[i][j] << "\n";
    }
    return output;
  }
};

/**
 * C++ Matrix Class
 * https://stackoverflow.com/questions/2076624/c-matrix-class
 */
int main (int argc, char *argv[])
{
  Matrix<3, 3> matrix;
  std::cout << matrix << std::endl;

  matrix[0][0] = 911;
  std::cout << matrix << std::endl;

  std::cout << matrix[0] << std::endl;
  std::cout << matrix[0][0] << std::endl;

  Matrix<4,4> matrix2 = { 0 };
}

构建最后一个示例Matrix<4,4> matrix2 = { 0 };时,出现类型不兼容错误:

When build the last example Matrix<4,4> matrix2 = { 0 }; I am getting incompatible types error:

D:\test_template_initicialization.cpp: In instantiation of 'Matrix<width, height>::Matrix(long int (*)[width]) [with int width = 4; int height = 4]':
D:\test_template_initicialization.cpp:67:29:   required from here
D:\test_template_initicialization.cpp:16:56: error: incompatible types in assignment of 'long int (*)[4]' to 'long int [4][4]'
   Matrix(long int matrix[height][width]) : _data(matrix)

错误的主要部分是'long int (*)[4]' to 'long int [4][4]'. long int (*)[4]来自matrix2 = { 0 };,而long int [4][4]是我的标准类模板声明long int _data[height][width];.

The main part of the error is 'long int (*)[4]' to 'long int [4][4]'. The long int (*)[4] is coming from the matrix2 = { 0 }; and the long int [4][4] is my standard class template declaration long int _data[height][width];.

我可以修复我的Matrix构造函数,以便它可以接受来自matrix2 = { 0 };初始化调用的long int (*)[4]吗?

Can I fix my Matrix constructor, so it can accept this long int (*)[4] coming from matrix2 = { 0 }; initialization call?

推荐答案

在以下链接的帮助下,我设法对其进行了修复:

I managed to fix it, with the help of the following links:

  1. http://en.cppreference.com/w/cpp/utility/initializer_list
  2. 从'int'到'int(*)的无效转换[3] 'c ++
  3. 使用多维std :: initializer_list
  4. 用括号括起来的初始化程序列表构造器
  5. C ++矩阵类
  6. java是否等效于python __call__?
  7. C ++函子-及其用途
  8. 返回函数中的数组
  9. std :: assert发生了什么
  10. 没有依赖模板的参数参数
  11. 可选模板参数
  1. http://en.cppreference.com/w/cpp/utility/initializer_list
  2. Invalid Conversion from 'int' to 'int(*)[3]' c++
  3. Using multidimensional std::initializer_list
  4. Brace-enclosed initializer list constructor
  5. C++ Matrix Class
  6. Does java have an equivalent to python __call__?
  7. C++ Functors - and their uses
  8. Return array in a function
  9. When using C headers in C++, should we use functions from std:: or the global namespace?
  10. What happened to std::assert
  11. There are no arguments that depend on a template parameter
  12. Optional Template parameter

固定代码:

#include <cassert>
#include <iostream>

template <unsigned int array_width, typename array_datatype=long int>
struct Array
{
  array_datatype _data[array_width];

  Array()
  {
  }

  Array(std::initializer_list< array_datatype > raw_data)
  {
    unsigned int data_size = raw_data.size();
    unsigned int column_index = 0;
    // std::cout << data_size << std::endl;

    if( data_size == 1 )
    {
      array_datatype initial = *(raw_data.begin());

      for( ; column_index < array_width; column_index++ )
      {
        this->_data[column_index] = initial;
      }
    }
    else
    {
      assert(data_size == array_width);

      for( auto column : raw_data )
      {
        this->_data[column_index] = column;
        column_index++;
      }
    }
  }

  /**
   * Overloads the `[]` array access operator, allowing you to access this class objects as the
   * where usual `C` arrays.
   *
   * @param  line the current line you want to access
   * @return      a pointer to the current line
   */
  array_datatype* operator[](int line)
  {
    return this->_data;
  }

  /**
   * Prints a more beauty version of the matrix when called on `std::cout<< matrix << std::end;`
   */
  friend std::ostream &operator<<( std::ostream &output, const Array &matrix )
  {
    unsigned int column;
    output << "{";

    for( column=0; column < array_width; column++ )
    {
      output << matrix._data[column];

      if( column != array_width-1 )
      {
        output << ", ";
      }
    }

    output << "}";
    return output;
  }
};


template <unsigned int matrix_width, unsigned int matrix_height, typename matrix_datatype=long int>
struct Matrix
{
  matrix_datatype _data[matrix_height][matrix_width];

  Matrix()
  {
  }

  Matrix(matrix_datatype initial)
  {
    unsigned int line;
    unsigned int column;

    for( line=0; line < matrix_height; line++ )
    {
      for( column=0; column < matrix_width; column++ )
      {
        this->_data[line][column] = initial;
      }
    }
  }

  Matrix(std::initializer_list< std::initializer_list< matrix_datatype > > raw_data)
  {
    // std::cout << raw_data.size() << std::endl;
    assert(raw_data.size() == matrix_height);

    // std::cout << raw_data.begin()->size() << std::endl;
    assert(raw_data.begin()->size() == matrix_width);

    unsigned int line_index = 0;
    unsigned int column_index;

    for( auto line : raw_data )
    {
      column_index = 0;

      for( auto column : line )
      {
        this->_data[line_index][column_index] = column;
        column_index++;
      }

      line_index++;
    }
  }

  /**
   * Overloads the `[]` array access operator, allowing you to access this class objects as the
   * where usual `C` arrays.
   *
   * @param  line the current line you want to access
   * @return      a pointer to the current line
   */
  matrix_datatype* operator[](int line)
  {
    return this->_data[line];
  }

  /**
   * Prints a more beauty version of the matrix when called on `std::cout<< matrix << std::end;`
   */
  friend std::ostream &operator<<( std::ostream &output, const Matrix &matrix )
  {
    unsigned int line;
    unsigned int column;
    output << "{";

    for( line=0; line < matrix_height; line++ )
    {
      output << "{";

      for( column=0; column < matrix_width; column++ )
      {
        output << matrix._data[line][column];

        if( column != matrix_width-1 )
        {
          output << ", ";
        }
      }

      if( line != matrix_height-1 )
      {
        output << "}, ";
      }
      else
      {
        output << "}";
      }
    }

    output << "}";
    return output;
  }
};


void array_tests()
{
  std::cout << "Array tests" << std::endl;
  Array<3, long int> array;

  std::cout << array << std::endl;
  std::cout << array[0] << std::endl;

  Array<3> array2 = {0,0,0};
  std::cout << "array2: " << array2 << std::endl;

  Array<3> array3 = {3};
  std::cout << "array3: " << array3 << std::endl;
}


void matrix_tests()
{
  std::cout << "Matrix tests" << std::endl;
  Matrix<3, 3, long int> matrix;
  std::cout << matrix << std::endl;

  matrix[0][0] = 911;
  std::cout << matrix << std::endl;

  std::cout << matrix[0] << std::endl;
  std::cout << matrix[0][0] << std::endl;

  Matrix<3, 3> matrix2{ {0,0,0}, {0,0,0}, {0,0,0} };
  std::cout << matrix2 << std::endl;

  Matrix<3, 3> matrix3 = { 3 };
  std::cout << matrix3 << std::endl;

  Matrix<3, 1, long int> matrix4 = { 4 };
  std::cout << matrix4 << std::endl;
}


/**
 * To build it use:
 *     g++ -std=c++11 main.cpp -o main
 */
int main (int argc, char *argv[])
{
  matrix_tests();
  std::cout << std::endl;
  array_tests();
}

运行此命令,您将得到:

Running this, you get:

Matrix tests
{{4200800, 0, -928274330}, {32765, 14354248, 0}, {-1499063668, 0, 24}}
{{911, 0, -928274330}, {32765, 14354248, 0}, {-1499063668, 0, 24}}
0x65fde0
911
{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
{{3, 3, 3}, {3, 3, 3}, {3, 3, 3}}
{{4, 4, 4}}

Array tests
{0, 1875566066, 0}
0x65fdf4
array2: {0, 0, 0}
array3: {3, 3, 3}

这篇关于错误:将'long int(*)[4]'分配给'long int [4] [4]'时类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆