Learning C++: Fixing Class & Vector Error

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++ Class Vector
Click For Summary
SUMMARY

The discussion focuses on resolving a compilation error encountered while learning C++ related to class and vector usage. The user is attempting to create a private inner class, class2, within class1, and is facing issues with accessing its members due to the private constructor. A solution is provided by suggesting the use of the 'friend' keyword to allow class1 access to class2's private members. This adjustment enables the user to instantiate class2 objects within class1's methods without encountering access violations.

PREREQUISITES
  • Understanding of C++ class and object-oriented programming concepts
  • Familiarity with C++ vectors and their usage
  • Knowledge of access specifiers in C++ (public, private, friend)
  • Basic understanding of C++ enumerations and enum classes
NEXT STEPS
  • Explore C++ access specifiers and their implications on class design
  • Learn about the 'friend' keyword in C++ and its usage in class relationships
  • Investigate vector initialization and manipulation in C++
  • Study C++ enumerations and enum classes for better state management
USEFUL FOR

C++ learners, software developers working with object-oriented programming, and anyone interested in understanding class design and vector usage 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 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K