Strange compilation error in my code, very stuck

Started by
17 comments, last by GameDev.net 18 years, 10 months ago
Ive scanned my code over and over again, and I really have no idea what is wrong. Im using C++, Visual C++ . net 2003 Im sorry this class is a little convuluted and a strange idea. Basically, It's an array of int vectors called yPos , and im populating the vectors with numbers. I also have a single array which stores all the values in the vector called drawOrder1. Im using this class as a way to store which units are at a higher yPos on a chessboard than others. Since the units graphics overlap, I want to draw the units at a higher yPos before the units at a lower yPos. My graphics are 2D but the units look slightly 3 dimensional. I've found this algorithm/idea takes a reasonable time to compile and the update matrix is called fairly irregularly, so it doesnt dampen performance. This is also the first time im using vectors, so im not sure if it has something to do with that , but I highly doubt it. Any insight/ideas would be really apreciated.. This is very frustrating! :) Im getting the following compilation errors: Compiling... Video.cpp d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h(11) : error C2062: type 'int' unexpected d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h(11) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body Unit.cpp d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h(11) : error C2062: type 'int' unexpected d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h(11) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body main.cpp d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h(11) : error C2062: type 'int' unexpected d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h(11) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body d:\VisStudioPro\GalaxyFootball\main.cpp(76) : error C2661: 'OSFMatrix::OSFMatrix' : no overloaded function takes 3 arguments Generating Code... HERE'S MY CODE


#ifndef OSFMatrix_h
#define OSFMatrix_h
#include <vector>
#include <windows.h>
#include "D:/VisStudioPro/GalaxyFootball/Code/Squad/Unit.h"
using namespace std;


class OSFMatrix
{public:

OSFMatrix(int fixedSize, Unit * units [], int numUnits)
{this->fixedSize = fixedSize;
 this->numUnits = numUnits;
 int temp;
 yPos = new vector<int> [fixedSize];
 for(int i =0; i < numUnits; i++)
{       temp = units->getY() / 50;
	if (temp > 50 * fixedSize)
	        MessageBox(NULL,"error", "error", MB_OK);
	yPos[temp].insert(yPos[temp].begin(),i);
 }
drawOrder1 = new int [numUnits];
int counter = 0;
for (int j = 0; j < fixedSize ; j++)
{	for(int k = 0; k < yPos[j].size(); k++)
	{	drawOrder1[counter] = yPos[j][k];
		counter++;
	}
}

}

~OSFMatrix()
{delete drawOrder1;
}
 
int * drawOrder1;
void updateMatrix (int unitNumber, int oldY, int newY);
void updateDrawOrder();

private:
vector <int> * yPos;
int numUnits;
int fixedSize;
};

#endif



[Edited by - Kryodus on June 15, 2005 9:27:43 AM]
Advertisement
Use /[code/] tags to format your source properly.

And, uhm, I know your problem. You're mixing implementation and declaration. You only specify the functions and variables contained in the class in the header file. You actually implement the class in the .cc file.
Formated:
#ifndef OSFMatrix_h#define OSFMatrix_h#include <vector>#include <windows.h>#include "D:/VisStudioPro/GalaxyFootball/Code/Squad/Unit.h"using namespace std;class OSFMatrix {public:		OSFMatrix(int fixedSize, Unit * units [], int numUnits)	{		this->fixedSize = fixedSize;		this->numUnits = numUnits;		int temp;		yPos = new vector<int> [fixedSize];		for(int i =0; i < numUnits; i++) { 			temp = units->getY() / 50;			if (temp > 50 * fixedSize)				MessageBox(NULL,"error", "error", MB_OK);			yPos[temp].insert(yPos[temp].begin(),i);		}		drawOrder1 = new int [numUnits];		int counter = 0;		for (int j = 0; j < fixedSize ; j++) { 			for(int k = 0; k < yPos[j].size(); k++) { 				drawOrder1[counter] = yPos[j][k];			        counter++;			}		}		}		~OSFMatrix()	{		delete drawOrder1;	}		int * drawOrder1;	void updateMatrix (int unitNumber, int oldY, int newY);	void updateDrawOrder();		private:	vector <int> * yPos;	int numUnits;	int fixedSize;};


