Thread: Access enum class and its values outside of the class.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    51

    Access enum class and its values outside of the class.

    I cannot wrap my head as to how to access my enum class Azimuth.

    Code:
    #ifndef BOUSOLE_H
    #define BOUSOLE_H
    
    #include <iostream>
    #include <string>
    #include "StringHandler.h"
    
    class Bousole{
    
        public:
            enum class Azimuth {NORD = 0,  //HOW TO ACCESS THESE
                EST = 90,
                SUD = 180,
                OUEST = 270,
                UNSET=-1};
    
            Bousole();
            Bousole(int deg);
            Bousole(Azimuth az);
            void setNord();
            void setEst();
            void setSud();
            void setOuest();
            void setAzimuth(int deg);
            void setAzimuth(Azimuth az);
            void display();
            Azimuth getAzimuth();
            std::string getPointcardinal();
            std::string toString();
            std::string toString(Azimuth az);
    
        private:
            int m_degre;
            Azimuth azimuth;
    };
    
    #endif
    And here is where I am trying to access my enum for testing/understanding purposes

    Code:
    #include "Bousole.h"
    
    using namespace std;
    
    int main (int argc, char *argv[]){
        cout <<"Start bousole" << endl;
        Bousole b;
        Bousole::Azimuth az;  //Dont think this the way to go...
        b.display();
    
        cout << "Understand Azimuth ";
        cout <<az::NORD << endl;  //CONFUSED HERE 
    
        cout << "Setting Est...";
        b.setEst();
        b.display();
    
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try Bousole::Azimuth::NORD instead. Though it is still unclear what you are trying to achieve. What are you doing with az?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    @Elysia.
    I will have to double check this when I am at my computer, but I am pretty sure I tried Bousole::Azimuth::NORD or a combination thereof and got a compile error. Or I am lost... which is possible.
    I was not very clear in what I try to do. In other words, I have a class Turtle (not shown here) which can move and change direction. To change direction it uses the class Bousole (Compas). So in my turtle, I want to be able to access the enum EAST, WEST, etc...

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    It turns out I get a compiler error when I do this:
    Code:
    cout << Bousole::Azimuth::OUEST << endl;
    But I can do this without problem
    Code:
     auto z=Bousole::Azimuth::OUEST;
    But I cannot do cout << z without compile error. I guess I can work around this, but I was curious as to why it does not compile

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right, I missed that.
    Bousole::Azimuth::OUEST is of type Bousole::Azimuth, which std::cout have no idea how to output. That's why it gives a compile error.
    You can get around it by implementing a operator << for Bousole::Azimuth:

    Code:
    std::ostream& operator << (std::ostream& lhs, Bousole::Azimuth rhs)
    {
    	// Translate it to something readable in the output
    	// lhs << "Bousole::Azimuth::OUEST"; <--- For example
    	return lhs;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    @Elysia
    It's a very interesting idea, but I get a compile error in my prototype. I think I need a break.
    Bousole.h:23:73: error: ‘std:: ostream& Bousole:: operator<<(std:: ostream&, Bousole::Azimuth)’ must take exactly one argument
    ]

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The form of `operator <<' you want is necessarily a non-member function.

    Use the declaration Elysia gave.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  2. Using values from derived class in base class
    By gratuitous_arp in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2011, 09:25 AM
  3. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  4. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  5. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM