Class and vector

  • #1
38
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
 

Answers and Replies

  • #2
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.
 
  • #3
38
2
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.
 

Related Threads on Class and vector

Replies
15
Views
937
R
Replies
1
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
2
Replies
28
Views
7K
Replies
7
Views
1K
  • Last Post
Replies
8
Views
575
  • Last Post
Replies
2
Views
652
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
3
Views
1K
Top