Reputation: 2386
note: I am still new to c++, and while this may be a simple issue, yet I am unable to find a solution.
Purpose:
I would like to pass an empty string (as one would in java/C#) to my constructor. I receive an error:
error: no matching function for call to 'ReturnObject::ReturnObject(ResultCode::ClientCode, const char [1])'
return new ReturnObject(ResultCode::ClientCode::enum_FailedOpeningClientSocket, "");
The ReturnObject
's purpose is to encapsulate an enum
and a string
.
What does this error mean and how can I solve it?
I have attempted changing my constructor parameter from QString data
to char data
and calling with ''
but that resulted in an error empty character constant
.
calling code:
return new ReturnObject(ResultCode::ClientCode::enum_FailedSocketConnection, "");
header:
class ReturnObject
{
public:
ReturnObject(ResultCode enum_code, QString data);
QString getData();
ResultCode getCode();
private:
ResultCode e_code;
QString data_string;
};
implementation
#include "returnobject.h"
ReturnObject::ReturnObject(){
data_string="WARN";
}
ReturnObject::ReturnObject(ResultCode enum_code, QString data)
: e_code(enum_code)
, data_string(data)
{}
ResultCode ReturnObject::getCode()
{
return e_code;
}
QString ReturnObject::getData()
{
return data_string;
}
Thanks to wasthishelpful and a few comments, I made a tragic logic error which had me looking at the wrong parameter, the solution is that I should casting my enum class ResultCode
which is the parent class to one of the nested class
es, in this case ClientCode
, as seen below from my enum class header
enum.h
#ifndef ENUMS_H
#define ENUMS_H
class ResultCode{
public:
enum class LoginDialogCode{
enum_LoginSuccess=0,
enum_InternetOffline=1,
enum_ServerOffline=2,
enum_InvalidLoginPass=3,
enum_EmptyLoginPass=4,
enum_FailedRetreivingServerList=5,
enum_TokenFailed=6
};
enum class ClientCode{
enum_SentSuccess=10,
enum_FailedOpeningClientSocket=11,
enum_FailedClientSocketConnection=12,
enum_FailedWritingtoClientSocket=13,
enum_FailedReadingfromClientSocket=14
};
enum class ServerCode{
enum_ReceivedSuccess=20,
enum_FailedOpeningListenSocket=21,
enum_FailedBindingtoListenSocket=22,
enum_FailedAcceptingListenSocket=23,
enum_FailedWritingtoListenSocket=24,
enum_FailedReadingfromListenSocket=25
};
};
#endif // ENUMS_H
Upvotes: 0
Views: 1257
Reputation: 15956
Your error is not on the second, but on the first parameter. From your question, I guess you have code like this:
struct ReturnCode
{
enum class ClientCode
{
enum_FailedSocketConnection,
// other values
};
};
So you ended up with two declared types: ReturnCode
and ReturnCode::ClientCode
. Looking at your constructor declaration:
`ReturnObject::ReturnObject(ResultCode enum_code, QString data)`
It needs an object of type ReturnCode
as first parameter, while looking at your call:
ReturnObject(ResultCode::ClientCode::enum_FailedSocketConnection, "")
You pass an object of type ReturnCode::ClientCode
as first parameter.
You may change your code like this:
class ReturnObject
{
public:
ReturnObject(ResultCode::ClientCode enum_code, QString data);
QString getData();
ResultCode::ClientCode getCode();
private:
ResultCode::ClientCode e_code;
QString data_string;
};
Once you are here. You may consider taking the enumeration out of ResultCode
:
enum class ClientCode
{
enum_FailedSocketConnection,
// other values
};
class ReturnObject
{
public:
ReturnObject(ClientCode enum_code, QString data);
QString getData();
ClientCode getCode();
private:
ClientCode e_code;
QString data_string;
};
This follows the Zen of Python: "Flat is better than nested". IMHO this is also true in C++.
EDIT:
From your comments, we're here on an XY problem, and your code needs to be redesign. Here is a first proposition:
#include <type_traits>
enum class ClientCode{
// ...
enum_FailedClientSocketConnection=12,
// ...
};
template<typename T>
struct ReturnObject
{
static_assert(std::is_enum<T>::value, "T should be an enum");
const T e_code;
const QString data_string;
};
template<typename T>
ReturnObject<T> make_return_object(T e_code, std::string data_string)
{
return ReturnObject<T>{e_code, data_string};
}
// usage
return make_return_object(
ClientCode::enum_FailedClientSocketConnection, ""
);
I removed the accessors getData
and getCode
for public const members: they are just read, and should not change for a given return object, so let them be public, with the const qualifier to prevent modification.
I used templates to represent the code, with static_assert
to check the given type is an enumeration.
The drawbacks are:
make_return_object
will return a different type for each different enumeration.Upvotes: 2