I have a parametrized class Queue and a subclass ClientsQueue not parametrized that inherits from Queue. I think I have a syntax error:
client.h
#ifndef CLIENT_H_
#define CLIENT_H_
class Client {
public:
Client();
~Client();
};
#endif
queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
template <class T> class Queue {
public:
Queue();
~Queue();
};
#endif
clientsQueue.h
#ifndef CLIENTSQUEUE_H_
#define CLIENTSQUEUE_H_
#include "queue.h"
#include "client.h"
class ClientsQueue: public Queue<Client> {
public:
ClientsQueue();
~ClientsQueue();
};
#endif
clientsQueue.cpp
#include "clientsQueue.h"
ClientsQueue::ClientsQueue() {
};
bank.cpp
#include "clientsQueue.cpp"
int main() {
return 0;
}
So, when I try to compile and run the program, the compiler says:
clientsQueue.cpp:3:1: error: ‘ClientsQueue’ does not name a type
ClientsQueue::ClientsQueue() {
^
I can't see the error. If I quit all the code from clientsQueue.cpp, it works.
How can I fix it?
Thanks!
You should #include "clientsQueue.h" not #include "clientsQueue.cpp" in main file. When you include header you present a declarations to the compiler. You miss the declaration of the class ClientsQueue when you include just the source (cpp) file.
Related
This is driving me insane, I have a class called Model, a class called View, and a header file for something GameCommand. I've included the right header guards and everything as far as I can tell but I keep getting an unknown type name error
Model.h
#ifndef MODEL_H
#define MODEL_H
class Model
{
public:
Model(); //default constructor
};
#endif
Model.cpp
#include "Model.h"
#include <iostream>
using namespace std;
Model::Model() //default constructor
{
whatever
}
View.h
#ifndef VIEW_H
#define VIEW_H
class View
{
public:
View();
};
#endif
View.cpp
#include "View.h"
#include <iostream>
using namespace std;
View::View()
{ whatever
}
GameCommand.h
#ifndef GAMECOMMAND_H
#define GAMECOMMAND_H
#include "Model.h"
#include "View.h"
void DoGoCommand(Model&, View&);
error: unknown type name 'Model'
void DoGoCommand(Model&, View&);
^
error: unknown type name 'View'
void DoGoCommand(Model&, View&);
^
I feel like I've tried everything, is there something I am just not seeing here?
You can try to forward declare the classes.
class Model;
class View;
in your GameCommand.h
I have this 2 separated class and i cant fix this compiler problem:
In file included from classA.cpp:2:0:
classB.h:6:10: error: 'string' in namespace 'std' does not name a type
std::string str;
^
In file included from classA.cpp:3:0:
classA.h:6:25: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
classB *ptr_b = new classB;
There is the classA.h:
#ifndef CLASSA_H
#define CLASSA_H
class classA {
private:
classB *ptr_b = new classB;
public:
classA();
};
#endif /* CLASSA_H */
classA.cpp:
#include "classB.h"
#include "classA.h"
classA::classA() {
}
classB.h:
#ifndef CLASSB_H
#define CLASSB_H
class classB {
private:
std::string str;
public:
classB();
};
#endif /* CLASSB_H */
classB.cpp:
#include <string>
#include "classB.h"
classB::classB() {
}
I apreciate all the help you can give. I don´t know how to fix this and I'm going crazy.
Thanks for reading.
You need #include <string> in classB.h. Right now classA.cpp includes classB.h with no prior #include <string> anywhere, so the included reference to std::string in classB.h causes an error.
In general, if a name is used in a header foo.h, you should include the header bar.h that declares the name in foo.h, or forward-declare the name in foo.h. Otherwise everyone else who includes foo.h will need to remember to make sure bar.h gets included first.
My previous question was asked wrong, so I'll post it fixed.
I have this example throwing
expected class-name before ‘{’ token
error while compiling. I am understanding why is it fails, but I don't know how to fix it. Thank you.
BaseClass.h
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h"
class ElementClass;
class BaseClass
{
private:
ElementClass *m_someField;
};
#endif
ElementClass.h
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h"
class ChildClass;
class ElementClass
{
private:
ChildClass *m_class;
};
#endif
ChildClass.h
#ifndef INHERITTEST_CHILDCLASS_H
#define INHERITTEST_CHILDCLASS_H
#include "BaseClass.h"
class ChildClass : public BaseClass
{
};
#endif
You have circulary dependent .h files.
In BaseClass.h:
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h" // Includes ElementClass.h
In ElementClass.h:
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h" // Which included BaseClass.h
You can remove those #include lines since you are using those classes by pointers only and a forward declaration is sufficient for that purpose.
When you're working with inheritance the following
#include "ChildClass.h"
class ChildClass;
is unnecessary, if you're going to break these into sepperate source files (which it looks like you are) you can say
#include "ElementClass.h"
in the source file of your derived class
I have a bit of code of the following format contained in a single .h and .cc file:
myClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
class myClass
{
public:
myClass(); // constructor
~myClass(); // destructor
void classMethod1 ();
void classMethod2 ();
int memberVarable1;
int memberVariable2;
};
#endif
and myClass.cc:
#include "myClass.h"
myClass::myClass(){
// stuff
}
myClass::~myClass(){
// stuff
}
void myClass::classMethod1 (){
// stuff
}
void myClass::classMethod2 (){
// stuff
}
All of this is working fine. However my project is getting quite large and I'm about to add a set of new functionality. Instead of clogging up myClass.h and myClass.cc I want to put some new methods in another .cc file. I don't seem to be able to get this to work though.
myClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "secondFile.h"
class myClass
{
public:
myClass(); // constructor
~myClass(); // destructor
void classMethod1 ();
void classMethod2 ();
int memberVarable1;
int memberVariable2;
};
#endif
and myClass.cc:
#include "myClass.h"
#include "secondFile.h"
myClass::myClass(){
// stuff
}
myClass::~myClass(){
// stuff
}
void myClass::classMethod1 (){
// stuff
}
void myClass::classMethod2 (){
// stuff
}
secondFile.h:
#ifndef SECONDFILE_H
#define SECONDFILE_H
void someNewMethod();
#endif
secondFile.cc
#include "secondFile.h"
void someNewMethod(){
// can't see classMethod1()
}
In every source file, you need to include every header file that declares functions, etc. you want to use.
So in your case, it seems like you want secondFile.cc to contain
#include "myClass.h"
#include "secondFile.h"
void someNewMethod(){
// can't see classMethod1()
}
Btw, what you are doing is quite common to do in practice. Sometimes, I go even further than what you suggest, and implement the various methods of a single class in multiple source files. For large, complicated classes, this helps speed up the development cycle because I only have to recompile a fraction of the class implementation if I only made a small change. Example:
myclass.h
#pragma once
class MyClass
{
...
void complicatedMethod0();
void complicatedMethod1();
...
};
myclass_impl0.cpp
#include "myclass.h"
void MyClass::complicatedMethod0()
{
...
}
myclass_impl1.cpp
#include "myclass.h"
void MyCLass::complicatedMethod1()
{
...
}
If you are intending to add methods to myClass, you can't do that - the class methods have to be contained in one definition.
You can extend myClass, however, by inheriting from it:
secondFile.h:
#ifndef SECONDFILE_H
#define SECONDFILE_H
#include "myClass.h"
class mySecondClass : public myClass
{
public:
void someNewMethod();
}
#endif
secondFile.cc
#include "secondFile.h"
void mySecondClass::someNewMethod(){
this.classMethod1();
}
#include "secondFile.h"
#include "myClass.h"
//if you want the class methods,
//you need to tell the compiler where to look
void someNewMethod(){
// can't see classMethod1()
}
it seems you've forgotten to include "myClass.h".
So I am getting the following errors:
..\Actor.h:35: error: `Attack' is not a member of `RadiantFlux'
..\Actor.h:35: error: template argument 1 is invalid
..\Actor.h:35: error: template argument 2 is invalid
..\Actor.h:35: error: ISO C++ forbids declaration of `attacks' with no type
On this line (among others):
std::vector<RadiantFlux::Attack> attacks;
Here are the relevant files:
Actor.h:
#ifndef ACTOR_H_
#define ACTOR_H_
#include <string>
#include <vector>
#include "Attack.h"
namespace RadiantFlux {
...
class Actor {
private:
std::string name;
int health;
std::vector<RadiantFlux::Attack> attacks;
Attributes attributes;
public:
...
};
}
#endif /* ACTOR_H_ */
Attack.h:
#ifndef ATTACK_H_
#define ATTACK_H_
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Actor.h"
namespace RadiantFlux {
...
class Attack {
private:
...
public:
...
};
}
#endif /* ATTACK_H_ */
Why am I getting these errors and what can I do to fix them? I am assuming it has something to do with the namespaces...
You have a cyclic dependency of your header files.
Attack.h includes Actor.h and vice versa.
Use Forward Declaration of class to avoid circular dependency problems.
Since the OP's comments, here is what needs to be done:
class Actor;
class Attack
{
};
If your code fails to compile after doing this, You need to read the linked answer and Understand why the error and how to solve it. The linked answer explains it all.
The classes Actor and Attack both refer to each other, so you will need to add a forward declaration in one of the file.
For example, in Actor.h:
class Attack;
class Actor
{
...
};