Comp Sci Learning C++: Fixing Class & Vector Error

  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++ Class Vector
AI Thread Summary
The discussion focuses on resolving a compilation error encountered while learning C++ related to class and vector usage. The user has defined a private inner class, class2, within class1 and is trying to instantiate it but faces an issue due to the private constructor of class2. A suggested solution is to declare class2 as a friend of class1, allowing class1 to access class2's private members. The user aims to assign values to class2's members and create a vector of vectors to store instances of class2. The conversation emphasizes understanding class access modifiers and their implications in C++.
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
 
Physics news 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.
 

Similar threads

Replies
1
Views
1K
Replies
5
Views
3K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
7
Views
3K
Replies
5
Views
2K
Replies
1
Views
2K
Back
Top