C++ Build Error- expected class-name before '{' token| - c++

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.

Related

pointer variable of class 'Entity' within class 'Component', gives errors even though 'Entity.h' has been included

Okay So I'm making my own Entity Component System, and I'm trying to have an Entity pointer object within a Component class, but the object(variable) gives me a ton of errors, even though it has been included.
'Component.h'
#include "Entity.h"
class Component {
public:
Entity* ThisEntity;
}
That doesn't work, and gives me 76 errors inside 'Entity.h', all of which don't recognize my custom types(like Component, string, etc).
I usually use a global header file which has all the global variables, and includes everything necessary, like this:
'Public.h'
#ifndef Entity_h
#include "Entity.h" // Entity is include guarded
#endif
When i try to include 'Public.h' inside 'Component.cpp' it still gives me errors:
'Component.cpp'
#include "Public.h"
#include "Component.h" // The arrangement is correct, public before component but this still doesn't work
When I hover over the pointer variable "Entity* ThisEnt" it shows me "Entity Class", so it recognizes it but it still gives me 76 errors.
You can not refer to header files from each other as it creates a cyclical dependency.
You can break that dependency by forward declaring one of the classes you want to use.
Forward declaring the class 'Entity' before the class 'Component' seemed to fix the issue.

C++ - Class does not name a type

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);
...
...

How to include a self-created class in another self-created class's header file?

Say I have created two classes: Tires, and Car.
So I have four files: Tires.cpp, Tires.h, Car.cpp, Car.h.
The Car constructor takes Tires as its parameter. But I am not sure how to modify Car.h to
include Tires.h.
Here's what I've done so far (note: they are in separate files)
Tires.h
#include <iostream>
using namespace std;
class Tires
{
private:
int numTires;
public:
Tires();
};
Tires.cpp
#include <iostream>
#include "Tires.h"
using namespace std;
Tires::Tires()
{
numTires = 4;
}
Car.h
#include <iostream>
#include "Tires.h"
using namespace std;
class Tires; // Tried taking out forward declaration but still didn't work
class Car
{
private:
Tires tires;
public:
Car(Tires); // Edited. Thanks to Noah for pointing out.
};
Car.cpp
#include <iostream>
#include "Car.h"
#include "Tires.h"
using namespace std;
Car::Car(Tires _tires)
{
tires = _tires;
}
Thanks
Your approach seems fine here.
One thing to keep in mind when headers include other headers is that you may find you need to include an include guard:
// At the start of Tires.h:
//
// Only delcare this stuff if this is the first time including Tires.h:
//
#ifndef __myproject_Tires_h__
#define __myproject_Tires_h__
class Tires
{
// [snip]
};
// Close the #ifdef above...
//
#endif
This prevents you from declaring "class Tire {" et al. multiple times, should Tires.h happen to be included twice.
Another is that this line in Car.h is not needed:
class Tires;
This may be useful if you want to have declarations of Tires* or Tires&, but to do what you did next:
class Car
{
private:
Tires tires;
... requires Tires to be a "complete type", for its size to be known, etc. You're already covered by that by having #include "Tires.h" anyway.
Lastly, some consider it bad form to have a using statement inside a header as you have done. This kind of breaks namespaces by bringing in std as a global namespace for all files that use this header. Imagine if every header did this, and did so for multiple namespaces. Eventually this becomes the same as there being no such thing as namespaces, and collisions are more likely.
One thing you need is "include guards" so that you don't get a bunch of compiler errors due to redefinition.
Put something like the following in each of you header files:
#ifndef TIRES_H
#define TIRES_H
// contents of the header file...
#endif
Of course, change the name used for the macro guard (TIRES_H) as appropriate for each file. The macro name needs to be unique - basing it on the header file name is usually good enough. Also, many (most?) compilers support a #pragma once preprocessed directive that prevents headers from being processed more than once, but I still generally use the standard include guards.
This allows headers to be included more than once, since the guards cause subsequent includes of the file to essentially skip the entire contents.
Almost all C/C++ headers should have include guards so users don't need to worry about whether or not a necessary header was already included (the exceptions are headers which need to redefine things differently when included at different times - this is a pretty rare technique). Include guards also enable you to have header files (like cars.h in your example) include the headers they need without regard to what else might also include the headers, so your headers can be self-contained and can be included in any order.
You have already included Tires.h in Car.h. You also have a forward declaration of class Tires in Car.h. You should eliminate either the include or the forward declaration. As you don't handle Tires as reference or pointer and you thus need the "behavior" of class Tires, you should eliminate the forward declaration.
You've basically already answered your own question except that your Car(Tires) constructor has yet to be declared in your Car interface.
But I'd actually not do it that way. Your constructor should be Car(Tires const&) so that you can simply use the forward declaration you've already got in Car.h and not include Tires.h until in Car.cpp. The rest of your code could stay the same, but I'd still make a further change and use initialization rather than assignment in the constructor:
Car::Car(Tires const& _tires) : tires(_tires) {}
Even further, I recommend not EVER using '_' as the first character in any name. There's no need to and too often people get confused about when it is OK and when it is not.
From all files delete line containing "using namespace std;"
From Car.h delete line containing "class Tires;" as it's included from #include "Tires.h"
Now wrap all your header file in header guards.

Impossible expected class-name before ‘{’ token error to solve

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.

C++ Agent : Base class undefined error in Game

I have 6 C++ header files. There are a lot of includes so I tried to make it so that I use as little as I can. But I keep getting an error from the very beginning saying that a class "Agent" is undefined. I defined it and included it and can't find the problem here are the 2 header files that are causing the problem:
Sinbad.h:
#ifndef SINBAD_H
#define SINBAD_H
#pragma once
#include "Agent.h"
#define NUM_ANIMS 13 // number of animations the character has. Should be made character specific
class Agent;
class Sinbad : public Agent {
Agent.h:
#ifndef AGENT_H
#define AGENT_H
#include <deque>
#include "aStar.h"
extern Ogre::SceneManager* sceneMgr; // Defined in main.cpp
class GridNode; // forward declarations
class Grid;
class Astar;
class Agent {
Here's the error I am getting:
1>c\gameengine_solution\sinbad.h(12) : error C2504: 'Agent' : base class undefined
It looks like something is referring to class Agent before it is defined, i.e. probably in aStar.h.
EDIT: Search for #define AGENT_H everywhere in your source. If you find it anywhere outside Agent.h, that means the Agent class might never be defined, if only because Agent.h can be #included with AGENT_H already #defined.
Do not redeclare class Agent in Sinbad.h. You already included Agent.h. Seems also to be the case in Agent.h with Grid and Astar.
Some of the comments suggest you're unsure how forward declaring works.
Forward declare a type when you need to tell the compiler that the type exists, but do not need any of its definition. Generally, this is done in a header when a class's members or methods only have pointers or references to the type. In order to do anything that involves dereferencing the pointer or using the reference, you will need to include the header that defines the type, usually in the source that defines the methods of the class.