Advertisement

correct use of enums

Started by August 26, 2018 02:11 PM
2 comments, last by cmac 6 years, 3 months ago

I'm using enums for the first time to and I'm wondering how best to use them for classes. At the moment I'm encapsulating the meaning within classes (and duplicating for classes that will share it) - but I've run into a problem when assigning a enum type:

 


class mast {
	enum signal { E, G, H, HPLUS, FOURG };
private:
	signal capability;
	signal current_broadcast;
	bool power{ true };
public:
	mast();
	~mast() = default;
	signal getCapability();
	signal getBroadcast();
	void printCapability();
	void printBroadcast();
	void setCapability(signal s) {capability = s;}
	void setBroadcast(signal s) { current_broadcast = s; }
	bool getPower() { return power; }
	void setPower(bool b) { power = b; }
};

/*====================================================================================*/

class phone {
	enum signal { E, G, H, HPLUS, FOURG };
private:
	std::map<std::string,std::string> contacts;
	std::map<std::string, std::string> messages;
	signal signal_strength;
	bool wifi;
public:
	phone();
	~phone() = default;
	void boot(mast& m);
	void addContact(std::pair<std::string,std::string>);
	bool removeContact(std::string);
	void getContacts()
	{
		for (auto& it : contacts)
		{
			std::cout << "\nName: " << it.first << "\nNumber: " << it.second << std::endl;
			for (int i = 0; i < 20; ++i) std::cout << "-";
		}
	}
	void setSignal(signal s) { signal_strength = s; }
	void setWifi(bool b) { wifi = b; }
	bool getWifi() { return wifi; }
	void searchWifi() {};
};

//==========================================================================================

Are my 2 classes, however in the phone constructor  (see error line below) I get the following errors :

 error C3867: 'mast::getBroadcast': non-standard syntax; use '&' to create a pointer to member

 error C2440: '=': cannot convert from 'mast::signal (__thiscall mast::* )(void)' to 'phone::signal'

 


#include "phone.h"
#include "mast.h"
#include "rand_eng.h"

phone::phone()
{
	wifi = false;
}

void phone::boot(mast& m)
{
	signal_strength = m.getBroadcast; // THIS FLAGS ERROR
}

void phone::addContact(std::pair<std::string, std::string> entry)
{
	contacts.insert(entry);
}

bool phone::removeContact(std::string entry)
{
	std::map<std::string, std::string>::iterator it;
	it = contacts.find(entry);
	if (it != contacts.end())
	{
		contacts.erase(it);
		return true;
	}
	return false;
}

 

Any by the way the code for mast::getBroadcast is:

 

mast::signal mast::getBroadcast()
{
    return current_broadcast;
}

 

I've also tried putting the enum definition outside of the classes but it still says

 error C2440: '=': cannot convert from 'signal (__thiscall mast::* )(void)' to 'signal'

 

You get the error because you have two differrent enums. One is accessible with mast::siglanl and the other with phone::signal.

If you want to keep thes two enums and be able to convert values from one to another you should do this explicitly:


class A 
{
public:
  enum Items { one, two, three };
  Items v;
};


class B
{
public:
  enum Items { one, two, three };

  Items v;
};


int main ()
{

  A a;
  B b;

  a.v = A::one;
  b.v = (B::Items)a.v;
}

 

But, in my opinion, it's bad design decision. What if you add item into one enum but don't to the other?

If your signal enum is the same and should be the same, just move it to global space out of the class defenition. Or keep it in one of these two classes (not in both) and acces it with scope resolution operator (ClassName::EnumNameInside).

1 hour ago, ICanC said:

 error C2440: '=': cannot convert from 'mast::signal (__thiscall mast::* )(void)' to 'phone::signal'

You have these error because you trying to assign class method itself (pointer to function). I guess, you want to dot this:


void phone::boot(mast& m)
{
	signal_strength = m.getBroadcast();
}

 

Advertisement

Duplicating enums is a bad, difficult to maintain practice. Define them in a common scope/file, or include them from the owner's header file.

Also note you can forward declare enums so long as you specify the size of the type (eg. enum Items : int)

This topic is closed to new replies.

Advertisement