I want to be able to use an enum class that is defined in one file, and used in others. When I try I only get this error: enum "Animal" has no member "Lion"
I can't find any posts that answer my question.
Here is an example of what I have in mind:
zooanimals.h
#pragma once
namespace Zoo
{
enum class Animal;
}
zooanimals.cpp
#include "zooanimals.h"
namespace Zoo
{
enum class Animal
{
Lion,
Elefant,
Monkey
};
}
zoo.h
#pragma once
namespace Zoo
{
class Visitor;
}
zoo.cpp
#include "zoo.h"
#include "zooanimals.h"
namespace Zoo
{
class Visitor
{
Animal favoriteAnimal = Animal::Lion;
};
}
You don't split enums in declaration and definition, so
enum class Animal
{
Lion,
Elefant,
Monkey
};
should be in the header, not in a source file.
Remember, when you include a header into a source file, this source file can only "see" what is declared in this header. In your case, when the compiler processes zoo.cpp, it can not "see" the values of Animal, because they are not in the header.
Related
I made a smaller reproducible version of the code that gave me these errosr: 'MyNamespace::MySecondClass': 'class' type redefinition, 'print': is not a member of 'MyNamespace::MySecondClass'. Is there any way of working around this problem?
// MyClass.h
#pragma once
namespace MyNamespace {
class MySecondClass {};
}
// MyClass.cpp
#include "MyClass.h"
#include <iostream>
using namespace std;
class MyNamespace::MySecondClass
{
public:
void print(const char* msg)
{
cout << msg << endl;
}
};
The problem is that in MyClass.h you define a class MySecondClass as an empty class. When you the define your class in MyClass.cpp you give a different definition, which contains some new members. This infringes the One Definition Rule (ODR).
Solution 1
remove {} in the header. This will tell the compiler that you declare that such a class exists but that it will be defined later. Your code would compile. Unfortunately if you’d include the header in other cpp, these could make only a very very limited use of MySecondClass.
Solution 2
define in the header the class with all its members (but without providing the implementation of the member functions:the signature is sufficient). This would allow the class to be used in whichever cpp that
would include it:
// MyClass.h
#pragma once
namespace MyNamespace {
class MySecondClass {
public:
void print(const char* msg);
};
}
You’d then define the members of the class in its cpp in the appropriate namespace:
// MyClass.cpp
#include <iostream>
#include "MyClass.h"
using namespace std;
namespace MyNamespace {
// member functions
void MySecondClass::print(const char* msg)
{
cout << msg << endl;
}
}
Remark: the include sequence in the cpp should first include the standard library headers, then only your own headers. It makes no difference in your simple example, but better get used the good practices immediately.
Not sure if this is possible, but I have two classes of the same name in different levels of a nested namespace and I'd like to make the more shallow class a friend of the deeper class. Example:
In File1.h:
namespace A
{
class Foo
{
//stuff
};
}
In File2.h:
namespace A
{
namespace B
{
class Foo
{
friend class A::Foo; //Visual Studio says "Error: 'Foo' is not a member of 'A'"
};
}
}
Is this possible? If so, what is the proper syntax?
This code compiles ok when placed in one file (except that a ; is necessary after A::B::Foo class): IdeOne example.
So, the issue is in the code not included in the question text. Probably #include "File1.h" was forgotten in File2.h.
If you want to avoid including large header files into others, you need to at least forward declare your classes before using them:
namespace A
{
class Foo;
namespace B
{
class Foo
{
friend class A::Foo;
}
}
}
I have a couple functions that I want to use in many different classes. I have a couple classes that are derived from one base class and so tried to make it so that the base class held the functions and then the child classes could just call them. This seemed to cause linking errors, and so following advice from this question (Advantages of classes with only static methods in C++) I decided to give namespaces a swing, but the only file that is included by every header/file is resource.h, and I don't want to put a namespace for my functions in there as it seems to specialised to mess with.
My question is, how do I make a class that only includes a namespace, or the functions I want to use, so that I can just include this class and use the functions as desired?
Thank you in advance for the help, the answers I've found on the internet only focus on one file, not multiple files like I'm hoping to address :)
You seem confused about how namespaces are used. Here are some things to keep in mind when working with namespaces:
You create a namespace using the syntax namespace identifier { /* stuff */ }. Everything between the { } will be in this namespace.
You cannot create a namespace inside a user-defined type or function.
A namespace is an open group construct. This means you can add more stuff into this namespace later on in some other piece of code.
Namespaces aren't declared unlike some of the other language constructs.
If you want certain classes and/or functions inside a namespace scope, enclose it with the namespace syntax in the header of where it's defined. Modules using those classes will see the namespace when the headers get #include'd.
For example, in your Entity.h you might do:
// Entity.h
#pragma once
namespace EntityModule{
class Entity
{
public:
Entity();
~Entity();
// more Entity stuff
};
struct EntityFactory
{
static Entity* Create(int entity_id);
};
}
inside your main.cpp you access it like this:
#include "Entity.h"
int main()
{
EntityModule::Entity *e = EntityModule::EntityFactory::Create(42);
}
If you also want Player to be inside this namespace then just surround that with namespace EntityModule too:
// Player.h
#pragma once
#include "Entity.h"
namespace EntityModule{
class Player : public Entity
{
// stuff stuff stuff
};
}
This works because of point #3 above.
If for some reason you feel you need to create a namespace inside a class, you can simulate this to an extent using nested classes:
class Entity
{
public:
struct InnerEntity
{
static void inner_stuff();
static int more_inner_stuff;
private:
InnerEntity();
InnerEntity(const InnerEntity &);
};
// stuff stuff stuff
};
Some important differences and caveats doing it this way though:
Everything is qualified with static to indicate there's no specific instance associated.
Can be passed as a template parameter.
Requires a ; at the end.
You can't create a convenient shorthand with abusing namespace Entity::InnerEntity;. But perhaps this is a good thing.
Unlike namespaces, class and struct are closed constructs. That means you cannot extend what members it contains once defined. Doing so will cause a multiple definition error.
You can put anything in a namespace, but you can't put namespaces inside things ( that's not a very formal way of saying it but I hope you get what I mean.
Valid
namespace foospace
{
class foo
{
public :
foo();
~foo();
void eatFoo();
};
}
Invalid
namespace foospace
{
class foo
{
public :
foo();
~foo();
namespace eatspace
{
void eatFoo();
}
};
}
I'm not 100% certain that the second example wouldn't compile, but regardless, you shouldn't do it.
Now, from your comments it sounds like you want something like this :
In the file Entity.h, your entity class definition :
namespace EntitySpace
{
class Entity
{
public :
Entity();
~Entity();
};
}
In the file Player.h
#include "Entity.h"
namespace EntitySpace
{
class Player : public Entity
{
public :
Player();
~Player();
};
}
In the file main.cpp
#include "Player.h"
int main()
{
EntitySpace::Player p1;
EntitySpace::Player p2;
}
So you call upon Player in the EntitySpace namespace. Hope this answers what you were asking.
I have been having a hard time trying to find anything similar to this question, so instead I will ask here.
I have a project with a dozen or so source/header files. The main problem I am having is predefining the classes that I have made in the namespace. The code is as followed:
"GlobalIncludes.h"
/*include dependencies and library headers...*/
/*[Note 1]How would I predefine the classes inside namespaces?*/
typedef std::tr1::shared_ptr<Class1> ClassPtr1;//[Note 2]
typedef std::tr1::shared_ptr<Class2> ClassPtr2;//[Note 2]
/*[Note 2]What is the correct way to predefine the shared_ptr's?*/
#include "Class1.h"
#include "Class2.h"
"Class1.h"
namespace myNamespace
{
class Class1
{
/*variables and functions*/
void doSomething(...);
Class2 exampleObject;
};
}
"Class2.h"
namespace myNamespace
{
class Class2
{
/*variables and functions*/
};
}
My apologies in advance if this sounds a bit confusing...
Basically I am wondering if it is possible to predefine the classes that are in namespace myNamespace and at the same time declare the shared_ptr's. If this is possible, how would I do this and use them correctly in the source?
If you want the type definitions to be part of the same namespace as the classes (which I suggest):
namespace my_namespace
{
class Class1;
class Class2;
typedef std::tr1::shared_ptr<Class1> ClassPtr1;
typedef std::tr1::shared_ptr<Class2> ClassPtr2;
}
#include "Class1.h"
#include "Class2.h"
Otherwise, if you want your pointer type definitions to be part of the global namespace
namespace my_namespace
{
class Class1;
class Class2;
}
typedef std::tr1::shared_ptr<my_namespace::Class1> ClassPtr1;
typedef std::tr1::shared_ptr<my_namespace::Class2> ClassPtr2;
#include "Class1.h"
#include "Class2.h"
Possibly, you could make things more compact with a macro (same namespace):
#define DECLARE_PTR_ALIAS(N, C, P) \
namespace N { class C;
typedef std::tr1::shared_ptr<C> P; } \
Or (different namespace):
#define DECLARE_PTR_ALIAS(N, C, P) \
namespace N { class C; } \
typedef std::tr1::shared_ptr<N::C> P;
This would make it simpler to define pointer aliases for several classes:
DECLARE_PTR_ALIAS(my_namespace, Class1, ClassPtr1)
DECLARE_PTR_ALIAS(my_namespace, Class2, ClassPtr2)
...
To pre-declare classes (or functions, or etc...) in a namespace, you do this:
namespace myNamespace
{
class myClassA;
class myClassB;
}
If I'm just pre-declaring a single class, I like to put it on one line: (just personal preference)
namespace myNamespace { class myClassA; }
In your example, however, Class1 is already in a namespace, so you might as well do it like this:
namespace myNamespace
{
class Class2;
class Class1
{
/*variables and functions*/
void doSomething(...);
Class2 exampleObject;
};
}
The only problem is Class1 has Class2 as a member-variable. You can't use pre-declared objects as members, since the compiler needs to know the size of the object to embed it into the class. You can only use pre-declared objects as references or pointers, since those have fixed sizes.
If you made Class2 a smart pointer, then you can do so:
namespace myNamespace
{
class Class2;
typedef std::shared_ptr<Class2> Class2ptr;
class Class1
{
/*variables and functions*/
void doSomething(...);
Class2ptr exampleObject;
};
}
But the shared_ptr has to be fully included and can't be pre-declared, because the shared_ptrs are now member variables and the class needs to know their full size.
If I'm creating a static library with a header file such as this:
// Myfile.h
#include "SomeHeaderFile.h" // External library
Class MyClass
{
// My code
};
Within my own project I can tell the compiler (in my case, Visual Studio) where to look for SomeHeaderFile.h. However, I don't want my users to be concerned with this - they should be able to include my header without having to inform their compiler about the location of SomeHeaderFile.h.
How is this type of situation normally handled?
This is a classic "compilation firewall" scenario. There are two simple solutions to do:
Forward-declare any classes or functions that you need from the external library. And then include the external library's header file only within your cpp file (when you actually need to use the classes or functions that you forward-declared in your header).
Use the PImpl idiom (or Cheshire Cat) where you forward-declare an "implementation" class that you declare and define only privately (in the cpp file). You use that private class to put all the external-library-dependent code to avoid having any traces of it in your public class (the one declared in your header file).
Here is an example using the first option:
#ifndef MY_LIB_MY_HEADER_H
#define MY_LIB_MY_HEADER_H
class some_external_class; // forward-declare external dependency.
class my_class {
public:
// ...
void someFunction(some_external_class& aRef); // declare members using the forward-declared incomplete type.
};
#endif
// in the cpp file:
#include "my_header.h"
#include "some_external_header.h"
void my_class::someFunction(some_external_class& aRef) {
// here, you can use all that you want from some_external_class.
};
Here is an example of option 2:
#ifndef MY_LIB_MY_HEADER_H
#define MY_LIB_MY_HEADER_H
class my_class_impl; // forward-declare private "implementation" class.
class my_class {
private:
std::unique_ptr<my_class_impl> pimpl; // a vanishing facade...
public:
// ...
};
#endif
// in the cpp file:
#include "my_header.h"
#include "some_external_header.h"
class my_class_impl {
private:
some_external_class obj;
// ...
public:
// some functions ...
};
my_class::my_class() : pimpl(new my_class_impl()) { };
Say the external header file contains the following:
external.h
class foo
{
public:
foo();
};
And in your library you use foo:
myheader.h:
#include "external.h"
class bar
{
...
private:
foo* _x;
};
To get your code to compile, all you have to do is to forward declare the foo class (after that you can remove the include):
class foo;
class bar
{
...
private:
foo* _x;
};
You would then have to include external.h in your source file.