How to return enum vector outside the class?

Let's say that the class has a 2d enumeration vector, and I want to access this 2d vector outside the class and manipulate the value.

My question is: how can I declare a new vector to store the return value outside the class, since my type (enum type) is inside the class? I was hoping for something like

A a(5); std::vector<std::vector<A::s> > x = a.get_2dvec(); 

But that gives me an error, talking about closing it, and then if I create a type of public, I get an unannounced error.

I know that I can place enum s {RED, BLUE, GREEN}; and color typedef; outside the class and achieve the result, but it can be said that the main file is in another file.

  // f1.cpp #include <iostream> #include <vector> class A{ // This enum is inside class enum s {RED, BLUE, GREEN}; typedef s color; const int size = 3; std::vector<std::vector<color> > color_array; public: A(int size_):size(size_),color_array(size){ std::cout << "vector size = " << color_array.size() << std::endl; for(int i = 0; i < size; i++){ for(int j = 0; j < size; j++){ color_array[i].push_back(RED); } } } void print(){ for(auto it = color_array.begin(); it != color_array.end(); it++){ for(auto itt = it->begin(); itt != it->end(); itt++){ std::cout << *itt << " : " << std::endl; } } } // pass vector by value std::vector<std::vector<color> > get_2dvec(){ return color_array; } }; // main.cpp int main(){ A a(4); a.print(); // here I some how need to capture the 2d enum vector std::vector<std::vector<enum-type> > x = get_2dvec(); return 0; } 
+5
source share
2 answers

get_2dvec() is a member function that requires an object to be called. And if you don't want to make A::s public , you can use auto specifier (starting with C ++ 11) to avoid accessing a personal name. (But I'm not sure if this is what you want.)

Edit

 std::vector<std::vector<enum-type> > x = get_2dvec(); 

to

 auto x = a.get_2dvec(); 
+5
source

Your listing is in the private part of the class.

By default, a class runs by default in private , while struct starts by default in public .

Move it to the open part

Also, return the value by reference, in the constant getter method, or the performance and quality of the interface as a whole.

I would also type the matrix myself and immediately use it in the classroom, which made me put the private part in the end.

Edit: since the answers to the questions also mean educational materials from others, I completely reworked the example with links to const, auto, all types of private, all work, just for writing (and it builds).

 #include <vector> #include <iostream> class A { private: // This enum is inside class const int size = 3; enum s {RED, BLUE, GREEN}; typedef s color; typedef std::vector<std::vector<color> > ColorMatrix; ColorMatrix color_array; public: A(int size_):size(size_),color_array(size){ std::cout << "vector size = " << color_array.size() << std::endl; for(auto &it : color_array){ it.resize(size,RED); } } void print() const{ for(const auto &it : color_array){ std::cout << " :"; for(const auto &itt : it){ std::cout << " " << itt; } std::cout << std::endl; } } // pass vector by const reference to avoid copies // (for better performance) const ColorMatrix &get_2dvec() const { return color_array; } }; // main.cpp int main(){ A a(4); a.print(); // here I some how need to capture the 2d enum vector const auto &x = a.get_2dvec(); return 0; } 
+2
source

All Articles