Hi guys,

I am going mad. I am trying to code an algorithm in C++ and already at a very early stage there appears an error that I just don't understand... These are the error messages sent by the VC++ compiler:

```
\surface_watershed_matlab\surf_watershed.cpp(138) : error C2653: 'CMesh' : is not a class or namespace name
\surface_watershed_matlab\surf_watershed.cpp(144) : error C2065: 'n_verts' : undeclared identifier
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2065: 'it' : undeclared identifier
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2065: 'vertex' : undeclared identifier
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2228: left of '.neighbours' must have class/struct/union
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2228: left of '.begin' must have class/struct/union
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2065: 'it' : undeclared identifier
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2065: 'vertex' : undeclared identifier
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2228: left of '.neighbours' must have class/struct/union
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2228: left of '.end' must have class/struct/union
\surface_watershed_matlab\surf_watershed.cpp(147) : error C2065: 'it' : undeclared identifier
\surface_watershed_matlab\surf_watershed.cpp(148) : error C2065: 'it' : undeclared identifier
```

It's an interface program called by MATLAB (mex file), but I don't think that is the point. OK, here is the code:

```
//#include "stdafx.h"
//#pragma once
#include<iostream>
#include<list>
#include "mex.h"
using namespace std;
typedef struct vert_td{
double x,y,z;
list<long> neighbours;
} TVertex;
typedef struct face_td{
long v1,v2,v3; //indices of the vertices
} TFace;
class Cmesh{
long n_faces,n_verts;
TVertex *vertex;
TFace *face;
void defineNeighbours();
void outputNeighbours();
public:
Cmesh();
Cmesh(double *F,double *V,long n_f,long n_v);
~Cmesh();
//void InList(long v1,long v2,bool &flag);
};
Cmesh::Cmesh()
{
n_faces=0;
n_verts=0;
vertex=NULL;
face=NULL;
}
Cmesh::Cmesh(double *F,double *V,long n_f,long n_v)
{
long i;
n_faces=n_f;
n_verts=n_v;
vertex=(TVertex*)malloc(n_verts*sizeof(TVertex));
face=(TFace*)malloc(n_faces*sizeof(TFace));
for(i=0;i<=n_verts;i++){
vertex[i].x=V[i];
vertex[i].y=V[n_verts+i];
vertex[i].z=V[2*n_verts+i];
}
for(i=0;i<=n_faces;i++){
face[i].v1=(long)F[i]-1;
face[i].v2=(long)F[n_faces+i]-1;
face[i].v3=(long)F[2*n_faces+i]-1;
//if(i%1000==0)
// mexPrintf("Face %d, v1=%d, v2=%d, v3=%d\n",i+1,face[i].v1,face[i].v2,face[i].v3);
}
defineNeighbours();
outputNeighbours();
}
Cmesh::~Cmesh()
{
free(vertex);
free(face);
}
void InList(TVertex *vertex,long v1,long v2,bool &flag)
{
list<long>::iterator it;
flag=false;
for(it=vertex[v1].neighbours.begin(); it != vertex[v1].neighbours.end(); ++it){
if(*it==v2){
flag=true;
break;
}
}
}
void Cmesh::defineNeighbours()
{ //determine neighbouring vertices for each vertex
int i;
long v1,v2,v3;
list<long>::iterator it;
bool flag=false;
for(i=0;i<n_faces;i++){
v1=face[i].v1;
v2=face[i].v2;
v3=face[i].v3;
//Vertex v1: add v2 and v3 to neighbours, if not there already
InList(vertex,v1,v2,flag);
if (!flag)
vertex[v1].neighbours.push_back(v2);
InList(vertex,v1,v3,flag);
if (!flag)
vertex[v1].neighbours.push_back(v3);
//v2
InList(vertex,v2,v1,flag);
if (!flag)
vertex[v2].neighbours.push_back(v1);
InList(vertex,v2,v3,flag);
if (!flag)
vertex[v2].neighbours.push_back(v3);
//v3
InList(vertex,v3,v1,flag);
if (!flag)
vertex[v3].neighbours.push_back(v1);
InList(vertex,v3,v2,flag);
if (!flag)
vertex[v3].neighbours.push_back(v2);
}
}
void CMesh::outputNeighbours()
{
int i;
list<long>::iterator it;
for(i=0;i<n_verts;i++){
if(i%1000==0){
mexPrintf("Vertex %d: ",i);
for(it=vertex[i].neighbours.begin(); it != vertex[i].neighbours.end(); ++it)
mexPrintf("%d, ",*it);
mexPrintf("\n");
}
}
}
double *getWatershed(double *F,double *V,long n_faces,long n_verts)
{
Cmesh *myMesh;
double *output=NULL;
myMesh=new Cmesh(F,V,n_faces,n_verts);
delete myMesh;
return output;
}
// Interface function has 2 input args: faces and vertices
// and one output : segment class (for each vertex)
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
double *F,*V;
double *outArray,*vert_out;
long n_faces,n_verts;
if (nrhs!=2 || nlhs!=1)
mexErrMsgTxt("Number of arguments is invalid...");
F=mxGetPr(prhs[0]);
V=mxGetPr(prhs[1]);
n_faces=mxGetM(prhs[0]);
n_verts=mxGetM(prhs[1]);
mexPrintf("Input: %d Vertices and %d faces.\n",n_verts,n_faces);
//Allocate memory and assign output pointer
plhs[0] = mxCreateDoubleMatrix(n_verts, 1, mxREAL);
outArray = mxGetPr(plhs[0]);
vert_out=getWatershed(F,V,n_faces,n_verts);
}
```

Don't worry that there is no main, that's not the problem, as I said there's an interface function instead. What I just don't understand is that the compiler pretends not to know the class 'CMesh' in the definition of 'outputNeighbours' (line 138) while there is no problem with the analogous function 'defineNeighbours'. If I comment the 'outputXXX' function the code compiles without problems.

To me it almost seems that for some reason the compiler does not accept a second function (except de-/constructor) in the class...

I just don't see it. Any help is greatly appreciated!

Thanks in advance