2012-11-22 59 views
1

你能告诉为什么视觉工作室编译这段代码(在邮件的末尾)就好了,但G ++给我一个错误:G ++ VS Visual Studio中

chapter8_1.cpp:97:50: error: macro "minor" passed 3 arguments, but takes just 1 
chapter8_1.cpp:136:36: error: macro "minor" passed 3 arguments, but takes just 1 
chapter8_1.cpp:97:11: error: function definition does not declare parameters 
chapter8_1.cpp: In member function ‘double Matrices::determinant(double**, int)’: 
chapter8_1.cpp:136:17: error: ‘minor’ was not declared in this scope 

现在这两个功能都在一个struct,但如果我编译他们在没有struct(简单独立的函数),然后g + +给我没有错误,程序工作正常。该程序旨在计算任何方阵的行列式。

代码:

struct Matrices 
{ 
....... 

    double **minor(double **matrix, int dim, int row) // row stands for the number of column that we are expanding by 
    { 
     int dim2 =--dim; 
     double **minor2; 
     minor2=new double*[dim2];       // creates minor matrix 
     for(int i=0; i<dim2; ++i) 
      minor2[i]=new double[dim2]; 

     for(int hhh=0; hhh<dim2; ++hhh) 
      { 
       int bbb=0; 
       for(int aaa=0; aaa<dim2+1; ++aaa)     // initializing the minor matrix 
        { 

        if (aaa==row) 
         continue; 
        else 
         { 
          minor2[hhh][bbb]=matrix[hhh+1][aaa]; 
          ++bbb; 
         } 
        } 
       } 
     return minor2; 
    } 

    double determinant(double **mat, int dim) 
    { 
    double det=0; 
    if(dim==1) 
    det=mat[0][0]; 
    if(dim==2) 
    det=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0]; 
    else 
    { 
    double ***setOFmat;          // pointer that points to minors 
    setOFmat=new double**[dim]; 
    for (int ddd=0; ddd<dim; ++ddd)       // ddd represents here the number of column we are expanding by 
     setOFmat[ddd]=minor(mat, dim, ddd); 
    for (int ddd=0; ddd<dim; ++ddd)       // ddd srepresents the same here 
    { 

     det= det + pow(-1.0,ddd)*mat[0][ddd]*determinant(setOFmat[ddd], dim-1); // actual formula that calculates the determinant 

    } 
    } 
    return det; 
    } 
+0

在一个struct?你是说在一个班级? –

+8

由于错误消息提到了名为'minor'的**宏**,因此似乎有一个具有该名称的预处理器宏。尝试重命名你的函数,看看它是否更好。 –

+0

或者至少用大写来定义你的宏。 – Aesthete

回答

0

您应仔细阅读错误消息的更多一点:-)

chapter8_1.cpp:97:50: error: macro "minor" passed 3 arguments, but takes just 1

这意味着,gcc认为minor是一些描述宏这样的预处理该行:

double **minor(double **matrix, int dim, int row) 

is lik非常麻烦。

我会gcc -E编译它,从而获得预处理器输出,这样你就可以知道:

  • 它是如何破坏您的线;和
  • 有希望在minor宏定义。