KGCybeX KGCybeX - 1 year ago 87
C++ Question

type required : Passing String as parameter to function

Good day all

note: I am a begginer at c++, teaching myself as I go along, also apologies if this is a duplicate, but I have not found an example/answer/tutorial to answer question, possibly I just do not understand it yet.

I have a function with header and implementation defined below:



#include <QtCore>
#include "enums.h"

class ReturnObject
ReturnObject(ReturnCode enum_code, const char data);

const char getData();
ReturnCode getCode();

ReturnCode e_code;
const char data_string;




#include "returnobject.h"


ReturnObject::ReturnObject(ReturnCode enum_code, const char data)
: e_code(enum_code)
, data_string(data)

ResultCode ReturnCode::getCode()
return e_code;

const char ReturnObject::getData()
return data_string;

Please note, I am not as familiar with pointers,etc as I should be but I have a fair understanding of each concept.


This object is created by a custom class function, stored within the class and returned to the parent class. The object contains a custom
return value and possible output message/data from the class ( I created this, due to the limited restrictions on e.g.
allowing only a
return result of 1 or 0).


calling the class with:

ReturnObject _ReturnObject;

_ReturnObject = new ReturnObject(ReturnCode::LoginDialog_EmptyLoginPass, "");

provides the following error:

/home/cx/qt-projects/project-i/loginstatusdialog.cpp:21: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
_ReturnObject = new ReturnObject(ReturnCode::LoginDialog_EmptyLoginPass, "");

Thus, I conclude from the error that the second parameter ->
is the issue as I, most probably, do not properly define this datatype in the constructor.

How should I define this in the constructor?, rather which data type does
use? As I understand, it does take on a

What I have tried:

I have attempted using
const char
both which give a similar error.

Any suggestions?

Compiler output

after solution of
const char * data

../project-i/loginstatusdialog.cpp: In constructor 'LoginStatusDialog::LoginStatusDialog(QString, QString, QString*, QWidget*)':
../project-i/loginstatusdialog.cpp:21:44: error: use of deleted function 'ReturnObject& ReturnObject::operator=(ReturnObject&&)'
_Return_Object = ReturnObject(1, "");
In file included from ../project-i/loginstatusdialog.h:10:0,
from ../project-i/loginstatusdialog.cpp:1:
../project-i/returnobject.h:7:7: note: 'ReturnObject& ReturnObject::operator=(ReturnObject&&)' is implicitly deleted because the default definition would be ill-formed:
class ReturnObject

Answer Source

You say that you are not familiar with pointers yet, so I advise you against using char* for strings (that is the C-style of doing it), because it is heavily based on the concept of pointers.

Instead use std::string for which you have to #include<string> first. It is a class type which behaves much more friendly:

std::string data_string;

ReturnObject::ReturnObject(ReturnCode enum_code, std::string data)

std::string ReturnObject::getData()

and your code will compile.

const char is simply one single character and a constant one (unmutable) too. This is probably not what you wanted.

There is also no reason for you to use dynamic allocation (new) yet if you are not familiar with pointers. Instead just drop it to get static allocation:

_ReturnObject = ReturnObject(ReturnCode::LoginDialog_EmptyLoginPass, "");

new indicates that you want the created instance to stay alive until you call delete on a pointer to it, which can be anywhere in the program.

Without new the instance will be destructed as soon as the scope of the declaration of _ReturnObject is left (i.e. if the enclosing class instance is destroyed).

In your case the line will trigger a new error anyway because new returns not the object itself but a pointer to it and you are trying to assign that pointer to a non-pointer (_ReturnObject).

Also since you are using the Qt library, which I am not very familiar with, it may be better to use its string implementation QString. From looking at the documentation it seems to me that you should be able to use it directly inplace of std::string. std::string however is the standard library type that is always available with every C++ compiler and platform.