<<方法名称>>未在此范围内声明私有静态方法
<<method name>> was not declared in this scope for a private static method
我看到前面的线程叫做" '方法的名字'没有在这个范围内声明",但是答案并没有真正帮助我。发生这种情况的方法是一个私有静态方法。我正试图在课堂上使用它,但它不起作用。方法的名称是"nthCoeffCatalan"。每次使用都会出现错误。我不知道这是否会有帮助,但我正在使用代码::块与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( )
// ^^^^^^^^^
{
// ...
}
等等
您的所有成员函数定义都应该被限定为属于Catalan
。例如,recursiveRandomGenerator
的定义实际上应该是这样的:
void Catalan::recursiveRandomGenerator( ){
// ...
}
同样,你不应该把static
关键字放在nthCoeffCatalan
的定义之前。它应该看起来像这样:
int Catalan::nthCoeffCatalan(int n){
// ...
}
你的类定义声明了一堆成员函数,包括nthCoeffCatalan
。在源文件中,只定义了两个成员:构造函数和析构函数。其余的函数都是自由函数。您必须通过在它们的名称中添加Catalan::
来将它们标记为成员函数。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中