0

I have a project in which I need to get Enum as an input from the user and send it to a constructor.. I searched , didn't find something like this on this website,so here is my Class containing Enums, which compiles:

class Worker {
public:
string myGender;
enum workerType {DIRECTOR,  ACTOR, COPYWRITER, PRODUCER} myType;
Worker::Worker(workerType type,string gend)
{
myType = type;
myGender = gend;
}

this is the relevant method from my main class, from which i perform actions, and this one compiles as well:

void MovieIndustry::addWorker(Worker::workerType type, string gend)
{    
         workers.push_back(Worker(type, gend));
         break;
}

From my main I want to get an input like: 0, male

and to add a Worker instance with parameters myType = DIRECTOR myGender = male

I tried this, and it doesn't compile:

#include "MovieIndustry.h" //this is not the problem, works for other funcs
int main() {
MovieIndustry cinema;
int numForWorkertype;
string stringForGender;
cin >> numForWorkertype >> stringForGender;
cinema.addWorker(numForWorkertype ,stringForGender);
return 0;
}

serInterface.cpp:54:64: error: no matching function for call to  
‘MovieIndustry::addWorker(int&, std::string&)’

HELP..?

Zohar Argov
  • 143
  • 1
  • 1
  • 11
  • 1
    Regarding the values of that `gender` enumeration, [you need to read this](http://stackoverflow.com/questions/3960954/c-multicharacter-literal). – Some programmer dude Dec 01 '15 at 12:35
  • 1
    As for your problems, enumerations are always integers, no matter how you try to initialize them. You simply can't use a string in place of an enumeration. – Some programmer dude Dec 01 '15 at 12:37
  • ok I changed the gender, but how do i get the workerType from the user ? I still need to get it as an int ? – Zohar Argov Dec 01 '15 at 12:39

1 Answers1

0

numForWorkertype is int type. Cast to Worker::workerType.

cinema.addWorker( (Worker::workerType)numForWorkertype ,stringForGender);
EylM
  • 5,967
  • 2
  • 16
  • 28