Hiho,
I have implemented a singleton class in C++ which has got a problem with an initialization within its constructor where I try to initialize a member with a custom constructor but instead the compiler thinks I want to call the default constructor (without arguments) and (of course) can't find it as I don't need a default constructor for that class.
Here is the code:
class CionTokenTypes final {
private:
CionTokenTypes();
TokenType init_tt(TokenType token_type, bool skipped = false);
std::vector<TokenType> m_all_token_types;
std::vector<TokenType> m_skipped_token_types;
static const CionTokenTypes c_instance;
public:
CionTokenTypes(CionTokenTypes const&) = delete;
CionTokenTypes(CionTokenTypes &&) = delete;
static CionTokenTypes const& get_instance();
std::vector<TokenType> const& get_all() const;
std::vector<TokenType> const& get_skipped() const;
TokenType const my_custom_token_type;
};
Here is the source file:
const CionTokenTypes CionTokenTypes::c_instance = {};
TokenType CionTokenTypes::init_tt(
TokenType token_type,
bool skipped
) {
m_all_token_types.push_back(token_type);
if (skipped) {
m_skipped_token_types.push_back(token_type);
}
return std::move(token_type);
}
CionTokenTypes const& CionTokenTypes::get_instance() {
return c_instance;
}
std::vector<TokenType> const& CionTokenTypes::get_all() const {
return m_all_token_types;
}
std::vector<TokenType> const& CionTokenTypes::get_skipped() const {
return m_skipped_token_types;
}
CionTokenTypes::CionTokenTypes():
m_all_token_types{},
m_skipped_token_types{},
my_custom_token_type{init_tt({"bracket: closing bracket ]", "\\]"})}
{}
And you might also want to see the TokenType class:
class TokenType final {
public:
enum class MatchType : uint8_t {
non_greedy,
greedy
};
TokenType(
std::string const& name,
std::string const& regex = "",
TokenTypeStore store_type = TokenTypeStore::empty,
MatchType match_type = MatchType::non_greedy);
TokenType() = delete;
TokenType(TokenType const& other) = default;
TokenType(TokenType && other) = default;
TokenType & operator=(TokenType const& other) = default;
TokenType & operator=(TokenType && other) = default;
};
And its source file:
TokenType::TokenType(
std::string const& name,
std::string const& regex,
TokenTypeStore store_type,
TokenType::MatchType match_type
):
m_data{
std::make_shared<TokenType::Data>(
name,
regex,
store_type,
match_type
)
}
{}
I have cut out the not important parts as these files would have been just too huge for a complete paste at StackOverflow.
The compiler error:
/home/robbepop/coding/c++/projects/cion/src/token/cion_token_types.cpp: In constructor ‘cion::CionTokenTypes::CionTokenTypes()’:
/home/robbepop/coding/c++/projects/cion/src/token/cion_token_types.cpp:207:69: error: use of deleted function ‘cion::TokenType::TokenType()’
closing_brack {init_tt({"bracket: closing bracket ]" , "\\]"})}
^
In file included from /home/robbepop/coding/c++/projects/cion/include/token/cion_token_types.hpp:4:0,
from /home/robbepop/coding/c++/projects/cion/src/token/cion_token_types.cpp:1:
/home/robbepop/coding/c++/projects/cion/include/token/token_type.hpp:56:3: note: declared here
TokenType() = delete;
^
/home/robbepop/coding/c++/projects/cion/src/token/cion_token_types.cpp:207:69: error: use of deleted function ‘cion::TokenType::TokenType()’
closing_brack {init_tt({"bracket: closing bracket ]" , "\\]"})}
^
In file included from /home/robbepop/coding/c++/projects/cion/include/token/cion_token_types.hpp:4:0,
from /home/robbepop/coding/c++/projects/cion/src/token/cion_token_types.cpp:1:
/home/robbepop/coding/c++/projects/cion/include/token/token_type.hpp:56:3: note: declared here
TokenType() = delete;
^
src/CMakeFiles/cion_compiler.dir/build.make:1066: recipe for target 'src/CMakeFiles/cion_compiler.dir/token/cion_token_types.cpp.o' failed
make[2]: *** [src/CMakeFiles/cion_compiler.dir/token/cion_token_types.cpp.o] Error 1
CMakeFiles/Makefile2:75: recipe for target 'src/CMakeFiles/cion_compiler.dir/all' failed
make[1]: *** [src/CMakeFiles/cion_compiler.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
make 62.10s user 5.93s system 98% cpu 1:08.80 total
I hope you can help me as I really don't know why the compiler thinks that I want to call the default constructor instead of my custom one ...
I finally found out what's the source of this strange compiler error:
It is in fact really strange. The source of the problem was that the header file declared about one hundred members of the CionTokenTypes class but they got initialized in the wrong order and some got even left out in the source file of the class which resulted in the error that the compiler can't find the default constructor for the last member.
So I guess this confusing was mainly caused by a poor error handling for this situation.