KGCybeX KGCybeX - 1 month ago 9
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:

Header

#ifndef RETURNOBJECT_H
#define RETURNOBJECT_H

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

class ReturnObject
{
public:
ReturnObject();
ReturnObject(ReturnCode enum_code, const char data);

const char getData();
ReturnCode getCode();

private:
ReturnCode e_code;
const char data_string;

};

#endif // RETURNOBJECT_H


Implementation

#include "returnobject.h"

ReturnObject::ReturnObject(){
data_string="WARN";
}

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.

Purpose:

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

Issue/Problem/Question

calling the class with:

Private
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 ->
""
or
data
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
const
property.

What I have tried:

I have attempted using
char
,
const char
,
QString
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

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.