0

I having a trouble trying to insert an object from a class that I created to a char. I create a class name Element as part of a bigger program, but when I try to overloading operator= so that i can get in char variable the char I got in the Element obj nothing work...

Element.h

class Element {

private:
    char val;
public:
    Element();
    Element(char newVal); //check input - throw execption
    ~Element();

friend ostream& operator<<(ostream& os, const Element& obj);
void setElement(char newVal);
char getElement();

void operator= (const Element newVal);
void operator= (char newVal);


};

Element.cpp

#include "Element.h"



Element::Element()
{
    val ='.';
}

Element::Element(char newVal)
{
    if (newVal!='X' && newVal!='O'&& newVal!='.'){
        inputExecption error;
        error.what();
    } else{
        val=newVal;
    }
}

Element::~Element()
{

}

void Element::setElement(char newVal)
{
    if (newVal!='X' && newVal!='O'&& newVal!='.'){
        inputExecption error;
        error.what();
    } else{
        val=newVal;
    }
}

char Element::getElement()
{
    return val;
}

ostream& operator<<(ostream& os, const Element& obj)
{

    return os << obj.val;

}

void Element::operator= (const Element Value){
    val = Value.val;
}

void Element::operator= (char newVal){
    if(val != 'X' && val != 'O'){
        inputExecption a;
        a.what();
    }
    val = newVal;
}


So as I said, what i'm trying to do is:

Element x='X';
char c = x; //(c=x.val)
cout<<c<<endl;//Will print X

thx! :)

newInteger
  • 43
  • 1
  • 10
  • 3
    `void operator= (const Element newVal);` should be `Element& operator=(const Element& newVal);` . – Jesper Juhl May 03 '18 at 16:43
  • 2
    More detail on what @JesperJuhl brought up (and generally good stuff to know): [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 May 03 '18 at 16:45
  • `inputExecption error; error.what();` is unusual. You should take a closer look at it. [Possibly have a chat with your Rubber Duck.](https://en.wikipedia.org/wiki/Rubber_duck_debugging) – user4581301 May 03 '18 at 16:48
  • @JesperJuhl thx, will change it :) – newInteger May 03 '18 at 16:50
  • @user4581301 thx!! will look and read it! – newInteger May 03 '18 at 16:50
  • Your user defined destructor does nothing. Why don't you either leave it out entirely or declare it as `~Element() = default;` ? That way it would be trivial. The way you have it now, it is not. – Jesper Juhl May 03 '18 at 16:51
  • @user4581301 and I know, its not the final code, its just there for me, to know what to change later :) – newInteger May 03 '18 at 16:51
  • Good enough for me. – user4581301 May 03 '18 at 17:04

2 Answers2

5

In other words you're trying to convert an object of type Element to an object of type char. You can use a conversion operator for this:

class Element {
  // ...
  operator char() const { return val; }
};

The assignment operator only works when an instance of Element is used on the left hand side of the assignment.

David G
  • 94,763
  • 41
  • 167
  • 253
  • @0x499602D2 What about implicit conversion in the situation when some overloaded function than can accept both `Element` and `char` is declared in two separate header files? – shycha May 03 '18 at 19:38
2

IMHO, writing an implicit conversion operator (as 0x499602D2 suggested) is not the best idea.

For example, assume you have two separate file something like this:

//File char_utils.hh
#include <iostream>
void accept(char character) { std::cout <<"character: " <<character <<'\n'; }


//File elements_utils.hh
#include <iostream>
void accept(Element const& element) {
    std::cout <<"element.getVal(): " <<element.getVal() <<'\n';
}

Then, depending on what you include in your implementation file (char_utils.hh or elements_utils.hh) you will get different behaviour, which may lead to many subtle bugs and/or interesting behaviours. ;)

This can be overcome by declaring conversion operator explicit, i.e.,

explicit operator char() const { return val; }

In such a case, you just need to:

Element e{'x'};
char c = static_cast<char>(e);

which shows the intent more clearly.

However, why do you need using an implicit conversion at all? Especially when you can use Element::getElement()? And if I may suggest, I would use different namin, e.g., Element::getValue(). Element::getElement() is very confusing.

Further reading: https://stackoverflow.com/a/16615725/1877420

shycha
  • 440
  • 5
  • 13
  • Thx for all the suggestion! About the last one, Element is part of bigger Object and In the main of the program I don't intend to use his functions. About the name yap u right haha I even didnt notice that, thx again ! – newInteger May 04 '18 at 07:04