【问题标题】:Can someone find the syntax error in this program有人能在这个程序中找到语法错​​误吗
【发布时间】:2020-04-09 10:20:32
【问题描述】:

我编写了一个 C++ 程序来检查矩阵是否稀疏。出现的语法错误如下:

main.cpp:56:8: error: deduced class type ‘matrix’ in function return type
 matrix matrix <T>::add(matrix r)
        ^~~~~~~~~~
main.cpp:11:7: note: ‘template class matrix’ declared here
 class matrix
       ^~~~~~
main.cpp:56:8: error: prototype for ‘matrix matrix::add(matrix)’ does not match any in class ‘matrix’
 matrix matrix <T>::add(matrix r)
        ^~~~~~~~~~
main.cpp:19:9: error: candidate is: matrix matrix::add(matrix&)
  matrix add(matrix&);
         ^~~*

程序是:

   #include<iostream>
    #include <exception>
    using namespace std;


    class mismatchDimension:public exception 
    {
     public:
     void error_Msg () const;
    };


    template < class T > 
    class matrix 
    {  
        int row;   
        int col;
        T ele[10][10];
        public:
        void get ();
        bool check_Sparse ();
        matrix add (matrix &);
        void print ();
    };
     //no changes to be made to the above code
    void mismatchDimension::error_Msg () const const    //Error printing method
    {   
        cout << "Dimension of matrices do not match" << endl;
    } 
    template < class T > 
    void matrix < T >::get ()   //Element input of matrix  
    {
        cin >> row;
        cin >> col;
        int i, j;
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            cin >> ele[j][i];
        }

    }

    template < class T > 
    bool matrix < T >::check_Sparse ()  //Check if the matrix is sparse
    {
        int i, j, t;
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                if (ele[j][i] == 0)  ++t;
            }

        }

    if (t > (row * col) / 2)    return true;

    else    return false;

    }
    template < class T >  
        matrix matrix < T >::add (matrix r) //Addition of the matrices
        {  
            mismatchDimension z;
            int i, j;
            matrix < T > w;
            if (row != r.row && col != r.col)    throw z;
            for (i = 0; i < row; i++)
            {
                 for (j = 0; j < col; j++)
                 {    
                      w.ele[j][i] = ele[j][i] + r.ele[j][i];
                 } 
            }

            return w;
        }




     template < class T > 
        void matrix < T >::print () //printing the matrix
        {
            int i, j;
            for (i = 0; i < row; i++)
            {
                for (j = 0; j < col; j++)  cout << ele[j][i] << endl;
            }
        } 



    // no changes to be made below
      int
    main () 
    {

    matrix < int >m1, m2, m3;

    m1.get ();

    m2.get ();

    try 
      {

    m3 = m1.add (m2);

    m3.print ();

    } catch (mismatchDimension & m) 
      {

    m.error_Msg ();

    } 
    if (m1.check_Sparse ())

    cout << "Matrix is sparse" << endl;

      else

    cout << "Matrix is not sparse" << endl;


    }

【问题讨论】:

  • void mismatchDimension::error_Msg () const const 两次常量? JK,它的matrix matrix &lt; T &gt;::add (matrix r) 实现。应该是matrix matrix &lt; T &gt;::add (matrix&amp; r)

标签: c++


【解决方案1】:

有多个问题:

  1. 声明matrix add (matrix &amp;);通过引用而在定义matrix matrix &lt; T &gt;::add (matrix r)你通过通过值。只选择一个。

  2. 返回类型规范和参数类型应提供模板参数:

template <class T>
matrix<T> matrix<T>::add (matrix r)
{
   ...
}

请注意,从 C++11 开始,您还可以在返回类型中省略模板参数,尾随返回类型声明

template <class T>
auto matrix<T>::add (matrix r) -> matrix
{
   ...
}

【讨论】:

  • 而在 C++14 中,您可以完全省略尾随返回类型。
  • @sweenish 你是对的,但一般来说,我会非常小心地进行返回类型推导。这对函数编写者来说很舒服,但对他们的用户来说却不是。如果有人在类定义中看到类似auto add(const matrix&amp;); 的内容,那么他​​/她不知道函数返回什么,必须阅读函数定义的代码或查看文档。两者都需要一些时间。
  • 我很感激。我不一定是 AAA 范式的粉丝,但如果从声明中已经清楚返回类型是什么,我只会使用 auto 作为返回类型。这就是我的汽车哲学。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多