0

Newcomer to C++ and I have created an enum for 'degree' with 3 types 'SECURITY, NETWORKING, SOFTWARE' that are returning a type error when the subclass is attempting to use the enum. What type is it specifically looking for?

//Network Student Subclass

#include <string>
#include "degree.h"
#include "student.h"

class NetworkStudent : public Student //This class derives from Student
{
public:
    NetworkStudent();
    NetworkStudent(
        string student_id,
        string first_name,
        string last_name,
        string email,
        double age,
        double* days,
        degree type
    );

    degree getdegree();
    void setdegree(degree d);
    void print();

    ~NetworkStudent();
}; 

//Enum with 3 Types
#include <string>

//The three types of degrees available
enum degree {SECURITY,NETWORKING,SOFTWARE};

static const std::string degreeTypeStrings[] = { "SECURITY","NETWORKING", "SOFTWARE" };

//Network Student definition with invalid type error for NETWORKING

#include "student.h"
#include "networkStudent.h"
using std::cout;

NetworkStudent::NetworkStudent()
{
//Right here is where I get the error on NETWORKING
    setdegree(NETWORKING);
}

Error states: argument of type "degree" is incompatible with parameter of type "degree". I thought degree was an enum and NETWORKING was also an enum.

Proteus
  • 5
  • 3
  • 1
    NETWORKING is not the enum itself. It is an entry in the enumerator list . Using scope resolution should solve this issue something like setdegree(degree::NETWORKING) (assuming there is an implementation of the setdegree function). Checkout the answers in this [thread](https://stackoverflow.com/questions/12183008/how-to-use-enums-in-c) – dorKKnight Jul 16 '19 at 16:57
  • Thanks for the quick reply. Seems the 'trick' of adding `degree::` before NETWORKING is what I needed. Is the `degree::` needed because of this: `void setdegree(degree d);` – Proteus Jul 16 '19 at 17:08
  • Glad it worked. Please, do not think this as some workaround trick. Basically the scope resolution operator (::) helps the compiler to find where exactly is the variable defined i.e. in which scope. This is a standard C++ way of doing this. Read more about it in this [SO answer](https://stackoverflow.com/questions/9338217/why-does-c-need-the-scope-resolution-operator) – dorKKnight Jul 16 '19 at 17:14
  • Thanks for taking the time to explain it to a newcomer. Much appreciated! – Proteus Jul 16 '19 at 20:57

1 Answers1

0

As others in the comments have mentioned, what you're looking for here is actually degree::NETWORKING. The reason is that NETWORKING is part of the enum degree, and not in the global space. Thus, you need to specify which NETWORKING you intend to use.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94