Learning C++: Fixing Class & Vector Error

In summary, the conversation is about learning C++ and trying to write code, but encountering an error. The user is stuck in assigning values to members in a private class and shares their code for class1 and class2. They also mention trying to compile and getting an error related to the private context of temp1. The responder suggests declaring class2 as a friend class of class1 to solve the issue. The user clarifies that they want to create class1 with a private inner class class2 and a vector of class2 objects.
  • #1
anonim
40
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
  • #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
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.
 

1. What is C++ and why is it important to learn?

C++ is a high-level programming language that is widely used in the development of software and applications. It is important to learn because it is a powerful language that allows for efficient and effective coding, making it a valuable skill for any scientist or programmer.

2. What is a class in C++ and why is it important to fix errors related to it?

A class in C++ is a user-defined data type that encapsulates data and functions into a single entity. It is important to fix errors related to classes because they are the building blocks of object-oriented programming and any errors can result in incorrect program behavior or even crashes.

3. What is a vector in C++ and why is it important to fix errors related to it?

A vector in C++ is a dynamic array that can resize itself as needed. It is important to fix errors related to vectors because they are commonly used in data manipulation and any errors can lead to incorrect data processing or memory issues.

4. How can I fix errors related to classes and vectors in C++?

To fix errors related to classes and vectors in C++, you can use debugging tools such as a debugger or print statements to identify the source of the error. You can also refer to online resources or consult with more experienced programmers for assistance.

5. Are there any resources available for learning C++ and fixing errors related to classes and vectors?

Yes, there are many resources available for learning C++ and fixing errors related to classes and vectors. These include online tutorials, forums, and books specifically focused on C++ programming. Additionally, many programming communities offer support and assistance for troubleshooting errors in C++.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
845
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
886
  • Engineering and Comp Sci Homework Help
Replies
2
Views
928
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
897
Back
Top