No, you are allowed to include code in your header. Infact at times it is a good practice, as you may then inline said code.

Your problem may lie (not positive here, I've only allways assumed this to be the case) in your functions using variables that are defined after them in the class. Try moving all your variable declorations before your functions and see if it makes a difference.
By the way: Never, ever, EVER use absolute paths in your #include directives. What happens if someday you need to move your codebase to the D: drive?
Quote:Original post by Sneftel
By the way: Never, ever, EVER use absolute paths in your #include directives. What happens if someday you need to move your codebase to the D: drive?


I figured this was a bad idea, but what am I supposed to do if i want
to access a file in another directiory that is not a root of the current dir.

EG
/code/blah/meh.cpp
/code/bleh/blarny/bah.cpp

I want to import meh.cpp from bah.cpp
I dont know how to access meh without using the entire path,
just "meh.cpp" doesn't work, and neither does, "/code/blah/meh.cpp" which is from the root directory.
I assume therse a setting somewhere to set the root directory for this to work, but i havent been able to find it.
Quote:Original post by CyberFox
Formated:
*** Source Snippet Removed ***

No, you are allowed to include code in your header. Infact at times it is a good practice, as you may then inline said code.

Your problem may lie (not positive here, I've only allways assumed this to be the case) in your functions using variables that are defined after them in the class. Try moving all your variable declorations before your functions and see if it makes a difference.


Thanks for the tip, tried it, but I recieve the same error :(

I dont understand why its saying type int unexpected for a constructor, when I clearly dont have any return type!!!!!!!!!!!!!!!!!
GAH!
Is the file you posted "Algorithms.h"? If you look at your errors:
Video.cpp...Unit.cpp...main.cpp...d:\VisStudioPro\GalaxyFootball\main.cpp(76) : error C2661: 'OSFMatrix::OSFMatrix' : no overloaded function takes 3 argumentsGenerating Code...


When fixing errors, you need to take a top to bottom approach in fixing them because the errors in the beginning will cause additional errors in the end that might not be errors.

Since your OSFMatrix.h file uses the code from Unit.cpp, and Unit.cpp is not being compiled correctly, then I would say that explains that error at least.

So if you can post your "d:\VisStudioPro\GalaxyFootball\Code\Algorithms\Algorithms.h" file, we can take a look at that to see what the problem is.
Quote:Original post by Kryodus
/code/blah/meh.cpp
/code/bleh/blarny/bah.cpp

I want to import meh.cpp from bah.cpp
I dont know how to access meh without using the entire path,
just "meh.cpp" doesn't work, and neither does, "/code/blah/meh.cpp" which is from the root directory.


#include "../meh.cpp"

(although you shouldn't #include a CPP file)
You have:
OSFMatrix(int fixedSize, Unit * units [], int numUnits)

but fixedSize and numUnits are member variables, so is does it think you are
redeclaring them and so it says "type 'int' unexpected"?
1st of all,

having this :

OSFMatrix(int fixedSize, Unit * units [], int numUnits)

- int fixedSize is ok. But if u r setting it to the member variable version,
u gotta do something like this :

this->fixedSize = fixedSize;

- Unit * units[]
I'm not quite sure what u r trying to pass in here. Are you passing a 2d array?

And your constructor should be

OSFMatrix(int fixedSize, Unit * units [], int numUnits); // constructor
~OSFMatrix(void); // destructor

nothing should be done in the header file.

However in the implementation CPP or C file,
you'll have to implmenent ur constructor and destructor.

//Constructor
OSFMatrix::OSFMatrix(int fixedSize, Unit * units [], int numUnits)
{
... constructor codes here ...
}

//Destructor
OSFMatrix::~OSFMatrix(void)
{
... destructor codes here ...
}

You should never implement ur codes in the header file, unless you're writing interfaces for example :

class iIterator {
public :
virtual bool next(void) = 0;
}

Setting next() = 0 explicitly means that this class will not be implemented here but in other classes that inherits it.

OR

Setting default values to member methods
class cMyClass {
private:
int classCount;
public :
void setClassCount(int classCount = -1);
}

Lastly, seperate your declarations and implementations. Never place them together.

Hope that helps!
You'll never see heaven if you haven't been through hell.

This topic is closed to new replies.

Advertisement