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.
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);
...
...
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.
Several questions has been asked related to this error, but each one of them practically relates to the object or type in question not declared before usage. For example:
class A
{
public:
A_Object a_obj;
};
Getting the error A_Object was not declared in this scope means A_object is not declared anywhere within the file.
NOTE: This is my understanding of the error.
Now I have a file called Account.h as shown below:
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
class Account
{
//fields and methods
};
#endif /* ACCOUNT_H_ */
I also have a second file called Address.h as shown below:
#ifndef ADDRESS_H_
#define ADDRESS_H_
#include "Account.h"
typedef Account account_type;//Error here
class Address
{
//Fields and methods
};
#endif /* ADDRESS_H_ */
When I try to compile this file I get the error Account was not declared in this scope.
Any Ideas why?
Does Account.h actually also include Address.h? Such a circular reference seems the most likely situation.
Do you have a matching #endif at the end of both include files?
First point, your understanding about A_object is incorrect, the error means that A_object was not declared prior to it's first use, not that it wasn't declared anywhere.
Second point, the code you posted is incorrect, because you are missing #endif from both files. But assuming that was the only missing code then you would not get the error you describe. Post the real code that has the error.
I've seen this error when Address.h includes Account.h, which includes OtherFile.h, which includes Address.h. Is it possible you have a circular dependency? It may be hard to find.
This might be a case where a more core understanding of how the c/c++ compiler works would be in order. Include blocks, forward declarations, includes etc. All of these concepts did not make sense to me until I understood the basics of how compiler works. While I realize that this is somewhat of an oversimplification of compiler theory / logic, bear with me.
One of the first steps that a c++ compiler performs is a pre-processing (pre-compiler) step where it takes all of the files that are required, and combines them into one big flat file. In "C" languages, these pre-compiler operations a denoted using the hash (#) symbol. All an "#include" is doing, is directing the pre-compiler to bring this file into the entire "flat-file". If you have a cyclic include, your pre-compiler will get into an infinite loop and blow up, or say something super generic and useful like "the symbol has already been defined".
Include blocks, forward declarations, and all of the neat things that you are taught in c++ books that say "just do it, trust me" generally are helping you to avoid these type of compiling problems.
I was reading some code from the Doom 3 SDK ( in a VS solution ) when I found a header like this:
#ifndef __PLAYERICON_H__
#define __PLAYERICON_H__
class idPlayerIcon {
public:
idPlayerIcon();
~idPlayerIcon();
...... // omitted
public:
playerIconType_t iconType;
renderEntity_t renderEnt;
qhandle_t iconHandle;
};
#endif /* !_PLAYERICON_H_ */
The header has no forward class declaration nor #includes so, in my experience it should lead to an error like: Undeclared Identifier or Syntax error, cause renderEntity_t and qhandle_t are not "seen".
So how can this compile correctly?
Thank you in advance for the answers.
Because every time it is included, the needed entities are forward declared/included right before it, so everything is defined at the point of inclusion. As you correctly say, it will not work any other way.
I guess they include other headers before including this one.
Since this is a header file it's likely there is an order to their includes elsewhere (where this file is used perhaps?). As long as renderEntity_t and qhandle_t make it into the symbol table prior to this file being included it doesn't matter.
Is there something called stdafx.h? Most VS projects have these.
It would simply be a header that includes all the needed files for your application to reduce compile time from including headers.
So it would contain something like this:
#ifndef _STDAFX_H_
#define _STDAFX_H_
#include "playerIconAndOtherVariables.h"
#include "thatFileYouListed.h"
#endif
I have my own class inside the file "Particles.h" and the class's implementation is inside "Particles.cpp"
I want the file "Load.h" to recognize my classes inside there, so I've added the line
#include "Particles.h"
and the file doesn't recognize it and in the past everything was OK (I haven't made any changes inside that class).
What should I do?
It sounds like your include path - the list of directories that the compiler scans in order to locate files that you #include - is set incorrectly. Which compiler are you using?
Well, if you listed your error codes, it might help. Off the top of my head, do you have something in Particles.h to make sure that the file is only included once? There are two methods of doing this. The first is to use #pragma once, but I think that might be Microsoft specific. The second is to use a #define.
Example:
#ifndef PARTICLES_H
#define PARTICLES_H
class CParticleWrapper
{
...
};
#endif
Also, unless you're deriving from a class in Particles.h or using an instance of a class instead of a pointer, you can use a forward declaration of the class and skip including the header file in a header file, which will save you compile time.
#ifndef LOAD_H
#define LOAD_H
class CParticleWrapper;
class CLoader
{
CParticleWrapper * m_pParticle;
public:
CLoader(CParticleWrapper * pParticle);
...
};
#endif
Then, in the Load.cpp, you would include the particle.h file.
make sure the file "Particles.cpp" has also included "Particles.h" to start with and the files are in the same folder and they are all part of the same project. it will help if you also share the error message that you are getting from your compiler.
Dev C++,It uses GCC,
The line is:
Stone *stone[48];
and it says: "expected constructor, destructor, or type conversion before '*' token ".
It sounds like you need to include the definition of the Stone class, but it would be impossible to say without more details. Can you narrow down the error by removing unrelated code and post that?