C++ 从C到C++;:函数未在此作用域中声明
我从C库导入了一些方法,用在C++主代码中,但是我有编译错误:C++ 从C到C++;:函数未在此作用域中声明,c++,c,C++,C,我从C库导入了一些方法,用在C++主代码中,但是我有编译错误: $ make g++ -c main.cpp main.cpp: In function ‘int main(int, char**)’: main.cpp:419:67: error: ‘kmedoids’ was not declared in this scope kmedoids(class_num, N, distanceMatrix, 1, clusterid, error, ifound);
$ make
g++ -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:419:67: error: ‘kmedoids’ was not declared in this scope
kmedoids(class_num, N, distanceMatrix, 1, clusterid, error, ifound);
^
main.cpp: In function ‘void kmedoids(int, int, double**, int, int*, double*, int*)’:
main.cpp:537:64: error: ‘randomassign’ was not declared in this scope
if (npass!=0) randomassign(nclusters, nelements, tclusterid);
^
main.cpp:550:42: error: ‘getclustermedoids’ was not declared in this scope
centroids, errors);
^
main.cpp: In function ‘void randomassign(int, int, int*)’:
main.cpp:644:20: error: ‘binomial’ was not declared in this scope
j = binomial(n, p);
^
main.cpp:654:40: error: ‘uniform’ was not declared in this scope
{ j = (int) (i + (nelements-i)*uniform());
^
main.cpp: In function ‘int binomial(int, double)’:
main.cpp:704:24: error: ‘uniform’ was not declared in this scope
double u = uniform();
^
main.cpp:732:26: error: ‘uniform’ was not declared in this scope
double u = uniform();
^
make: *** [main.o] Error 1
这是我的新C方法代码的最后一部分:
#include "profileManager.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <limits.h>
#include <float.h>
#include <time.h>
int main(int argc, char *argv[])
{
//......PREVIOUS CODE.......
kmedoids(class_num, N, distanceMatrix, 1, clusterid, error, ifound);
return 0;
}
//NEW C METHODS
void kmedoids (int nclusters, int nelements, double** distmatrix,
int npass, int clusterid[], double* error, int* ifound)
{ int i, j, icluster;
int* tclusterid;
int* saved;
int* centroids;
double* errors;
int ipass = 0;
if (nelements < nclusters)
{ *ifound = 0;
return;
} /* More clusters asked for than elements available */
*ifound = -1;
/* We save the clustering solution periodically and check if it reappears */
saved = (int*)malloc(nelements*sizeof(int));
if (saved==NULL) return;
centroids = (int*)malloc(nclusters*sizeof(int));
if(!centroids)
{ free(saved);
return;
}
errors = (double*)malloc(nclusters*sizeof(double));
if(!errors)
{ free(saved);
free(centroids);
return;
}
/* Find out if the user specified an initial clustering */
if (npass<=1) tclusterid = clusterid;
else
{ tclusterid = (int*)malloc(nelements*sizeof(int));
if(!tclusterid)
{ free(saved);
free(centroids);
free(errors);
return;
}
}
*error = DBL_MAX;
do /* Start the loop */
{ double total = DBL_MAX;
int counter = 0;
int period = 10;
if (npass!=0) randomassign(nclusters, nelements, tclusterid);
while(1)
{ double previous = total;
total = 0.0;
if (counter % period == 0) /* Save the current cluster assignments */
{ for (i = 0; i < nelements; i++) saved[i] = tclusterid[i];
if (period < INT_MAX / 2) period *= 2;
}
counter++;
/* Find the center */
getclustermedoids(nclusters, nelements, distmatrix, tclusterid,
centroids, errors);
for (i = 0; i < nelements; i++)
/* Find the closest cluster */
{ double distance = DBL_MAX;
for (icluster = 0; icluster < nclusters; icluster++)
{ double tdistance;
j = centroids[icluster];
if (i==j)
{ distance = 0.0;
tclusterid[i] = icluster;
break;
}
tdistance = (i > j) ? distmatrix[i][j] : distmatrix[j][i];
if (tdistance < distance)
{ distance = tdistance;
tclusterid[i] = icluster;
}
}
total += distance;
}
if (total>=previous) break;
/* total>=previous is FALSE on some machines even if total and previous
* are bitwise identical. */
for (i = 0; i < nelements; i++)
if (saved[i]!=tclusterid[i]) break;
if (i==nelements)
break; /* Identical solution found; break out of this loop */
}
for (i = 0; i < nelements; i++)
{ if (clusterid[i]!=centroids[tclusterid[i]])
{ if (total < *error)
{ *ifound = 1;
*error = total;
/* Replace by the centroid in each cluster. */
for (j = 0; j < nelements; j++)
clusterid[j] = centroids[tclusterid[j]];
}
break;
}
}
if (i==nelements) (*ifound)++; /* break statement not encountered */
} while (++ipass < npass);
/* Deallocate temporarily used space */
if (npass > 1) free(tclusterid);
free(saved);
free(centroids);
free(errors);
return;
}
static void randomassign (int nclusters, int nelements, int* clusterid)
{ int i, j;
int k = 0;
double p;
int n = nelements-nclusters;
/* Draw the number of elements in each cluster from a multinomial
* distribution, reserving ncluster elements to set independently
* in order to guarantee that none of the clusters are empty.
*/
for (i = 0; i < nclusters-1; i++)
{ p = 1.0/(nclusters-i);
j = binomial(n, p);
n -= j;
j += k+1; /* Assign at least one element to cluster i */
for ( ; k < j; k++) clusterid[k] = i;
}
/* Assign the remaining elements to the last cluster */
for ( ; k < nelements; k++) clusterid[k] = i;
/* Create a random permutation of the cluster assignments */
for (i = 0; i < nelements; i++)
{ j = (int) (i + (nelements-i)*uniform());
k = clusterid[j];
clusterid[j] = clusterid[i];
clusterid[i] = k;
}
return;
}
static int binomial(int n, double p)
{ const double q = 1 - p;
if (n*p < 30.0) /* Algorithm BINV */
{ const double s = p/q;
const double a = (n+1)*s;
double r = exp(n*log(q)); /* pow() causes a crash on AIX */
int x = 0;
double u = uniform();
while(1)
{ if (u < r) return x;
u-=r;
x++;
r *= (a/x)-s;
}
}
else /* Algorithm BTPE */
{ /* Step 0 */
const double fm = n*p + p;
const int m = (int) fm;
const double p1 = floor(2.195*sqrt(n*p*q) -4.6*q) + 0.5;
const double xm = m + 0.5;
const double xl = xm - p1;
const double xr = xm + p1;
const double c = 0.134 + 20.5/(15.3+m);
const double a = (fm-xl)/(fm-xl*p);
const double b = (xr-fm)/(xr*q);
const double lambdal = a*(1.0+0.5*a);
const double lambdar = b*(1.0+0.5*b);
const double p2 = p1*(1+2*c);
const double p3 = p2 + c/lambdal;
const double p4 = p3 + c/lambdar;
while (1)
{ /* Step 1 */
int y;
int k;
double u = uniform();
double v = uniform();
u *= p4;
if (u <= p1) return (int)(xm-p1*v+u);
/* Step 2 */
if (u > p2)
{ /* Step 3 */
if (u > p3)
{ /* Step 4 */
y = (int)(xr-log(v)/lambdar);
if (y > n) continue;
/* Go to step 5 */
v = v*(u-p3)*lambdar;
}
else
{ y = (int)(xl+log(v)/lambdal);
if (y < 0) continue;
/* Go to step 5 */
v = v*(u-p2)*lambdal;
}
}
else
{ const double x = xl + (u-p1)/c;
v = v*c + 1.0 - fabs(m-x+0.5)/p1;
if (v > 1) continue;
/* Go to step 5 */
y = (int)x;
}
/* Step 5 */
/* Step 5.0 */
k = abs(y-m);
if (k > 20 && k < 0.5*n*p*q-1.0)
{ /* Step 5.2 */
double rho = (k/(n*p*q))*((k*(k/3.0 + 0.625) + 0.1666666666666)/(n*p*q)+0.5);
double t = -k*k/(2*n*p*q);
double A = log(v);
if (A < t-rho) return y;
else if (A > t+rho) continue;
else
{ /* Step 5.3 */
double x1 = y+1;
double f1 = m+1;
double z = n+1-m;
double w = n-y+1;
double x2 = x1*x1;
double f2 = f1*f1;
double z2 = z*z;
double w2 = w*w;
if (A > xm * log(f1/x1) + (n-m+0.5)*log(z/w)
+ (y-m)*log(w*p/(x1*q))
+ (13860.-(462.-(132.-(99.-140./f2)/f2)/f2)/f2)/f1/166320.
+ (13860.-(462.-(132.-(99.-140./z2)/z2)/z2)/z2)/z/166320.
+ (13860.-(462.-(132.-(99.-140./x2)/x2)/x2)/x2)/x1/166320.
+ (13860.-(462.-(132.-(99.-140./w2)/w2)/w2)/w2)/w/166320.)
continue;
return y;
}
}
else
{ /* Step 5.1 */
int i;
const double s = p/q;
const double aa = s*(n+1);
double f = 1.0;
for (i = m; i < y; f *= (aa/(++i)-s));
for (i = y; i < m; f /= (aa/(++i)-s));
if (v > f) continue;
return y;
}
}
}
/* Never get here */
return -1;
}
static double uniform(void)
{ int z;
static const int m1 = 2147483563;
static const int m2 = 2147483399;
const double scale = 1.0/m1;
static int s1 = 0;
static int s2 = 0;
if (s1==0 || s2==0) /* initialize */
{ unsigned int initseed = (unsigned int) time(0);
srand(initseed);
s1 = rand();
s2 = rand();
}
do
{ int k;
k = s1/53668;
s1 = 40014*(s1-k*53668)-k*12211;
if (s1 < 0) s1+=m1;
k = s2/52774;
s2 = 40692*(s2-k*52774)-k*3791;
if(s2 < 0) s2+=m2;
z = s1-s2;
if(z < 1) z+=(m1-1);
} while (z==m1); /* To avoid returning 1.0 */
return z*scale;
}
void getclustermedoids(int nclusters, int nelements, double** distance,
int clusterid[], int centroids[], double errors[])
{ int i, j, k;
for (j = 0; j < nclusters; j++) errors[j] = DBL_MAX;
for (i = 0; i < nelements; i++)
{ double d = 0.0;
j = clusterid[i];
for (k = 0; k < nelements; k++)
{ if (i==k || clusterid[k]!=j) continue;
d += (i < k ? distance[k][i] : distance[i][k]);
if (d > errors[j]) break;
}
if (d < errors[j])
{ errors[j] = d;
centroids[j] = i;
}
}
}
#包括“profileManager.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
//……以前的代码。。。。。。。
kmedoids(class_num,N,distanceMatrix,1,clusterid,error,ifound);
返回0;
}
//新的C方法
void kmedoids(整数类、整数元素、双**距离矩阵、,
int-npass,int-clusterid[],双*错误,int*ifound)
{int i,j,icluster;
int*tclustrid;
整数*已保存;
int*质心;
双*错误;
int ipass=0;
if(元素<非集群)
{*ifound=0;
返回;
}/*要求的群集数超过可用的元素数*/
*ifound=-1;
/*我们定期保存集群解决方案,并检查它是否再次出现*/
保存=(int*)malloc(nelements*sizeof(int));
if(saved==NULL)返回;
质心=(int*)malloc(nclusters*sizeof(int));
如果(!质心)
{免费(已保存);
返回;
}
错误=(双*)malloc(nclusters*sizeof(双));
如果(!错误)
{免费(已保存);
自由(质心);
返回;
}
/*了解用户是否指定了初始群集*/
if(npassj)?distmatrix[i][j]:distmatrix[j][i];
如果(t距离<距离)
{距离=距离;
tclusterid[i]=icluster;
}
}
总+=距离;
}
如果(总>=上一次)中断;
/*total>=previous在某些计算机上为FALSE,即使total和previous
*是按位相同的*/
对于(i=0;i1)自由(tclusterid);
免费(保存);
自由(质心);
自由(错误);
返回;
}
静态void randomsign(int-nclusters、int-neelements、int*clusterid)
{int i,j;
int k=0;
双p;
int n=元素和非聚类;
/*从多项式中绘制每个簇中的元素数
*分布,保留要独立设置的非群集元素
*以确保所有集群都不为空。
*/
对于(i=0;ip3)
{/*第4步*/
y=(int)(xr log(v)/lambdar);
如果(y>n)继续;
/*转至步骤5*/
v=v*(u-p3)*lambdar;
}
其他的
{y=(int)(xl+log(v)/lambdal);
如果(y<0)继续;
/*转至步骤5*/
v=v*(u-p2)*λ;
}
}
其他的
{const double x=xl+(u-p1)/c;
v=v*c+1.0-fabs(m-x+0.5)/p1;
如果(v>1)继续;
/*转至步骤5*/
y=(int)x;
}
/*步骤5*/
/*步骤5.0*/
k=绝对值(y-m);
如果(k>20&&k<0.5*n*p*q-1.0)
{/*步骤5.2*/
双ρ=(k/(n*p*q))*(k*(k*(k/3.0+0.625)+0.16666/(n*p*q)+0.5);
双t=-k*k/(2*n*p*q);
双A=对数(v);
如果(At+rho)继续;
其他的
{/*步骤5.3*/
双x1=y+1;
双f1=m+1;
双z=n+1-m;
双w=n-y+1;
双x2=x1*x1;
双f2=f1*f1;
双z2=z*z;
双w2=w*w;
如果(A>xm*log(f1/x1)+(n-m+0.5)*log(z/w)
+(y-m)*对数(w*p/(x1*q))
+(13860.-(462.-(132.-(99.-140./f2)/f2)/f2)/f1/166320。
+(13860.-(462.-(132.-(99.-140./z2)/z2)/z2)/z/166320。
+(13860.-(462.-(132.-(99.-140./x2)/x2)/x2)/x1/166320。
+(13860.-(462.-(132.-(99.-140./w2)/w2)/w2)/w/166320)
继续;
返回y;
}
}
其他的
{ /*
void kmedoids (int nclusters, int nelements, double** distmatrix, int npass, int clusterid[], double* error, int* ifound);