在Dev C ++上编译,但在Borland C ++下给出错误 [英] Compiles on Dev C++, but gives errors under Borland C++

查看:73
本文介绍了在Dev C ++上编译,但在Borland C ++下给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么回事?我正在努力使我获得如此远的代码,并可以在Dev C ++上运行,但给我23条警告,没有错误,而在Borland C ++上则失败了?例如,包含for的函数不是内联的,是什么... [被用作主题时截断-OriginalGriff

 #include   ><   stdlib.h  > 
 #include   <   stdio.h  > 
 #include   <   math.h  > 
 #include   <   conio.h  > 

#define PAUSE {printf(按\" Enter \继续\ n"); fflush(stdin); getchar(); fflush(stdin);}

// 声明
 class 矩阵;
 double  Det( const  Matrix& a);
矩阵Diag( const   int  n);
Matrix Diag( const  Matrix& v);
矩阵Inv( const  Matrix& a);
Matrix Ones( const   int 行, const   int 列);
 int 大小( const  Matrix& a, const   int  i);
矩阵零( const   int 行, const   int 列);


/*  
 *一个简单的异常类
 *您可以输入以下内容来创建例外
 *抛出Exception("...错误描述...");
 *并从数据味精中获取错误消息以进行显示:
 * err.msg
 */
异常
{
公共:
   const   char  * msg;
  异常(常量 字符 * arg)
   :味精(arg)
  {
  }
};



矩阵
{
公共:
  // 构造函数
  矩阵()
  {
    //  printf(正在执行构造函数Matrix()... \ n"); 
    // 创建一个没有内容的Matrix对象
    p = NULL;
    行=  0 ;
    cols =  0 ;
  }

  // 构造函数
  Matrix( const   int  row_count, const   int  column_count)
  {
    // 创建具有给定行数和列数的Matrix对象
    p = NULL;

    如果(行数>  0 && column_count>  0 )
    {
      行= row_count;
      cols = column_count;

      p =   * [rows];
       for ( int  r =  0 ; r <行; r ++)
      {
        p [r] =   double  [cols];

        // 最初为矩阵中的所有值填充零; 
         for ( int  c =  0 ; c < cols; c ++)
        {
          p [r] [c] =  0 ;
        }
      }
    }
  }

  // 赋值运算符
  Matrix( const  Matrix& a)
  {
    行= a.行;
    cols = a.cols;
    p =   * [a.rows];
     for ( int  r =  0 ; r < a.rows; r ++)
    {
      p [r] =   double  [a.cols];

      // 复制矩阵a中的值
       for ( int  c =  0 ; c < a.cols; c ++)
      {
        p [r] [c] = a.p [r] [c];
      }
    }
  }

  // 索引运算符.您可以像myMatrix(col,row)这样使用此类.
  // 索引是基于1的索引,而不是基于0的索引.
双重& 运算符()(常量  int  r, 常量  int  c)
{
   如果(p!= NULL& r>  0 & r< =行&& c>  0 && c< = cols)
   {
     返回 p [r-  1 ] [c-  1 ];
    }
   其他
   {
      抛出异常(" );
    }
  }

  // 索引运算符.您可以使用此类类,例如myMatrix.get(col,row)
  // 索引是基于1的索引,而不是基于0的索引.
  // 如果要从const矩阵读取,请使用此函数
 double  get( const   int  r, const   int  c) const 
{
   如果(p!= NULL& r>  0 & r< =行&& c>  0 && c< = cols)
   {
     返回 p [r-  1 ] [c-  1 ];
    }
   其他
   {
      抛出异常(" );
    }
  }

  // 赋值运算符
  矩阵与运算符 =(常量 Matrix& a)
  {
    行= a.行;
    cols = a.cols;
    p =   * [a.rows];
     for ( int  r =  0 ; r < a.rows; r ++)
    {
      p [r] =   double  [a.cols];

      // 复制矩阵a中的值
       for ( int  c =  0 ; c < a.cols; c ++)
      {
        p [r] [c] = a.p [r] [c];
      }
    }
    返回 * ;
  }

  // 添加一个双精度值(明智的选择)
  矩阵与添加(常量  double  v)
  {
     for ( int  r =  0 ; r <行; r ++)
    {
       for ( int  c =  0 ; c < cols; c ++)
      {
        p [r] [c] + = v;
      }
    }
   返回 * ;
  }

  // 减去一个双精度值(明智的选择)
  矩阵与减( const   double  v)
  {
    返回添加(-v);
  }

  // 乘以双精度值(明智的选择)
  矩阵与乘( const   double  v)
  {
     for ( int  r =  0 ; r <行; r ++)
    {
       for ( int  c =  0 ; c < cols; c ++)
      {
        p [r] [c] * = v;
      }
    }
   返回 * ;
  }

  // 除以双精度值(明智的方式)
  矩阵与除( const   double  v)
  {
   返回乘(1/v);
  }

  // 用Matrix添加Matrix 
  朋友矩阵运算符+( const  Matrix& a, const 矩阵&b)
  {
    // 检查尺寸是否匹配
    如果(a.rows == b.rows& a.cols == b.cols)
    {
     矩阵res(a.rows,a.cols);

       for ( int  r =  0 ; r < a.rows; r ++)
      {
         for ( int  c =  0 ; c < a.cols; c ++)
        {
          res.p [r] [c] = a.p [r] [c] + b.p [r] [c];
        }
      }
     返回 res;
    }
    其他
    {
      // 输入错误
      抛出异常(" );
    }

    // 返回一个空矩阵(从不发生这种情况,只是出于安全考虑)
    返回 Matrix();
  }

  // 加上双精度矩阵
  朋友 Matrix运算符+( const  Matrix& a, const   double  b)
  {
    矩阵res = a;
    res.Add(b);
    返回 res;
  }
  // 使用Matrix添加双精度
  朋友矩阵运算符+( const   double  b, 常量 Matrix& a)
  {
    矩阵res = a;
    res.Add(b);
    返回 res;
  }

  // 用Matrix减去Matrix 
  朋友矩阵运算符-( const  Matrix& a, const  Matrix& b)
  {
    // 检查尺寸是否匹配
    如果(a.rows == b.rows& a.cols == b.cols)
    {
     矩阵res(a.rows,a.cols);

       for ( int  r =  0 ; r < a.rows; r ++)
      {
         for ( int  c =  0 ; c < a.cols; c ++)
        {
          res.p [r] [c] = a.p [r] [c]-b.p [r] [c];
        }
      }
     返回 res;
    }
    其他
    {
      // 输入错误
      抛出异常(" );
    }

    // 返回一个空矩阵(从不发生这种情况,只是出于安全考虑)
    返回 Matrix();
  }

  // 矩阵加双的减法
  朋友矩阵运算符-( const  Matrix& a, const   double  b)
  {
    矩阵res = a;
    res.subtract(b);
    返回 res;
  }
  // 用Matrix减去double 
  朋友矩阵运算符-( const   double  b, const  Matrix& a)
  {
    矩阵res = -a;
    res.Add(b);
    返回 res;
  }

  // 运算符一元减
  朋友矩阵运算符-( const  Matrix&一个)
  {
   矩阵res(a.rows,a.cols);

     for ( int  r =  0 ; r < a.rows; r ++)
    {
       for ( int  c =  0 ; c < a.cols; c ++)
      {
        res.p [r] [c] = -a.p [r] [c];
      }
    }

    返回 res;
  }

  // 运算符乘法
  朋友矩阵运算符 *( const  Matrix& a, const  Matrix& b)
  {
    // 检查尺寸是否匹配
    如果(a.cols == b.rows)
    {
     矩阵res(a.rows,b.cols);

       for ( int  r =  0 ; r < a.rows; r ++)
      {
         for ( int  c_res =  0 ; c_res < b.cols; c_res ++)
        {
           for ( int  c =  0 ; c < a.cols; c ++)
          {
            res.p [r] [c_res] + = a.p [r] [c] * b.p [c] [c_res];
          }
        }
      }
     返回 res;
    }
    其他
    {
      // 输入错误
      抛出异常(" );
    }

    // 返回一个空矩阵(从不发生这种情况,只是出于安全考虑)
    返回 Matrix();
  }

  //  Matrix与double相乘
  朋友矩阵运算符 *( const  Matrix& a, const   double  b)
  {
    矩阵res = a;
    res.Multiply(b);
    返回 res;
  }
  //  double与Matrix的乘法
  朋友矩阵运算符 *( const   double  b, const  Matrix& a)
  {
    矩阵res = a; 



[edit]对象移至正文-OriginalGriff [/edit]

解决方案

Richard MacCutchan给了您正确的答案.

Borland编译器是为ANSI标准而设计的,而不是当前的Visual C ++编译器.

为了使您的代码在两者之间兼容,您需要确定并使用较旧的编码标准.

在发布最后一个Borland C ++编译器时,内联函数仍然是一个相当新的东西. microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express">Visual C ++ [

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>

#define PAUSE {printf("Press \"Enter\" to continue\n"); fflush(stdin); getchar(); fflush(stdin);}

// Declarations
class Matrix;
double Det(const Matrix& a);
Matrix Diag(const int n);
Matrix Diag(const Matrix& v);
Matrix Inv(const Matrix& a);
Matrix Ones(const int rows, const int cols);
int Size(const Matrix& a, const int i);
Matrix Zeros(const int rows, const int cols);


/*
 * a simple exception class
 * you can create an exeption by entering
 *   throw Exception("...Error description...");
 * and get the error message from the data msg for displaying:
 *   err.msg
 */
class Exception
{
public:
  const char* msg;
  Exception(const char* arg)
   : msg(arg)
  {
  }
};



class Matrix
{
public:
  // constructor
  Matrix()
  {
    //printf("Executing constructor Matrix() ...\n");
    // create a Matrix object without content
    p = NULL;
    rows = 0;
    cols = 0;
  }

  // constructor
  Matrix(const int row_count, const int column_count)
  {
    // create a Matrix object with given number of rows and columns
    p = NULL;

    if (row_count > 0 && column_count > 0)
    {
      rows = row_count;
      cols = column_count;

      p = new double*[rows];
      for (int r = 0; r < rows; r++)
      {
        p[r] = new double[cols];

        // initially fill in zeros for all values in the matrix;
        for (int c = 0; c < cols; c++)
        {
          p[r][c] = 0;
        }
      }
    }
  }

  // assignment operator
  Matrix(const Matrix& a)
  {
    rows = a.rows;
    cols = a.cols;
    p = new double*[a.rows];
    for (int r = 0; r < a.rows; r++)
    {
      p[r] = new double[a.cols];

      // copy the values from the matrix a
      for (int c = 0; c < a.cols; c++)
      {
        p[r][c] = a.p[r][c];
      }
    }
  }

  // index operator. You can use this class like myMatrix(col, row)
  // the indexes are one-based, not zero based.
	 double& operator()(const int r, const int c)
	 {
   	if (p != NULL && r > 0 && r <= rows && c > 0 && c <= cols)
   	{
     	return p[r-1][c-1];
    }
   	else
   	{
      throw Exception("Subscript out of range");
    }
  }

  // index operator. You can use this class like myMatrix.get(col, row)
  // the indexes are one-based, not zero based.
  // use this function get if you want to read from a const Matrix
	 double get(const int r, const int c) const
	 {
   	if (p != NULL && r > 0 && r <= rows && c > 0 && c <= cols)
   	{
     	return p[r-1][c-1];
    }
   	else
   	{
      throw Exception("Subscript out of range");
    }
  }

  // assignment operator
  Matrix& operator= (const Matrix& a)
  {
    rows = a.rows;
    cols = a.cols;
    p = new double*[a.rows];
    for (int r = 0; r < a.rows; r++)
    {
      p[r] = new double[a.cols];

      // copy the values from the matrix a
      for (int c = 0; c < a.cols; c++)
      {
        p[r][c] = a.p[r][c];
      }
    }
    return *this;
  }

  // add a double value (elements wise)
  Matrix& Add(const double v)
  {
    for (int r = 0; r < rows; r++)
    {
      for (int c = 0; c < cols; c++)
      {
        p[r][c] += v;
      }
    }
   	 return *this;
  }

  // subtract a double value (elements wise)
  Matrix& Subtract(const double v)
  {
    return Add(-v);
  }

  // multiply a double value (elements wise)
  Matrix& Multiply(const double v)
  {
    for (int r = 0; r < rows; r++)
    {
      for (int c = 0; c < cols; c++)
      {
        p[r][c] *= v;
      }
    }
   	 return *this;
  }

  // divide a double value (elements wise)
  Matrix& Divide(const double v)
  {
   	 return Multiply(1/v);
  }

  // addition of Matrix with Matrix
  friend Matrix operator+(const Matrix& a, const Matrix& b)
  {
    // check if the dimensions match
    if (a.rows == b.rows && a.cols == b.cols)
    {
     	Matrix res(a.rows, a.cols);

      for (int r = 0; r < a.rows; r++)
      {
        for (int c = 0; c < a.cols; c++)
        {
          res.p[r][c] = a.p[r][c] + b.p[r][c];
        }
      }
     	return res;
    }
    else
    {
      // give an error
      throw Exception("Dimensions does not match");
    }

    // return an empty matrix (this never happens but just for safety)
    return Matrix();
  }

  // addition of Matrix with double
  friend Matrix operator+ (const Matrix& a, const double b)
  {
    Matrix res = a;
    res.Add(b);
    return res;
  }
  // addition of double with Matrix
  friend Matrix operator+ (const double b, const Matrix& a)
  {
    Matrix res = a;
    res.Add(b);
    return res;
  }

  // subtraction of Matrix with Matrix
  friend Matrix operator- (const Matrix& a, const Matrix& b)
  {
    // check if the dimensions match
    if (a.rows == b.rows && a.cols == b.cols)
    {
     	Matrix res(a.rows, a.cols);

      for (int r = 0; r < a.rows; r++)
      {
        for (int c = 0; c < a.cols; c++)
        {
          res.p[r][c] = a.p[r][c] - b.p[r][c];
        }
      }
     	return res;
    }
    else
    {
      // give an error
      throw Exception("Dimensions does not match");
    }

    // return an empty matrix (this never happens but just for safety)
    return Matrix();
  }

  // subtraction of Matrix with double
  friend Matrix operator- (const Matrix& a, const double b)
  {
    Matrix res = a;
    res.Subtract(b);
    return res;
  }
  // subtraction of double with Matrix
  friend Matrix operator- (const double b, const Matrix& a)
  {
    Matrix res = -a;
    res.Add(b);
    return res;
  }

  // operator unary minus
  friend Matrix operator- (const Matrix& a)
  {
   	Matrix res(a.rows, a.cols);

    for (int r = 0; r < a.rows; r++)
    {
      for (int c = 0; c < a.cols; c++)
      {
        res.p[r][c] = -a.p[r][c];
      }
    }

    return res;
  }

  // operator multiplication
  friend Matrix operator* (const Matrix& a, const Matrix& b)
  {
    // check if the dimensions match
    if (a.cols == b.rows)
    {
     	Matrix res(a.rows, b.cols);

      for (int r = 0; r < a.rows; r++)
      {
        for (int c_res = 0; c_res < b.cols; c_res++)
        {
          for (int c = 0; c < a.cols; c++)
          {
            res.p[r][c_res] += a.p[r][c] * b.p[c][c_res];
          }
        }
      }
     	return res;
    }
    else
    {
      // give an error
      throw Exception("Dimensions does not match");
    }

    // return an empty matrix (this never happens but just for safety)
    return Matrix();
  }

  // multiplication of Matrix with double
  friend Matrix operator* (const Matrix& a, const double b)
  {
    Matrix res = a;
    res.Multiply(b);
    return res;
  }
  // multiplication of double with Matrix
  friend Matrix operator* (const double b, const Matrix& a)
  {
    Matrix res = a;



[edit]Subject moved in to body - OriginalGriff[/edit]

解决方案

Richard MacCutchan gave you the right answer.

The Borland Compiler was written for an older version of the ANSI Standard than the current Visual C++ compilers.

To make you code compatible between the two, you''ll need to make sure and use the older coding standards.

Inline functions were still pretty new at the time that the last Borland C++ compiler was released.


As an aside you may wish to investigate the free Visual C++[^] IDE and compiler from Microsoft.


这篇关于在Dev C ++上编译,但在Borland C ++下给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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