-2

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
       ^~~~~~~~~~~~
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • 1
    Reread your constructors definition carefully, you have a typo. – Baum mit Augen Oct 12 '16 at 23:24
  • 2
    Also, `_ReturnObject` is an implementation reserved identifier, don't use that. – Baum mit Augen Oct 12 '16 at 23:25
  • @BaummitAugen I am sorry, I do not follow. Are you possibly refering to the possiblity that I forgot to add the `*` to `const char* data`, if yes then I did not forget, I was made aware of its existance and meaning a moment ago from an answer given (hence I am still new to C++), if no then please assist me in shedding some light onto the error. Thanks – CybeX Oct 12 '16 at 23:31
  • Yes, that's what I was talking about. If you left that out on purpose, what is your question? – Baum mit Augen Oct 12 '16 at 23:33
  • @BaummitAugen I have found my answers, thank you for the assistance, even though I have a -2 on the question, there is bound to be someone who will find this question a great deal helpful. Thanks once again! – CybeX Oct 12 '16 at 23:45
  • After your edit: It would be better to make a new question for that, because it is not related to the string issue and might be more complicated. Does it happen for `std::string` and `QString` too? I highly recommend not using `const char*` for strings. It results in many many more complications that will be extremely hard to detect and confusing for beginners. Also include the content of `enums.h` in the new question. –  Oct 13 '16 at 04:11

3 Answers3

2

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.

  • Where do I begin, thank you for the explanation. This has helped me a great deal. In C# and Java, we were taught to use `new` but never really understood what it meant up until now. Thank you! – CybeX Oct 12 '16 at 23:40
  • 1
    @KGCybeX C++ is different, the meaning of `new` is quite different here. In Java an object is destroyed in the background at some point after it isn't used anymore (garbage collection). In C++ object are destroyed deterministically (when you leave their scope) or if you explicitly ask for it ('delete'). This means that you have to make sure that objects you are trying to use actually still exist. References for example work quite differently for this reason, too. –  Oct 12 '16 at 23:45
  • 1
    @KGCybeX I addded some information to my answer since you are using the QT library. –  Oct 12 '16 at 23:59
0

You can load a simple character into a char type (e.g. 'a'). If you want a string you must use char* or std::string. e.g. char* data_string = "WARN";

  • so char (from e.g. java) refers to a single character, then what does char* mean? char[] ? – CybeX Oct 12 '16 at 23:28
  • 1
    char* refers to a memory portion as every pointer in c/c++ does. I assume you've just started to work in c++ after java, so what i can recommend is you should learn about pointers in c++ (as it's one of the biggest differences between the two languages). And maybe you could also read about smart pointers, which were introduced in c++11 and provides more safely memory management – mhajnal Oct 12 '16 at 23:31
  • apologies but I need a little more than that. How does `char` refer to `''` or a single char but `char*` refers to `""` or "a string message"? – CybeX Oct 12 '16 at 23:35
  • 2
    I've edited my previous comment before, so maybe you've already got a kind of "abstract" answer for that. Basically the "char" refers to a 1 byte size already allocated variable, what already can use. The "char*" refers to a memory address/portion what contains multiple "char" elements. Therefore a "char" can mean only a simple character, but a "char*" means multiple chars. And as you've asked before, you can handle char arrays (char[]) as "char*", but this is not true backwards, so you shouldn't handle a char* as a char[], because noone can guarantee it will work (correctly). – mhajnal Oct 12 '16 at 23:37
  • thank you for the insight, I understand now. Thank you! – CybeX Oct 12 '16 at 23:42
  • 1
    ""char*" means multiple chars" Is a bit off. Better wording would be "`char*` means a reference to a space in memory that could contain 0 or more `char`s, and it has no idea how many." @KGCybeX many things you learned in Java you will have to unlearn for C++. It doesn't hold your hand and it doesn't pick up after you. It does exactly what you tell it to do when you tell it to do it. It does nothing more, nothing less. Java will throw nice little exceptions when you reach past the end of a memory allocation. If you are lucky, C++ will crash. If you've asked for something silly, it does it. – user4581301 Oct 12 '16 at 23:57
  • Yes, I didn't write a 100% precise definition for it. My purpose was to give a simple explanation about it to make him understand the situation on a basic level and I seemed to reach it. – mhajnal Oct 13 '16 at 00:27
0

This error comes from the type of object that you give as an argument. The second parameter of the constructor of ReturnObject is a char, but you give a "", which is an empty string (an array of char, namely a "char *").

If you write '' instead of "", your program will compile as '' is used to indicate an empty char. But I'm not sure this is what you need to implement...

If you want the second argument of ReturnObject constructor to be a string, then you should replace the prototype

ReturnObject(ReturnCode enum_code, const char data);

by

ReturnObject(ReturnCode enum_code, const char * data);

or better by

ReturnObject(ReturnCode enum_code, const QString data);

If you do that, don't forget to change the type of const char data_string by const char * data_string or QString data_string in the header according to your choice. In the same way, const char getData(); should also by replaced in the header and the implementation by const char * getData(); or const QString getData(); respectively.

Bruno
  • 31
  • 3
  • "an array of char, namely a "char *"" Untrue. It is a `const char *`. String Literals may reside in memory that may not be writable, thus constant. You clearly get that later in the question, but you know... TL;DR.. best to put this distinction out upfront. – user4581301 Oct 13 '16 at 00:01
  • thanks, with the other answers I understand my problem, with your solution I have found what I was looking for. Thank you, if you have a moment, and this will fork my question, but after applying the proposed `const char * data` solution, I have run into an issue, searching for a solution has proved fruitless. You opinion would be greatly valued, if you care to assist. Also I am aware this is against SO policy, but creating a new question for this seems illogical since I presume it is a small error – CybeX Oct 13 '16 at 01:04