0

I have two files, gui.hpp and util.hpp. The source code is in respective .cpp files

gui.hpp

#pragma once
#include "util.hpp"
class Text_; //forward-declaration
typedef Text_* TextPtr;

//Things that use Text_ and TextPtr


class Text_{
//uses stuff from util.hpp
};

util.hpp

#pragma once
#include "gui.hpp"

std::vector<TextPtr> vec;

Unfortunately, this does not compile. They create random unrelated errors, so it's not worth posting them here. But, it all stems from each file depending on the other.

I can fix this by defining util.hpp as

#include "gui.hpp"

class Text_; //Forward-declare

std::vector<Text_*> vec;

Why do I need to forward-declare Text_ again when gui.hpp is already included? How can I let util.hpp use TextPtr instead of Text_*

Nathan29006781
  • 173
  • 1
  • 9
  • 1
    You have no header guard to avoid an inclusion loop. Think about adding '#pragma once' at the beginning of your header files. Most likely your "random unrelated errors" probably stem from having your definitions and declarations appear multiple times when your CCP files are compiled. – TommyD Feb 02 '22 at 15:54
  • @TommyD Sorry, there is actually `#pragma once`. I just forgot to include it in the example. – Nathan29006781 Feb 02 '22 at 16:01
  • `std::vector vec;` should not be in a header that gets included by more than 1 cpp file. – drescherjm Feb 02 '22 at 16:21

0 Answers0