Member Avatar for getack

Have a class, in a separate .h file, and the implementation of this class in it's own .C file.

The class does not include anything external, so it exists completely on it's own.
When I compile, G++ tells me it is looking for a semicolon before the class declaration. This really baffles me, as I do not include anything in the class. The only solution that seems to keep the compiler quiet is

;class foo //beginning the def with a semicolon
{
  public:
    int getBar();
  private:
    int bar;
};

Recommended Answers

All 12 Replies

Your probably missing a semi-colon in the other file.

Member Avatar for getack

But what other file? Foo is not including anything above itself? That's the problem I'm having. If I put a "using namespace std" above the class declaration, i have to move the semicolon to before the using...

It is understood that you are asking about an issue in your *.h file and that there are no #includes above this class' definition. gerard is asking you about your *.cpp files. With an error like this, the cause of the problem frequently is not actually in the file that the error reports it in, but rather a different linked file. Please post your *.cpp file.

Member Avatar for getack

Okay here is the code:

//LegalMoveChecker.h
#ifndef LEGALMOVECHECKER_H
#define LEGALMOVECHECKER_H

#include <iostream>
class legalFootSoldierMoveChecker;

class LegalMoveChecker
{
  public:
    virtual bool isThisALegalMove(int iCX, int iCY, int iDX, int iDY) = 0;
};

class LegalFootSoldierMoveChecker: public LegalMoveChecker
{
  public:
    bool isThisALegalMove(int iCX, int iCY, int iDX, int iDY);
};
#endif
//LegalFootSoldierMoveChecker.C
using namespace std;
#include <iostream>
#include "LegalMoveChecker.h"
bool LegalFootSoldierMoveChecker::isThisALegalMove(int iCX, int iCY, int iDX, int iDY)
{
	return true;
}

I get the error in line 6

In your *.h file, you don't even need Line 6. Either remove the line or comment it out. Since C/C++ is case-sensitive, it doesn't match anything anyway because your capitalization is wrong.

In your *.cpp file, move your #includes up to Lines 2 and 3, then move "using namespace std;" down to line 4.

Member Avatar for getack

Thanks for showing my capitalization mistake *embarrassed*

I did what you recommended and it still complaining about a missing semicolon before "class" in line 8

Are you sure the error is in this header file? Please paste a direct copy of the error you are getting.

Member Avatar for getack

Morning!

Here is the direct output copied from Terminal: I am using G++ to compile, and developing on a Linux platform.

In file included from Piece.C:11:
LegalMoveChecker.h:16: error: expected `;' before ‘class’
make: *** [FootSoldierPiece.o] Error 1

Just so you understand, I am working on a chess like game. This part is designed to check whether the user has requested a move that is legal for each specific type of piece. At this stage it only returns true, as I have not implemented the actual test logic yet.

This error is flagging the inclusion of LegalMoveChecker.h from Piece.C. I think you need to post the code of Piece.C for us to look at.

You mentioned that you're using G++, but I don't remember seeing what OS and IDE you're using.

>>Just so you understand, I am working on a chess like game.
I figured it was something like that...

Member Avatar for getack

here is Piece.C

The OS I'm using is some ancient Debian at the university labs. Not sure about the version. I don't use an IDE, I code in Kate and use make and terminal to build.

#include <iostream>
#include <cstring>
using namespace std;

#include "LegalMoveChecker.h"
#include "Piece.h"

void Piece::move(int iCX, int iCY, int iDX, int iDY)
{
	if(checkValidMove(iDX, iDY))
	{
		myPositionX = iDX;
		myPositionY = iDY;
	}
	else
	{
		throw "INVALID MOVE";
	}
}

bool Piece::checkValidMove(int iDX, int iDY)
{
	bool retVal;
	retVal = checker->isThisALegalMove(myPositionX, myPositionY, iDX, iDY);
	return retVal;
}

void Piece::makeSelected()
{
	iAmSelected = true;
}
Member Avatar for getack

These errors are making me crazier by the minute. I use a vector in my game somewhere, so I included <vector> in one of my .C files. The compiler now spits out a missing semicolon in stl_construct.h! But when I move the #include to the bottom the list of includes, the problem goes away. All my classes end with a semicolon. I hexaChecked. I swear.

You could try compiling with the -E switch which informs the GCC compile to run the preprocessor only.

G++ filename -E -o resultfile

Note the file produced is large.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.