简体   繁体   English

呼叫 Function 但代码显示 C++ 中没有匹配的 function

[英]Calling a Function but code says no matching function in C++

This is my code and the error which comes is这是我的代码,出现的错误是

no matching function for call to to_reduced_row_echelon_form(float[((unsigned int)((int)r))][((unsigned int)((int)c))])' 
#include <algorithm> // for std::swap
#include <cstddef>
#include <cassert>

// Matrix traits: This describes how a matrix is accessed. By
// externalizing this information into a traits class, the same code
// can be used both with native arrays and matrix classes. To use the
// default implementation of the traits class, a matrix type has to
// provide the following definitions as members:
//
// * typedef ... index_type;
//   - The type used for indexing (e.g. size_t)
// * typedef ... value_type;
//   - The element type of the matrix (e.g. double)
// * index_type min_row() const;
//   - returns the minimal allowed row index
// * index_type max_row() const;
//   - returns the maximal allowed row index
// * index_type min_column() const;
//   - returns the minimal allowed column index
// * index_type max_column() const;
//   - returns the maximal allowed column index
// * value_type& operator()(index_type i, index_type k)
//   - returns a reference to the element i,k, where
//     min_row() <= i <= max_row()
//     min_column() <= k <= max_column()
// * value_type operator()(index_type i, index_type k) const
//   - returns the value of element i,k
//
// Note that the functions are all inline and simple, so the compiler
// should completely optimize them away.
template<typename MatrixType> struct matrix_traits
{
  typedef typename MatrixType::index_type index_type;
  typedef typename MatrixType::value_type value_type;
  static index_type min_row(MatrixType const& A)
  { return A.min_row(); }
  static index_type max_row(MatrixType const& A)
  { return A.max_row(); }
  static index_type min_column(MatrixType const& A)
  { return A.min_column(); }
  static index_type max_column(MatrixType const& A)
  { return A.max_column(); }
  static value_type& element(MatrixType& A, index_type i, index_type k)
  { return A(i,k); }
  static value_type element(MatrixType const& A, index_type i, index_type k)
  { return A(i,k); }
};

// specialization of the matrix traits for built-in two-dimensional
// arrays
template<typename T, std::size_t rows, std::size_t columns>
 struct matrix_traits<T[rows][columns]>
{
  typedef std::size_t index_type;
  typedef T value_type;
  static index_type min_row(T const (&)[rows][columns])
  { return 0; }
  static index_type max_row(T const (&)[rows][columns])
  { return rows-1; }
  static index_type min_column(T const (&)[rows][columns])
  { return 0; }
  static index_type max_column(T const (&)[rows][columns])
  { return columns-1; }
  static value_type& element(T (&A)[rows][columns],
                             index_type i, index_type k)
  { return A[i][k]; }
  static value_type element(T const (&A)[rows][columns],
                            index_type i, index_type k)
  { return A[i][k]; }
};

// Swap rows i and k of a matrix A
// Note that due to the reference, both dimensions are preserved for
// built-in arrays
template<typename MatrixType>
 void swap_rows(MatrixType& A,
                 typename matrix_traits<MatrixType>::index_type i,
                 typename matrix_traits<MatrixType>::index_type k)
{
  matrix_traits<MatrixType> mt;
  typedef typename matrix_traits<MatrixType>::index_type index_type;

  // check indices
  assert(mt.min_row(A) <= i);
  assert(i <= mt.max_row(A));

  assert(mt.min_row(A) <= k);
  assert(k <= mt.max_row(A));

  for (index_type col = mt.min_column(A); col <= mt.max_column(A); ++col)
    std::swap(mt.element(A, i, col), mt.element(A, k, col));
}

// divide row i of matrix A by v
template<typename MatrixType>
 void divide_row(MatrixType& A,
                  typename matrix_traits<MatrixType>::index_type i,
                  typename matrix_traits<MatrixType>::value_type v)
{
  matrix_traits<MatrixType> mt;
  typedef typename matrix_traits<MatrixType>::index_type index_type;

  assert(mt.min_row(A) <= i);
  assert(i <= mt.max_row(A));

  assert(v != 0);

  for (index_type col = mt.min_column(A); col <= mt.max_column(A); ++col)
    mt.element(A, i, col) /= v;
}

