c++ - <<method name>> 未在此范围内为私有(private)静态方法声明

标签 c++ codeblocks

我之前看到了名为“'name of method' was not declared in this scope”的线程,但答案对我一点帮助都没有。发生这种情况的方法是私有(private)静态方法。我试图在类里面使用它,但它不起作用。该方法的名称是“nthCoeffCatalan”。我每次使用它时都会收到错误消息。我不知道这是否有帮助,但我将 Code::Blocks 与 wxWidgets 和 Mingw32 一起用于编译器。 这是 .h 文件:

#ifndef CATALAN_H
#define CATALAN_H

#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>

using namespace std;

class Catalan
{
    public:
        Catalan(int);
        virtual ~Catalan( );
        void recursiveRandomGenerator( );
        void boltzmannRandomGenerator( );
        int rank( );
        void unrank(int, int);
        void outputBinaryTree( );
        void outputDyckPath( );
        void outputTriangulation( );
    private:
        int n;
        int catalanObject[];
        int max_size;
        static int nthCoeffCatalan(int);
};

#endif // CATALAN_H

这是 .cpp 文件:

#include <Catalan.h>
#include <math.h>
#include <time.h>

using namespace std;

int catalanObject[] = {};
int n;
int max_size;

Catalan::Catalan(int sz){
n = sz;

     //find out the position of the leaf in a tree which is just a node and n-1 right vertices
    max_size = 0;
    for(int i = 0; i <=n; i++){
        max_size += pow(2,i);
    }

    catalanObject[max_size];

    for (int i = 0; i < max_size; i++){
        catalanObject[i] = 0;
    }
}

Catalan::~Catalan( ){
    delete &n;
    delete &max_size;
    delete catalanObject;
}

void recursiveRandomGenerator( ){
    //initialize the random number generator
    srand(time(NULL));
    int r = n;
    int i = 0;

    //give the tree a root
    catalanObject[i] = 1;
    r--;

    //decide size of left and right trees
    float x = ((float) (rand()%10000))/(10000.0);
    int k = -1;
    float s = 0;
    int a_r = 0;
    int b_k = 0;
    int c_rk = 0;

    //calculate a_r
    if (r == 0){
        a_r = 1;
    }
    else{
        //calculate [x^r] in B(x)^2
        if (r > 1){
            for (int j = r/2; j >= 1; j--){
                int temp = 0;
                if (r%2 == 0 && j == r/2){
                    temp = nthCoeffCatalan(j);
                    temp = temp*nthCoeffCatalan(r-j);
                }
                else{
                    temp = nthCoeffCatalan(j);
                    temp = temp*nthCoeffCatalan(r-j);
                    temp = temp*2;
                }
                a_r += temp;
            }
        }
        //calculate [x^r] in 2B(x)
        a_r += 2*nthCoeffCatalan(r);
    }
    while (x > s){
        k = k + 1;
        //calculate b_k
        if (k == 0){
            b_k = 1;
        }
        else{
            b_k = nthCoeffCatalan(k);
        }
        //calculate c_rk
        if (k == r){
            c_rk = 1;
        }
        else{
            c_rk = nthCoeffCatalan(r-k);
        }
        //re-calculate s
        int temp;
        temp = (float) b_k;
        temp = temp * (float) c_rk;
        temp = temp / (float) a_r;
        s += temp;
    }


}

void boltzmannRandomGenerator( ){

}

int rank( ){
    return 0;
}

void unrank(int rnk, int n){

}

void outputBinaryTree( ){

}

void outputDyckPath( ){

}  

void outputTriangulation( ){

}

static int nthCoeffCatalan(int n){
    int num = 1;
    int den = 1;
    int retVal = 0;

    for(int i = 0; i < n; i++){
        num = num*(n+i+1); // runs from n+1 to 2n (=2n!/n!)
        den = den*(i+1); // runs from 2 to n (=n!)
    }

    retVal = num/den;

    return retVal;
}

边注

此外,作为旁注,我想将数组用作类变量,但在构造函数中,在我初始化数组大小的那一行,我的调试器说代码没有效果。我做错了什么吗?

最佳答案

您错误地提供了全局 函数的定义,但您实际上想定义成员 函数(其声明出现在Catalan 的类定义中) >).

例如,在您的 .cpp 文件中:

static int nthCoeffCatalan(int n)
{
    // ...
}

应该是:

int Catalan::nthCoeffCatalan(int n)
//  ^^^^^^^^^
{
    // ...
}

这也适用于其他非静态成员函数。因此,例如(再次在您的 .cpp 文件中)而不是:

void recursiveRandomGenerator( )
{
    // ...
}

你应该写:

void Catalan::recursiveRandomGenerator( )
//   ^^^^^^^^^
{
    // ...
}

等等。

关于c++ - <<method name>> 未在此范围内为私有(private)静态方法声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866063/

相关文章:

c++ - QListWidget : Event on item click

c++ - 为什么在使用 menu_selector 时是 "Class::method"而不仅仅是 "method"?

c++ - 如何从 SuperBible 获取 GLTools 库以在 Ubuntu 中工作?还是另一种选择?

c++ - 使用 Visual Studio 2010 调试 C++ 项目时出现问题

c++ - 无法访问公共(public)静态方法

c++ - 在C++中,默认情况下,istringstreams或stringstreams通常初始化为什么?

c++ - 将 int[5][5] 类型的变量传递给需要 int** 的函数

c++ - 是代码块 10.05。与 QtWorkbench 兼容?

opengl - 如何正确使用 glutSpecialFunc?

c++ - 在 Code::Blocks 中构建一个 wxWidgets 程序