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(TokenType const& 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 is ultimatively strange. It is an error-loop and repeats itself about 1500 lines long (I have output it in a file which was about 1500 lines long).
The repeating part states this:
src/token/cion_token_types.cpp:210:52: error: no matching function for call to ‘cion::TokenType::TokenType()’
my_custom_token_type{"bracket: closing bracket ]", "\\]"}
^
src/token/cion_token_types.cpp:210:52: note: candidates are:
In file included from include/token/cion_token_types.hpp:4:0,
from src/token/cion_token_types.cpp:1:
include/token/token_type.hpp:57:3: note: cion::TokenType::TokenType(const cion::TokenType&)
TokenType(TokenType const& other) = default;
^
include/token/token_type.hpp:57:3: note: candidate expects 1 argument, 0 provided
include/token/token_type.hpp:50:3: note: cion::TokenType::TokenType(const string&, const string&, cion::TokenTypeStore, cion::TokenType::MatchType)
TokenType(
^
include/token/token_type.hpp:50:3: note: candidate expects 4 arguments, 0 provided
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 ...
Aucun commentaire:
Enregistrer un commentaire