Hello im trying to build my own library in c++, and after many hours of searching and trying i learnt that a good way of doing one would be using nested classes, so i made some code in this form of tree: [file tree][1]
So i divided all the class files into different header files(i know this is harder and maybe not necessary but i wanted have it like that, if nothing more then just for training. And so to keep the nested class connected, but in different files, each header needs to include its child and or parent to function correctly. This didnt work, gave me errors of recursive include (duh) so i searched online and found #ifndef and #define and i used them in this way:
//A.hpp
#ifndef A_HPP_INCLUDED // some unique macro
#define A_HPP_INCLUDED
class A
{
public:
class B;
class C;
};
#include "B.hpp"
#include "C.hpp"
#endif
//B.hpp
#ifndef B_HPP_INCLUDED
#define B_HPP_INCLUDED
#include "A.hpp"
class A::B
{
public:
static int num;
void func();
};
#endif
int A::B num = 5;
//C.hpp
#ifndef C_HPP_INCLUDED // some unique macro
#define C_HPP_INCLUDED
#include "A.hpp"
class A::C
{
public:
void func();
};
#endif
But the second i add #include "B.hpp" into the B.cpp file it immediately gives me error that all variables and functions ive added in this way are defined multiple times. Without cpp files it works perfectly, but with them it crashes. In fact this happens when adding any cpp file to any header
Answers to this problem are welcome, but if you have any tips on how to make library files in a better way or even just random tips, thatd be awesome!
fyi im using qt creator, qmake, c++11. if qmake file is needed ill send it but i think everything there is good.
The files in question (for reproducing the problem):
<!-- language: lang-cpp -->
```c++
//testlibrary.hpp
#ifndef TESTLIBRARY_HPP_INCLUDED // some unique macro
#define TESTLIBRARY_HPP_INCLUDED
class TestLibrary
{
public:
class Core;
class SFML;
};
#include <Core/Core.hpp>
#include <SFML++/SFMLpp.hpp>
#endif
//Core.hpp
#pragma once
#ifndef CORE_HPP_INCLUDED // some unique macro
#define CORE_HPP_INCLUDED
#include <testlibrary.hpp>
class TestLibrary::Core
{
public:
class Constants;
};
#include "Constants.hpp"
#endif
//Constants.hpp
#pragma once
#ifndef CONSTANTS_HPP_INCLUDED // some unique macro
#define CONSTANTS_HPP_INCLUDED
#include "Core.hpp"
class TestLibrary::Core::Constants
{
public:
static float const_PI;
static float const_Euler;
Constants();
};
float TestLibrary::Core::Constants::const_PI = 3.1415;
float TestLibrary::Core::Constants::const_Euler = 2.71828;
#endif
//SFMLpp.hpp
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#ifndef SFML_HPP_INCLUDED // some unique macro
#define SFML_HPP_INCLUDED
#include <testlibrary.hpp>
class TestLibrary::SFML
{
public:
class Text;
};
//#include "Text.hpp"
#endif
//Text.hpp
#pragma once
#ifndef TEXT_HPP_INCLUDED // some unique macro
#define TEXT_HPP_INCLUDED
#include "SFMLpp.hpp"
class TestLibrary::SFML::Text : public sf::Text
{
public:
sf::Font font_;
Text();
Text(std::string string);
Text(std::string string, sf::Font &font);
Text(std::string string, sf::Font &font, int size);
Text(std::string string, sf::Font &font, int size, sf::Color color);
void setTextInt(int inputText);
void drawText(sf::RenderWindow &window);
};
#endif
//text.cpp
#include "text.hpp"
[![The error messages][2]][2]
[1]: https://i.stack.imgur.com/1lIux.png
[2]: https://i.stack.imgur.com/jF3Tm.png
Aucun commentaire:
Enregistrer un commentaire