I'm receiving the error:
In file included from proprietario.h:5,
from veiculo.h:4:
motocicleta.h:8: error: expected class-name before ‘{’ token
Motocicleta.h:
#ifndef __MOTOCICLETA__
#define __MOTOCICLETA__
#include <iostream>
#include "veiculo.h"
#include "proprietario.h"
using namespace std;
class Proprietario;
class Motocicleta:public Veiculo{
public:
Motocicleta(int nPassageiros, string modelo, string placa, int aFabricacao, Proprietario* pai, int nRodas, int aro);
~Motocicleta();
Motocicleta (const Motocicleta& source);
Motocicleta& operator= (const Motocicleta& source);
string toString();
};
#endif
Proprietario.h
#ifndef __PROPRIETARIO__
#define __PROPRIETARIO__
#include <iostream>
#include "motocicleta.h"
#include "caminhao.h"
#include "carreta.h"
#include "carro.h"
using namespace std;
class Carro;
class Carreta;
class Caminhao;
class Motocicleta;
class Proprietario{
protected:
string nome;
string cpf;
Motocicleta* mMoto;
Caminhao* mCaminhao;
Carreta* mCarreta;
Carro* mCarro;
};
Veiculo.h:
#ifndef __VEICULO__
#define __VEICULO__
#include <iostream>
#include "proprietario.h"
#include "roda.h"
#include "motor.h"
using namespace std;
class Motor;
class Proprietario;
class Veiculo{
protected:
int nPassageiros;
string modelo;
string placa;
int aFabricacao;
Proprietario* pai;
Roda* rodas;
Motor* mMotor;
int nRodas;
};
I removed the methods, because if i added those the question will be to long, sorry, the code is in PT-BR.
I saw that the problem is usually is forward declaration.
But i cannot find out the problem, i looked in so many forums but i cannot find out the problem..
Someone can help me?
Need any other part of the code?
The real problem here is the liberal use of #include preprocessor directives. In general, you should only include a header file at the lowest scope at which it is needed and forward-declare everything you can. You simply don't (shouldn't) generally need full class declarations for header files. Header files do not generally need to know about implementation details. You definitely shouldn't been forward-declaring and including the header.
As the code stands in the question at the time of writing this answer, you have a circular dependency on veiculo.h (and also on proprietario.h). As veiculo.h really is needed for the header of its subclass Motocicleta, you should remove the #include directives for the classes you have already forward-declared in each of the headers. You can then include the headers in the source files as needed.
Related
I have a multifile program, and I can't figure out why my program says that "Customers" (in the registerNewUser() function) is an undeclared identifier.
proc.h
#ifndef PROC_H
#define PROC_H
#include <iostream>
#include "const.h"
#include "customers.h"
#include <fstream>
using namespace std;
void registerNewUser(Customers cBase); // Add new user.
#endif // !PROC_H
I have included the header file (customers.h) with the Customers class also.
customers.h
#ifndef CUSTOMERS_H
#define CUSTOMERS_H
#include <iostream>
#include "const.h"
#include "proc.h"
#include "customer.h"
using namespace std;
class Customers {
private:
char* current;
List* customerList; // List for customers.
public:
Customers(); // Constructor.
~Customers(); // Destructor.
void handler(); // Customers handler/menu.
void addNew(char username[]);
};
#endif // !CUSTOMERS_H
Can anyone see what's wrong?
You have a circular include. customers.h includes proc.h so basiacally
void registerNewUser(Customers cBase);
Will get added to customers.h before the compiler has seen what a Customer is. It looks like you should just be able to remove the #include "proc.h" in customers.h and it should compile.
As stated in the comments above you should never use using namespace std; in a header file as anything that includes it now has the entire std namespace exposed. You should also get in the habit of only using it in the most narrow scope you can or drop it completely. For further reading on the use of using namespace std; see Why is “using namespace std” in C++ considered bad practice?
Basically including "customers.h" in "customers.h" wouldn't be a problem here, since you have a guard (plus point for that). Nevertheless it is not very nice.
As NathanOliver said it COULD be a problem with the order of the includes but it doesn't have to. If you include proc.h first everything is fine. If you include customers first, the compiler includes proc.h before he sees the customer class. proc then wont include customers.h (since its guard prevents it). Then he will find your function not knowing what "Customer" means. So depending on the include order of your header files it will or will not work.
If you want a hint: I normally first only include the necessary files for a forward declaration, then do a forward declaration. Then I include the files necessary files for the definition of the class (These will already know that the class exists). The complete class declaration (with member function declaration) follows. If you do it like this you can avoid many mistakes. In your case:
#ifndef CUSTOMERS_H
#define CUSTOMERS_H
class Customers;
#include "proc.h"
#include "ListTool2B.H"
using namespace std;
class Customers
{
private:
char* current;
List* customerList; // List for customers.
public:
Customers(); // Constructor.
~Customers(); // Destructor.
void handler(); // Customers handler/menu.
void addNew(char username[]);
};
#endif
This is probably a duplicate: you have proc.h including customers.h and customers.h including proc.h this will cause a circular reference, and looks like proc.h included in customers is not necessary, so you could try simply to delete this line:
#include "proc.h"
When I include this header File "pathfinding.h":
#pragma once
#include <BWAPI.h>
#include "BWAPI/TilePosition.h"
#include <vector>
#include "PathNode.h"
#include "Logger.h"
#include "ArgosMap.h"
#include "MapField.h"
#include "Utils.h"
#include "ComparePathNodePointer.h"
using namespace BWAPI;
class Pathfinding {
private:
std::vector<PathNode*> openList;
std::vector<PathNode*> closedList;
std::vector<Position*> buildpath(PathNode* targetNode);
void expandNode(PathNode* currentNode, MapField* targetField);
ArgosMap* argosMap;
public:
Pathfinding();
~Pathfinding();
std::vector<Position*> getShortestPath(MapField* startField, MapField* targetField);
};
In this header File "UnitAgent.h":
#pragma once
#include <BWAPI.h>
#include <vector>
#include "ArgosMap.h"
#include "Pathfinding.h"
using namespace BWAPI;
class UnitAgent {
protected:
Unit* unit;
UnitType unitType;
int unitID;
std::vector<Position*> trail;
Position target;
public:
UnitAgent(Unit* unit);
std::vector<Position*> getTrail();
Position getTarget();
Position* getPosition();
int getUnitID();
void setTarget(Position target);
void addPositionToTrail(Position* targetLocation);
void moveTo(TilePosition* targetPosition);
};
I get like a million errors mostly error C2143, C2065. But thats not true, the errors do not exist. When I include the header file in another file its all totally fine (except naturally for the stuff that needs the specific header file).
Any Ideas what I should check. Anyone an Idea how i can check my C++ Code, in a way Eclipse checks my java code. I mean why doesnt Visual Studio do that?
This kind of directive should not be in a header file
using namespace BWAPI;
To start with, why do you need all this
#include <BWAPI.h>
#include "BWAPI/TilePosition.h"
#include <vector>
#include "PathNode.h"
#include "Logger.h"
#include "ArgosMap.h"
#include "MapField.h"
#include "Utils.h"
#include "ComparePathNodePointer.h"
using namespace BWAPI;
in pathfinding.h? Just forward declaring ArgosMap, MapField, PathNode, and Position like
class ArgosMap;
class MapField;
class PathNode;
class Position;
is sufficient for pathfinding.h looking at the declaration of Pathfinding class, the stuff above should go to pathfinding.cpp if it is necessary for implementation of Pathfinding methods. The less stuff and dependencies you have in your headers, the easier it will be to debug.
Declarations in pathfinding.h look fine, problem is that some of the methods are not implemented/are not implemented properly. To find out what this methods are, you would need to narrow the scope of the problem - by removing unnecessary dependencies to start with.
Including pathfinding.h in files that do not use it's methods/methods from other headers included would always work fine...
i have received in .cpp errors of invalid use of undefined struct PelephonePN, CellcomPN and so on
and also error in .h errors of forward declaration of PelephonePN,...
#ifndef PHONE_H_INCLUDED
#define PHONE_H_INCLUDED
#include <iostream>
#include <ctime>
#include <string>
#include "phone.h"
using namespace std;
class PhoneNumber;
class PelephonePN;
class CellcomPN;
class OrangePN;
class HotPN;
class BezeqPN;
class PhoneManager
{
private:
PelephonePN* mpPelephone;
CellcomPN* mpCellcom;
OrangePN* mpOrange;
HotPN* mpHot;
BezeqPN* mpBezeq;
public:
PhoneManager();
~PhoneManager();
void split_check_data(string str);
};
#endif
and .cpp
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include "phone_manager.h"
#include "phone.h"
using namespace std;
PhoneManager::PhoneManager()
{
srand(time(0));
mpPelephone = new PelephonePN();
mpCellcom = new CellcomPN();
mpOrange = new OrangePN();
mpHot = new HotPN();
mpBezeq = new BezeqPN();
mpPelephone->add(mpCellcom);
mpPelephone->add(mpOrange);
mpPelephone->add(mpHot);
mpPelephone->add(mpBezeq);
mpBezeq->setNext(mpPelephone);
}
To instantiate an object, forward declaration is just insufficient. Include the corresponding headers in the source file. In the body of the constructor, you are instantiating mpPelephone, ..... So, make sure that corresponding class headers can be seen in the translation unit.
In your .cpp source file you need to #include the headers that define class PelephonePN and its associates.
It is fine to forward-declare these classes in the header if you are only using them as pointers or references, but one you start using them in your implementation, you'll need to provide the compiler with the definition.
You can't use a forward declaration of a class to access its members (which includes the constructor, default or otherwise) or its size and without those two things, you can't instantiate an instance of that class.
You require its FULL implementation to do that, so in your .cpp file you need to include the header with the whole class PelephonePN {/* class body */}; section.
in the first file you have lines:
#ifndef PHONE_H_INCLUDED
#define PHONE_H_INCLUDED
I suppose you have same lines in phone.h, please check...
Are your PelephonePN and co. in a particular namespace? If so, predeclarations must all be in the same namespace. Since you added a using namespace std (sigh), my guess is that phone.h is defining your classes inside the namespace std (I hope it doesn't, but that's another thing).
If so, your predeclarations must be:
namespace std {
class PelephonePN;
}
Btw, what is a pelephone??? O.O
game.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
game.h
#ifndef GAME_H
#define GAME_H
#include <string>
class Game
{
private:
string white;
string black;
string title;
public:
Game(istream&, ostream&);
void display(colour, short);
};
#endif
The error is:
game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type
Your using declaration is in game.cpp, not game.h where you actually declare string variables. You intended to put using namespace std; into the header, above the lines that use string, which would let those lines find the string type defined in the std namespace.
As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead
string does not name a type. The class in the string header is called std::string.
Please do not put using namespace std in a header file, it pollutes the global namespace for all users of that header. See also "Why is 'using namespace std;' considered a bad practice in C++?"
Your class should look like this:
#include <string>
class Game
{
private:
std::string white;
std::string black;
std::string title;
public:
Game(std::istream&, std::ostream&);
void display(colour, short);
};
Just use the std:: qualifier in front of string in your header files.
In fact, you should use it for istream and ostream also - and then you will need #include <iostream> at the top of your header file to make it more self contained.
Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.
The namespace in game.cpp is after the header is included.
You can overcome this error in two simple ways
First way
using namespace std;
include <string>
// then you can use string class the normal way
Second way
// after including the class string in your cpp file as follows
include <string>
/*Now when you are using a string class you have to put **std::** before you write
string as follows*/
std::string name; // a string declaration
This has been stumping me for a while. I am trying to create a function that takes a hash table, and returns said hash table. However I am getting this error in the header file,
error: ‘string’ was not declared in this scope.
error: template argument 1 is invalid
Here is the header file itself:
#ifndef NAME_SPAWN_H
#define NAME_SPAWN_H
#include <QString>
#include <QHash>
#include <string>
class Name_Spawn
{
public:
Name_Spawn();
void initalize();
private:
QString int_2_str(int);
void seed();
QHash<string,QString> setTable(QHash<string,QString>);
};
#endif // NAME_SPAWN_H
As you can see, string has been declared. Any ideas? I am at my wits end.
The real name of string is std::string. Try using that instead.
(You can leave off the std:: qualifier only if there's a using namespace std; directive in scope. But it's a good habit not to put using namespace std; in header files.)