-
Include files
I am having trouble getting some code that I have written compiled. I have multiple include files. On one of the include files there are functions which require varaibles to be defined which are done so with the #define statement. For example the functions in Cell.cpp require certain variables which are used in their definition as the index of arrays for example. I wish these parameters to be stored in the main.cpp file where they are grouped with the others . However I find I keep getting errors relating to the Cell.h files:
Cell.h:9: error: `Ysections' was not declared in this scope
In the main.cpp file I have included the#include "Cell.h" line after I have defined the variables required. However I keep getting errors like this and cannot see why his should be the case. Can you please help on the best course of action.
I am using GNU compiler under linux.
Thanks
-
Howzabout posting a small (but complete) sample of code that replicates your problem?
-
Here is the main.cpp file. I have included the #defines in an include file for simplicity:
#include <math.h>
#include <string.h>
#include <stdio.h>"
#include "Variables.h"
#include "Cell.h"
################################################## ############
This is Cell.cpp
#include <stdlib.h>
#include <stdio.h>
#include "Variables.h"
#include "Cell.h"
################################################## ########
This is Cell.h
#ifndef _Cell_h
#define _Cell_h
void Readin_Files(double arr_lipid[][3][3], int N, char DOSFileName[], double &Xbox, double &Ybox, double &Zbox);
#endif
################################################## ##
This should give you a flavour of what is going on. Its probably something easy but just cant fathom it.
-
I don't see 'Ysections' in your post. The more difficult you make it for someone to help you, the less likely you are going to get help.
-
Try declaring Ysections with the extern storage class to make it global over all source files.
eg.
Code:
//main.h
#ifndef MAIN_H
#define MAIN_H
extern int Ysections;
#endif
-
These are big files.... I'll include them now.
Code:
Cell.cpp
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include "Variables.h"
#include "Cell.h"
void Readin_Files(double arr_lipid[][3][3], int N, char DOSFileName[], double &Xbox, double &Ybox, double &Zbox)
{
// Read in files containing information about positions for further runs.
char name[40];
char header[40];
int lipidNumber;
int lipidGroup;
int i,z;
//double XBox,YBox,ZBox;
ifstream inFile;
inFile.open(DOSFileName);
if (!inFile)
{
//cerr << "Unable to open file datafile.txt";
//exit(1); // call system to stop
}
inFile >> header;
if (strcmp(header,"LIPIDTOTAL") == 0)
{
inFile >> header;
//N = atoi (header);
inFile >> header;
}
if (strcmp(header,"XWIDTH") == 0)
{
inFile >> header;
Xbox = atoi (header);
inFile >> header;
}
if (strcmp(header,"YWIDTH") == 0)
{
inFile >> header;
Ybox = atoi (header);
inFile >> header;
}
if (strcmp(header,"ZWIDTH") == 0)
{
inFile >> header;
Zbox = atoi (header);
inFile >> name;
}
for (i=0;i<3*N;i++)
{
if (strcmp(name,"LIPID") == 0)
{
inFile >> name;
lipidNumber = atoi (name);
inFile >> name;
//inFile >> name;
}
if (strcmp(name,"GROUP") == 0)
{
inFile >> name;
lipidGroup = atoi (name);
inFile >> name;
for (z = 0; z<3; z++)
{
arr_lipid[lipidNumber][lipidGroup][z] = atof(name);
inFile >> name;
}
}
}
inFile.close();
}
void GetArray(double arraylipids[][3][3], int N, int MC,double& Xbox, double &Ybox, double &Zbox)
{
char *str = new char[80];
char *startfile = new char[80];
sprintf(startfile,"%d",MC);
strcpy (str,"./files/MC");
strcat (str,startfile);
strcat (str,".txt");
printf("%s\n",str);
//printf("%s\n",startfile);
printf("Reading in input file...");
Readin_Files(arraylipids,N,str,Xbox,Ybox,Zbox);
delete str;
delete startfile;
}
void InitCellIndex(int CellIndex[][Ysections][Zsections][MaxLipidsinCell])
{
int x,y,z,lipid;
for (x=0;x<Xsections;x++)
{
for (y=0;y<Ysections;y++)
{
for (z=0;z<Zsections;z++)
{
for (lipid=0; lipid < MaxLipidsinCell; lipid++)
{
CellIndex[x][y][z][lipid] = 0;
}
}
}
}
}
void InitCellIndexEnd(int CellIndex[][Ysections][Zsections])
{
int x,y,z;
for (x=0;x<Xsections;x++)
{
for (y=0;y<Ysections;y++)
{
for (z=0;z<Zsections;z++)
{
CellIndex[x][y][z] = 0;
}
}
}
}
void InitLookupCell(int LookupCell[][3])
{
int lipid;
for (lipid=0;lipid<MaxLipidsinCell;lipid++)
{
LookupCell[0][lipid] = 0;
LookupCell[1][lipid] = 0;
LookupCell[2][lipid] = 0;
}
}
void DivideAndConquer (double Xbox, double Ybox, double Zbox, int Xsec, int Ysec, int Zsec, double xpos, double ypos, double zpos, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int LookupCell[][3], int CellIndexEnd[][Ysections][Zsections], int lipidnumber)
{
// Use this to place all the lipid ins the correct cell to start off with
int xgridposition;
int ygridposition;
int zgridposition;
xgridposition = (int) (xpos / (Xbox / Xsec));
ygridposition = (int) (ypos / (Ybox / Ysec));
zgridposition = (int) (zpos / (Zbox / Zsec));
//printf("Positions %d %d %d\n",xgridposition,ygridposition,zgridposition);
//printf("Adding lipid: %d\n",lipidnumber);
Add_to_Lookup(LookupCell,xgridposition,ygridposition,zgridposition,lipidnumber);
Add_to_CellIndex(CellIndex,CellIndexEnd,lipidnumber,xgridposition,ygridposition,zgridposition);
}
void Add_to_Lookup(int LookupCell[][3], int xgrid, int ygrid, int zgrid, int lipidnumber)
{
LookupCell[lipidnumber][0] = xgrid;
LookupCell[lipidnumber][1] = ygrid;
LookupCell[lipidnumber][2] = zgrid;
}
void Add_to_CellIndex(int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int CellIndexEnd[][Ysections][Zsections], int lipidnumber, int xgrid, int ygrid, int zgrid)
{
int currentposition;
//printf("Adding at: %d %d %d\n",xgrid,ygrid,zgrid);
if (CellIndexEnd[xgrid][ygrid][zgrid] == 0)
{
//printf("Current position: 0\n");
CellIndex[xgrid][ygrid][zgrid][0] = lipidnumber;
//yCellIndex[ygrid][0] = lipidnumber;
//zCellIndex[zgrid][0] = lipidnumber;
CellIndexEnd[xgrid][ygrid][zgrid] += 1;
}else
{
currentposition = CellIndexEnd[xgrid][ygrid][zgrid];
//printf("Current position: %d\n",currentposition);
CellIndex[xgrid][ygrid][zgrid][currentposition] = lipidnumber;
//xCellIndex[xgrid][currentposition] = lipidnumber;
//yCellIndex[ygrid][currentposition] = lipidnumber;
//zCellIndex[zgrid][currentposition] = lipidnumber;
CellIndexEnd[xgrid][ygrid][zgrid] += 1;
}
}
void Delete_from_CellIndex(int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int CellIndexEnd[][Ysections][Zsections], int lipidnumber, int xgrid, int ygrid, int zgrid)
{
int endposition = 0;
int pos;
if (CellIndexEnd[xgrid][ygrid][zgrid] != 0)
{
endposition = CellIndexEnd[xgrid][ygrid][zgrid];
//printf("endposition: %d\n",endposition);
}
for (pos=0;pos<endposition;pos++)
{
//printf("....Cell Indexes [%d][%d][%d]: %d\n" , xgrid, ygrid, zgrid, CellIndex[xgrid][ygrid][zgrid][pos]);
if (CellIndex[xgrid][ygrid][zgrid][pos] == lipidnumber)
{
//printf("Found match\n");
CellIndex[xgrid][ygrid][zgrid][pos] = -1;
// Close up gap
CellIndexEnd[xgrid][ygrid][zgrid] = CellIndexEnd[xgrid][ygrid][zgrid] - 1;
endposition = CellIndexEnd[xgrid][ygrid][zgrid];
CloseGap(pos, CellIndex, endposition, xgrid, ygrid, zgrid);
}
}
}
void CloseGap(int positiongap, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int endposition, int xgrid, int ygrid, int zgrid)
{
int pos;
for (pos=positiongap;pos<(endposition);pos++)
{
//printf("looping Cell Indexes [%d][%d][%d]: %d\n" , xgrid, ygrid, zgrid, CellIndex[xgrid][ygrid][zgrid][pos]);
CellIndex[xgrid][ygrid][zgrid][pos] = CellIndex[xgrid][ygrid][zgrid][pos+1];
//CellIndex[xgrid][ygrid][zgrid][endposition] = 0;
}
}
void NeighbourCells(int NeighbourArray[][3], int xcentre, int ycentre, int zcentre)
{
// Ideally must have more than 27 cells in order to work effectively!
// Most cases this is no problem.
int addx;
int addy;
int addz;
int x,y,z;
int xcurrent;
int ycurrent;
int zcurrent;
int count = 0;
int MaximumNumberofCells;
MaximumNumberofCells = Xsections * Ysections * Zsections;
if (MaximumNumberofCells < 27)
{
printf("max number of cells: %d\n",MaximumNumberofCells);
// End the program here
// Invalid starting conditions
getchar();
}else
{
MaximumNumberofCells = 27;
}
for (addx=-1;addx<1+1;addx++)
{
for (addy=-1;addy<1+1;addy++)
{
for (addz=-1;addz<1+1;addz++)
{
xcurrent = (xcentre + addx) % Xsections;
ycurrent = (ycentre + addy) % Ysections;
zcurrent = (zcentre + addz) % Zsections;
printf("Before Positions %d %d %d\n",xcurrent,ycurrent,zcurrent);
if (xcurrent < 0)
{
xcurrent = xcurrent + Xsections;
}
if (ycurrent < 0)
{
ycurrent = ycurrent + Ysections;
}
if (zcurrent < 0)
{
zcurrent = zcurrent + Zsections;
}
NeighbourArray[count][0] = xcurrent;
NeighbourArray[count][1] = ycurrent;
NeighbourArray[count][2] = zcurrent;
printf("Positions %d %d %d\n",xcurrent,ycurrent,zcurrent);
printf("count: %d\n",count);
getchar();
count+=1;
// 27 times round the loop!!!
}
}
}
}
void DivideIntoCells(double Xbox, double Ybox, double Zbox, int Xsec, int Ysec, int Zsec, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int LookupCell[][3], int CellIndexEnd[][Ysections][Zsections], int lipidnumber, double lipidarray[][3][3])
{
int currentlipid;
for (currentlipid = 0;currentlipid<LipidTotal;currentlipid++)
{
DivideAndConquer(Xbox,Ybox,Zbox,Xsections,Ysections,Zsections,lipidarray[currentlipid][0][0],lipidarray[currentlipid][0][1],lipidarray[currentlipid][0][2],CellIndex,LookupCell,CellIndexEnd,currentlipid);
}
}
void NeighbourLipids(int xcellcentre, int ycellcentre, int zcellcentre, int NNList[], int &NNend, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int CellIndexEnd[][Ysections][Zsections])
{
int NCells[27][3];
int count = 0;
int xcell, ycell, zcell;
int lipid;
int mycell;
int endposition;
int MaximumNumberofCells;
NeighbourCells(NCells,xcellcentre,ycellcentre,zcellcentre);
MaximumNumberofCells = Xsections * Ysections * Zsections;
if (MaximumNumberofCells < 27)
{
printf("max number of cells: %d\n",MaximumNumberofCells);
}else
{
MaximumNumberofCells = 27;
}
for (mycell = 0; mycell<MaximumNumberofCells; mycell++)
{
xcell = NCells[mycell][0];
ycell = NCells[mycell][1];
zcell = NCells[mycell][2];
printf("Looking at cell: %d %d %d\n",xcell,ycell,zcell);
endposition = CellIndexEnd[xcell][ycell][zcell];
printf("End position: %d\n",endposition);
getchar();
for (lipid=0;lipid<endposition;lipid++)
{
//printf("Lipid: %d\n",CellIndex[xcell][ycell][zcell][lipid]);
NNList[count] = CellIndex[xcell][ycell][zcell][lipid];
count +=1;
}
}
NNend = (count-1);
//printf("NN lipids: %d\n",NNend);
}
void NeighbourCellArray(int NCArrayAll[][Ysections][Zsections][27][3])
{
// Create an array that is ready to roll of all the neighbour cells for each and every cell
// Include itself in the mix too
int NCellArray[27][3];
int x,y,z;
int neighbour;
for (x=0;x<Xsections;x++)
{
for (y=0;y<Ysections;y++)
{
for (z=0;z<Zsections;z++)
{
NeighbourCells(NCellArray,x,y,z);
for (neighbour = 0;neighbour<27;neighbour++)
{
NCArrayAll[x][y][z][neighbour][0] = NCellArray[neighbour][0];
NCArrayAll[x][y][z][neighbour][1] = NCellArray[neighbour][1];
NCArrayAll[x][y][z][neighbour][2] = NCellArray[neighbour][2];
//printf("Neighbour of: %d %d %d is: %d %d %d\n",x,y,z,NCellArray[neighbour][0],NCellArray[neighbour][1],NCellArray[neighbour][2]);
}
}
}
}
}
void ReplaceLookupCell(int LookupCell[][3], int lipid, int x, int y, int z)
{
LookupCell[lipid][0] = x;
LookupCell[lipid][1] = y;
LookupCell[lipid][2] = z;
}
void CellChange(bool movedcell, double xnewpos, double ynewpos, double znewpos, int xcell, int ycell, int zcell, double Xbox, double Ybox, double Zbox, int newxcell, int newycell, int newzcell)
{
// Code assumes that we can only move into the next cell.
// Will not handle large jumps in lipids or likewise small cells.
double xleft, xright;
double yleft, yright;
double zleft, zright;
movedcell = false;
xleft = (xcell) * (Xbox / Xsections);
xright = xleft + (Xbox / Xsections);
yleft = (ycell) * (Ybox / Ysections);
yright = yleft + (Ybox / Ysections);
zleft = (zcell) * (Zbox / Zsections);
zright = zleft + (Zbox / Zsections);
if (xnewpos < xleft)
{
newxcell = (xcell - 1);
movedcell = true;
}
if (xnewpos > xright)
{
newxcell = (xcell + 1);
movedcell = true;
}
if ((newxcell == Xsections) || (newxcell == -1))
{
// we cant move here as we are out of range
// remove for speed later
}
if (ynewpos < yleft)
{
newycell = (ycell - 1);
movedcell = true;
}
if (ynewpos > yright)
{
newycell = (ycell + 1);
movedcell = true;
}
if ((newycell == Ysections) || (newycell == -1))
{
// we cant move here as we are out of range
// remove for speed later
}
if (znewpos < zleft)
{
newzcell = (zcell - 1);
movedcell = true;
}
if (znewpos > zright)
{
newzcell = (zcell + 1);
movedcell = true;
}
if ((newzcell == Zsections) || (newzcell == -1))
{
// we cant move here as we are out of range
// remove for speed later
}
// Check to see if we have gone beyond the limits of the cell
}
/*int main(void)
{
int CellIndex[Xsections][Ysections][Zsections][MaxLipidsinCell];
int CellIndexEnd[Xsections][Ysections][Zsections];
int LookupCell[LipidTotal][3];
double lipidarray[LipidTotal][3][3];
int NNList[LipidTotal];
int currentlipid;
double Xbox, Ybox, Zbox;
int NNend;
int NeighbourArray[27][3];
printf("Loading file for reading....\n");
InitCellIndex(CellIndex);
InitCellIndexEnd(CellIndexEnd);
InitLookupCell(LookupCell);
GetArray(lipidarray,LipidTotal,0,Xbox,Ybox,Zbox);
printf("Beginning Cell Initialisation....\n");
DivideIntoCells(Xbox,Ybox,Zbox,Xsections,Ysections,Zsections,CellIndex,LookupCell,CellIndexEnd,currentlipid,lipidarray);
int NCellArrayAll[Xsections][Ysections][Zsections][27][3];
//NeighbourCellArray(NCellArrayAll);
NeighbourLipids(1,1,1,NNList,NNend,CellIndex,CellIndexEnd);
printf("%d NN's in this list\n",NNend);
for (currentlipid = 0;currentlipid<NNend+1;currentlipid++)
{
printf("Nearest Neighbours for cell at %d\n", NNList[currentlipid]);
}
//Delete_from_CellIndex(CellIndex, CellIndexEnd, 204, 0, 0, 0);
int x,y,z;
int lipid;
int count =0;
int endarray;
for (x=0;x<Xsections;x++)
{
for (y=0;y<Ysections;y++)
{
for (z=0;z<Zsections;z++)
{
endarray = CellIndexEnd[x][y][z];
//printf("End array: %d\n",endarray);
for (lipid=0;lipid<endarray;lipid++)
{
//printf("Cell Indexes [%d][%d][%d]: %d\n" , x, y, z, CellIndex[x][y][z][lipid]);
count +=1;
}
}
}
}
printf("%d\n",count);
}*/
Cell.h
#ifndef _Cell_h
#define _Cell_h
void Readin_Files(double arr_lipid[][3][3], int N, char DOSFileName[], double &Xbox, double &Ybox, double &Zbox);
void GetArray(double arraylipids[][3][3], int N, int MC,double& Xbox, double &Ybox, double &Zbox);
void InitCellIndex(int CellIndex[][Ysections][Zsections][MaxLipidsinCell]);
void InitCellIndexEnd(int CellIndex[][Ysections][Zsections]);
void InitLookupCell(int LookupCell[][3]);
void DivideAndConquer (double Xbox, double Ybox, double Zbox, int Xsec, int Ysec, int Zsec, double xpos, double ypos, double zpos, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int LookupCell[][3], int CellIndexEnd[][Ysections][Zsections], int lipidnumber);
void Add_to_Lookup(int LookupCell[][3], int xgrid, int ygrid, int zgrid, int lipidnumber);
void Add_to_CellIndex(int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int CellIndexEnd[][Ysections][Zsections], int lipidnumber, int xgrid, int ygrid, int zgrid);
void Delete_from_CellIndex(int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int CellIndexEnd[][Ysections][Zsections], int lipidnumber, int xgrid, int ygrid, int zgrid);
void CloseGap(int positiongap, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int endposition, int xgrid, int ygrid, int zgrid);
void NeighbourCells(int NeighbourArray[][3], int xcentre, int ycentre, int zcentre);
void DivideIntoCells(double Xbox, double Ybox, double Zbox, int Xsec, int Ysec, int Zsec, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int LookupCell[][3], int CellIndexEnd[][Ysections][Zsections], int lipidnumber, double lipidarray[][3][3]);
void NeighbourLipids(int xcellcentre, int ycellcentre, int zcellcentre, int NNList[], int &NNend, int CellIndex[][Ysections][Zsections][MaxLipidsinCell], int CellIndexEnd[][Ysections][Zsections]);
void NeighbourCellArray(int NCArrayAll[][Ysections][Zsections][27][3]);
void ReplaceLookupCell(int LookupCell[][3], int lipid, int x, int y, int z);
void CellChange(bool movedcell, double xnewpos, double ynewpos, double znewpos, int xcell, int ycell, int zcell, double Xbox, double Ybox, double Zbox, int newxcell, int newycell, int newzcell);
#endif
Variables.cpp
#define Xsections 10
#define Ysections 2
#define Zsections 2
#define MaxLipidsinCell 100
#define PI 3.14159
#define LipidTotal 1000
Variables.h
#ifndef _Variables_h
#define _Variables_h
#endif
Main.cpp
Only including
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include "Energy.h"
#include "MyMatrix.h"
#include "Sorts.h"
#include "FileHandling.h"
#include "Variables.h"
#include "Cell.h"
-
You need to reproduce the problem in a small sample of your code, and post that. Otherwise you have to hope someone here will take the time to troll through your code.
-
You #define Ysections in variables.cpp and so when it is compiling Cell.h the compiler doesn't know what Ysections is. You should use a const int instead of a #define anyway, and you ned to put it in a location that will always be included (like Cell.h).