// in matrix A, add v times row k to row i
template<typename MatrixType>
 void add_multiple_row(MatrixType& A,
                  typename matrix_traits<MatrixType>::index_type i,
                  typename matrix_traits<MatrixType>::index_type k,
                  typename matrix_traits<MatrixType>::value_type v)
{
  matrix_traits<MatrixType> mt;
  typedef typename matrix_traits<MatrixType>::index_type index_type;

  assert(mt.min_row(A) <= i);
  assert(i <= mt.max_row(A));

  assert(mt.min_row(A) <= k);
  assert(k <= mt.max_row(A));

  for (index_type col = mt.min_column(A); col <= mt.max_column(A); ++col)
    mt.element(A, i, col) += v * mt.element(A, k, col);
}

// convert A to reduced row echelon form
template<typename MatrixType>
 void to_reduced_row_echelon_form(MatrixType& A)
{
  matrix_traits<MatrixType> mt;
  typedef typename matrix_traits<MatrixType>::index_type index_type;

  index_type lead = mt.min_row(A);

  for (index_type row = mt.min_row(A); row <= mt.max_row(A); ++row)
  {
    if (lead > mt.max_column(A))
      return;
    index_type i = row;
    while (mt.element(A, i, lead) == 0)
    {
      ++i;
      if (i > mt.max_row(A))
      {
        i = row;
        ++lead;
        if (lead > mt.max_column(A))
          return;
      }
    }
    swap_rows(A, i, row);
    divide_row(A, row, mt.element(A, row, lead));
    for (i = mt.min_row(A); i <= mt.max_row(A); ++i)
    {
      if (i != row)
        add_multiple_row(A, i, row, -mt.element(A, i, lead));
    }
  }
}

// test code
#include <iostream>
#include <vector>

using namespace std;
int main()
{ 
  int r=0,c=0,i,j;
  cout << "Enter no of rows of the matrix";
  cin >> r;
  cout << "Enter no of columns of the matrix";
  cin >> c;

  float l[r][c];

  int p = 0, q = 0;

  while (p < r) {
    while (q < c) {
      
      cin >> l[p][q];

      q = q + 1;
    }
    p = p + 1;
    q = 0;
  }
  to_reduced_row_echelon_form(l);

  for (int i = 0; i < r; ++i)
  {
    for (int j = 0; j < c; ++j)
      std::cout << l[i][j] << '\t';
    std::cout << "\n";
  }
  system("pause");
  return EXIT_SUCCESS;
}

calling the function whose code i took from https://rosettacode.org/wiki/Reduced_row_echelon_form调用 function 我从https 中获取的代码://rosettacode.org/wiki/Reduced_row_echelon_form

Variable length arrays (VLAs) like float l[r][c];可变长度 arrays (VLAs) like float l[r][c]; are not part of standard C++, so any attempt to reason when they might or might not work is fruitless.不是标准 C++ 的一部分,因此任何尝试推理它们何时可能或可能不起作用的尝试都是徒劳的。

The requirements of the type passed to to_reduced_row_echelon_form are clearly described in the comment starting 'Matrix traits'.传递给to_reduced_row_echelon_form的类型的要求在以“Matrix traits”开头的注释中有清楚的描述。 You need to define a class with those features.您需要定义具有这些功能的 class。 Something like this像这样的东西

class MyMatrix
{
public:
    MyMatrix(int r, int c) : r(r), c(c), l(r, std::vector<int>(c)) {}
    std::vector<int>& operator[](int r) { return l[r]; }
    const std::vector<int>& operator[](int r) const { return l[r]; }
    using index_type = int;
    using value_type = int;
    value_type& operator()(index_type i, index_type j) { return l[i][j]; }
    value_type operator()(index_type i, index_type j) const { return l[i][j]; }
    index_type min_row() const { return 0; }
    index_type max_row() const { return r - 1; }
    index_type min_column() const { return 0; }
    index_type max_column() const { return c - 1; }
private:
    int r, c;
    std::vector<std::vector<int>> l;
};

Not the most elegant or efficient code, but it does compile and hopefully works.不是最优雅或最高效的代码,但它确实可以编译并有望工作。

Then use it like this然后像这样使用它

MyMatrix l(r, c);
...
to_reduced_row_echelon_form(l);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM