In my code below, I get this compiler error error C2236: unexpected 'class' 'Pawn'. Did you forget a ';'? But as you can see plainly, I'm not missing a semicolon... am I? I used to think it was a problem due to cyclical dependencies, but I removed any includes beside the vector. This class was also supposed to inherit from my Piece class, but even after removing that I still get an error.
#ifndef CHESS_PAWN_H
#define CHESS_PAWN_H
#include <vector>
class Pawn {
private:
bool _hasMoved;
public:
Pawn(int x, int y);
~Pawn();
std::vector<int> availMoves();
};
#endif
Any advice on what I'm doing wrong here?
Extrapolating, you chess.cpp file might look like this:
#include "piece.h"
#include "pawn.h"
//etc..
The missing semicolon is located in piece.h. Standard preprocessor lossage.
This is a shot in the dark, but is it possible the "vector" header didn't get accidentally modified at some point? I have had this happen to me where I accidentally deleted a line or character in a header file without noticing.
Post the contents of we can take a look and see if it was modified.
Related
I am instructed to code a Blackjack project for my college CMSC class. I have made all required source files but there is an error that I cannot figure out. I am using the terminal with a Makefile to compile my program.
When I compile my program I get this error in the terminal along with other warnings (I am not concerned about the warnings).
In file included from Blackjack.h:19:0,
from Proj2.cpp:12:
Player.h:17:3: error: ‘Hand’ does not name a type
Hand hand;
^
In file included from Blackjack.h:19:0,
from Blackjack.cpp:1:
Player.h:17:3: error: ‘Hand’ does not name a type
Hand hand;
Here is my source code in a Github repository.
https://github.com/Terrablezach/Blackjack
Can anyone tell me why the class 'Hand' does not name a type? I have included it in my header files where it needs to be included and I don't understand why it does not recognize it as a class.
Thank you in advance for your help.
You haven't included Hand.h in Player.h, so the definition of Hand is not available there.
Looking at the project brief you can change code, correct errors and include orders, however it states you cannot change the function declarations (this will be to limit the possible number of variations in code functionality I would suspect).
The function declaration is simply this line: Player(char *newName, int newFunds)
Looking at your code you are potentially going to run into problems with circular inclusion in your headers.
What you could do is wrap each header in a small piece of logic to prevent the same file from being included multiple times, for example add the lines
#pragma once
// the #pragma effectively does the same as the #ifndef/#define lines,
// its the equivalent of belt and braces if you use both
#ifndef HAND_H
#define HAND_H
//normal hand.h code in here
#endif
that way no matter how many times you call on the hand.h file you cannot end up with a multiply defined/included header. As a matter of rote I do that with all my header files whilst doing rapid development.
In regards specifically to the error Player.h:17:3: error: ‘Hand’ does not name a type
Hand hand; I suspect the previous comment in regards to the include order is correct, however I have no linux environment to hand, but will get back to you later tonight/tomorrow:)
The order of the #include declarations is incorrect.
Player class is dependant upon the declaration of Hand class. So in Blackjack.h the #include for Hand.h must go before the #include for Player.h
#ifndef BLACKJACK_H
#define BLACKJACK_H
#include <vector>
#include "Hand.h" // must be before Player.h include
#include "Player.h"
Alternatively, a forward declaration can be used in Player.h.
class Hand; // forward declaration of class Hand
class Player {
public:
Player();
Player(char *newName, int newFunds);
...
...
I am getting this compiler error
error: 'RawLog' does not name a type
Here is the relevant code:
//DataAudit.h
#ifndef DATAAUDIT_H
#define DATAAUDIT_H
class RawLog;
class DataAudit
{
...
private:
RawLog* _createNewRawLog(); // This is the line indicated with the error
};
#endif // DATAAUDIT_H
Usually a forward declaration resolves this kind of error. This answer indicates that a circular header inclusion may cause this. But doesn't the use of the #ifndef and #define statements prevent circular header inclusion?
Is there another reason I might see this error?
What are some avenues of approach I could use to further deduce the nature of this error?
Update: This is rather odd. I have a Globals.h file, and if I define a new enum in Globals.h, the error appears. Then if I comment out the enum, the error goes away. This leads me to think that the circular dependency has existed for a while, and adding the enum somehow re-orders the compilation units, thus exposing the dependency that wasn't there before?
The #ifndef header guard doesn't prevent circular dependencies. It just prevents multiple inclusions of the same header in a single file.
Looks like a circular dependency to me. This means you #include a header in DataAudit.h that #includes DataAudit.h either directly or indirectly.
In the end, I am not sure I understand completely why the error occurred, but this is what I did to resolve it, and some other related information. Maybe this will help others that come across this question.
For each header file in my project
For each #include "..." in the header
If there are no references to the class in the #include, remove it.
If there are only pointers to the class defined in the #include, then replace it with a class ... forward declaration.
If there is a member instance of the class defined in #include and it makes sense to use a pointer and allocate the member on the heap, then change the member to a pointer, and replace the #include with a class .... forward declaration.
Go through my Globals.h and move anything that can be moved out of it to more localized and specific header files.
Specifically, remove an enum that was defined is Globals.h, and place it in a more localized header file.
After doing all this I was able to make the error go away. Strangely enough, the enum in Globals.h seemed to be a catalyst for the error. Whenever I removed it from Globals.h, the error would go away. I don't see how this enum could cause the error, so I think it indirectly led to the error somehow. I still wasn't able to figure out exactly how or why, but it has helped me for this guideline when coding in C++:
Don't put anything in a header file unless it needs to be there. Don't
place anything in a Globals.h that can be placed in a more localized
file. Basically, do all you can to reduce the amount of code that is
included through the #include directives.
I have the class below which inherits from a Collection class where I have virtual functions defined that I need to later on implement in my derived class below. I have yet to include the definitions for my member functions, except for the constructor of my derived class, in the .cpp file. However when I build my project, I get the following error message
expected class-name before '{' token|
I have tried everything I know to try, and am in need of assistance in understanding what I have wrong in my code and how I can go about fixing it.
#ifndef VARIABLEARRAY_H
#define VARIABLEARRAY_H
#include "Collection.h"
using namespace std;
class VariableArray: public Collection{
int* list[];// dynamic array that is resized on demand
public:
VariableArray();
};
#endif
any help will be greatly appreciated.
Are you sure the Collection symbol has already been seen by your translation unit?
You may want to add:
#include "Collection.h"
(or whatever the correct name is) before the class definition.
I think you could also forward declare Collection:
class Collection;
class VariableArray : public Collection {
....
};
How did you determine if Collection is available to the compiler? Does the class declaration for Collection show up in the preprocessed output of your VariableArray.cpp file, or whatever the corresponding source file is?
Looking at the preprocessed output can help you determine:
if namespace pollution is an issue (e.g. #define collides with Collection)
if the Collection declaration is truly available before your VariableArray declaration.
If it's not a namespace pollution issue, I'd check if you are using the same #include guards in more than one header file.
I have a C++ project in Visual Studio 2008.
In the project I have several forms and several non-form classes. One non-form specifically called Import_LP.h that is a class with several methods all of which are written in the header file with nothing in the resource file.
I have no problem with #include Import_LP in any of the form classes and creating objects and referencing any of its methods, however any other class I try to #include it into, it gives me a
syntax error : undeclared identifier 'Import_LP'
on the line it is referenced occurs ie Import_LP^ importLP;
I come from a java/c# background is there something I'm missing with the linking here?
If you have include guards, it goes like this: the preprocessor includes Import_LP.h, which says "only include me once", then includes Window.h, which tries to include Import_LP.h, but doesn't because of the include guard. So Window.h starts parsing the window class, but fails because the Import_LP.h class header hasn't fully loaded yet.
The solution is to predeclare the classes:
Window.h:
#ifndef WINDOW_H //works best if this is first
#define WINDOW_H
#pramga once
class Import_LP;
class Window {
Import_LP* member; //member has to be a pointer
void func();
};
#include "Import_LP.h"
inline void Window::func() {
}
#endif WINDOW_H
Import_LP.h:
#ifndef IMPORT_LP_H //works best if this is first
#define IMPORT_LP_H
#pramga once
class Window;
class Import_LP {
void func(Window& parent); //parameter has to be a pointer or reference
};
#include "Window.h"
inline void Import_LP::func(Window* parent) {
}
#endif IMPORT_LP_H
This will only allow you to reference the other by pointer or reference until the actual include, but that should be doable. Technically you only have to do this to one or the other headers.
I was able to resolve the problem by random chance.
So apparently you can't have two files include each other?
I was doing
#include Window.h
in Import_LP.h
and
#include Import_LP.h
in Window.h.
I would appreciate it if someone could explain to me why you can't do this.
Sounds like the type Import_LP is undefined, your challenge is to figure out why. First thing to do is to read the contents of import_LP.h and figure out how Import_LP is declared. One possible approach would be to open one of the good files, right click on a use of Import_LP and select "Go To Declaration". That should move the focus to the declaration of Import_LP that applies in that specific case.
It could be the declaration is surrounded by a #ifdef that somehow prevents it being compiled in your other files. Or it could be something else, we don't really have enough details to work with.
Update:
So apparently you can't have two files include each other?
. . .
I would appreciate it if someone could explain to me why you
can't do this.
You can end up defining types and variables more than once when you do that.
There are techniques like #include guard and #pragma once which may be used to mitigate these sorts of problems.
It's a well known issue this damn error
expected class-name before ‘{’ token
Well, despite my hard working and googling, I could not solve this error. Sorry. This is my last shore.
In ui.cpp of a project of mine I do:
#include "wfqueue_proxy_factory.hpp"
OK, this raises this stupid error in my compiler:
In file included from
wfqueue_proxy_factory.hpp:29,from
ui.cpp:28:
wfqueue_manager_proxy.hpp:42: error:
expected class-name before ‘{’ token
There are three classes in my project:
First
// wfqueue_proxy_factory.hpp
#ifndef _WFQUEUE_PROXY_FACTORY_HPP
#define _WFQUEUE_PROXY_FACTORY_HPP
#include "wfqueue_manager_proxy.hpp"
// ...
class WFQueueProxyFactory {
//...
};
#endif
Second
// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};
#endif
Third
// wfqueue.hpp
#ifndef _WFQUEUE_HPP
#define _WFQUEUE_HPP
#include "workflow.hpp"
class WFQueue {
// ...
};
#endif
PLEASE PLEASE PLEASE note that I use ; after } of every class, I checked out EVERY header in my project looking for this problem and didn't find any class not followed by ; after its closing bracket. This is valid for workflow.hpp which is a simple class (not deriving from any class, just a plain class).
WFQueue is some sort if interface, I use this pattern with other classes too and they work. WFQueue contains some virtual pure methods... problem should not be here anyway.... I suppose this because I use another "interface" class with other classes and they work fine.
This error disappears if I do this:
// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy {
//...
};
#endif
Don't really know how to solve this problem... please help me.
Thank you
You should run the preprocessor on your code but not compile it, and examine the result. To do this, copy the command which runs the failing compilation, and with most compilers you'd then remove the -o outfile option and add something like -E (see your compiler's documentation for the flag which does preprocessing only).
The compiler will emit (on stdout) the entire translation unit with all #includes and such resolved, so you can clearly see what is missing (just search for the line of code that matches the error line, then look up to see what declarations you find). If it's still not clear what the problem is, write the preprocessed output to a file and try compiling that. You can then tweak the preprocessed source and see what's needed to fix it.
Just a wild guess: Your error says that in
class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};
there must be a class name before {. Therefore I assume that the compiler doesn't know that WFQueue is a class. Are you sure that its definition is included? I mean, maybe in wfqueue.hpp the class is named WfQueue or different in some other way?
The problem might be in misnamed include guards. Try to check if they are really unique per file. It seems that you made it to disable the definition of WFQueue while compiling WFQueueManagerProxy.
It's something it never happened... my god sorry...
It seems that my virtual machine backup disk collided with the original one. I run my project on a virtual machine, making the backup, 2 hours ago, probably messed up something... I adjusted it and now the virtual machine can locate the correct folder and the correct files to compile. It was amazing ahaha and obvious, the ols files g++ tried to compile where a previous version filled with mistakes... This was one of that bugs... a guard header repeated.
Icecrime was right... despite I looked for repetitions in my files, in the previous version, where I didn't fix this problem, there were some files I pasted and forgot to change guard header.
Thank you everyone for your patience and effort.
I'm sorry I didn't notice this very strange virtual disk collision in my machine. Thanks again.
Make sure you typed
using namespace omnetpp;
after includes. It solved my problem.