Learning C++: Fixing Class & Vector Error

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++ Class Vector
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
anonim
Messages
39
Reaction score
2
Homework Statement
-
Relevant Equations
-
I am now learning C ++ and trying to learn class and vector. I'm trying to write code, but I got an error.
this is my class and enum class:
Code:
enum class state: char{ empty='.', filled_with_x='x', filled_with_o='o'};
Code:
class class1{
    private:
        class class2{
            class2();
            int x;
            int y;
            char z;        
        };

    private:
        int size;
        vector< vector<class2>>vec;
    public:
        class1();
        class1(int size);
        void fill_vector();    

};
I'm stuck in assigning values to members in the private class and then I wrote like this but I am not sure this is right.
Code:
class1::class2::class2() : x(-1), y(-1),z(static_cast<char>(state::empty)){};

And :
Code:
void class1::fill_vector(){
    class2 temp1;
    int i,j;
    vector<class2>temp;
    for(i=0; size>i;i++){
        for(j=0; size>j; j++){
            temp.push_back(temp1);
        }
        vec.push_back(temp);
    }

}

when I tried compile, I get an error: temp1 is private within this context
 
on Phys.org
Hi.

I wonder what you are trying to do here. Anyway, if you insist on having a private constructor for class2, you can solve by declaring class2 like this:
C++:
class class2{
    friend class class1;
    class2();
    int x;
    int y;
    char z;       
};

Try and let me know how that goes.
 
RaamGeneral said:
Hi.

I wonder what you are trying to do here. Anyway, if you insist on having a private constructor for class2, you can solve by declaring class2 like this:
C++:
class class2{
    friend class class1;
    class2();
    int x;
    int y;
    char z;     
};

Try and let me know how that goes.
I want to create class1. This class will have a private inner class named class2. I want to assign value to member in class2 at first. And I want to create vector<vector<class2>>vec.