0

Header.h

Class A{

private:
struct Student
    {
        std::string name;
        uint32_t rollno;
    };

}

Codefile.cpp

#include "Header.h"
namespace
{
     const A::Student students[] = {
     {"Luffy", 1},
     {"Zoro", 2}, 
     };
}

const A::Student throws an error saying that Student is inaccessible in codefile.cpp. How do I make a struct Student variable declared as a private variable in header file accessible in an unnamed namespace in a cpp file?

Luffy555
  • 3
  • 1
  • Why do you make it private, if you want to put it in a global area? – Devolus Mar 01 '21 at 15:15
  • It is a requirement to keep it private. and what do you mean by put it in a global area? – Luffy555 Mar 01 '21 at 15:16
  • 1
    You declared it globally in your module. Since it is private, this will not work. What exactly do you try to solve? Maybe this is meant to be static? – Devolus Mar 01 '21 at 15:19
  • 1
    Maybe you miswrote but Student is not a declaration of a variable, it's a new type definition. Anyway this does seem like an [xy problem](https://meta.stackexchange.com/q/66377/684852) it would help if you explain what you're trying to achieve. – anastaciu Mar 01 '21 at 15:21
  • I need to have a struct declared in header file which should be private to its class and be accessible in the cpp file inside a namespace. – Luffy555 Mar 01 '21 at 15:21
  • @ManojKeerthy that requirement doesn't make much sense. You're basically say you want it to be private but not private. – Jabberwocky Mar 01 '21 at 15:22
  • 1
    Please quote the exact wording of your assignment. – zkoza Mar 01 '21 at 15:22
  • If `Foo` is private to the class `Bar`, then `Foo` can only be accessed by other members of `Bar`. That's what the `private` keyword does. – Jabberwocky Mar 01 '21 at 15:25
  • @Jabberwocky so you say that a namespace cannot access a private variable? – Luffy555 Mar 01 '21 at 15:28
  • @Luffy555 no, if it's private it's private – Jabberwocky Mar 01 '21 at 15:34
  • How do I declare a namespace in header file that I can use it in my cpp file to access this private struct? – Luffy555 Mar 03 '21 at 09:15

1 Answers1

1

Private members of structs and classes are called "private" because they can only be accessed from within the class/struct.

If you want to keep the struct private try to initialize its values during the construction of the class. Like this:

#include <vector>
#include <string>
class A{
public:
//constructor for two students
    A(const std::string &student1, const int rollNo1,  const std::string &student2, const int rollNo2){
        m_student.push_back(Student(student1, rollNo1));
        m_student.push_back(Student(student2, rollNo2));
        
    }
private:
    struct Student
        {
            std::string name;
            uint32_t rollno;
            //explicit constructor for the struct
            Student(const std::string &Name, const int rollNo){
                name = Name;
                rollno = rollNo;
            }
        };
    std::vector<Student>m_student; //we need some container to store the students in the class
};
//usage
int main()
{
    A myClassInstance("Luffy", 1, "Nami", 2);
return 0;
}

Obviously there are much better ways to do this, add an "addStudent" public member function to the class that can add a member. Also make sure you are using an unnamed namespace for the correct reasons :-) Check this thread out: Why are unnamed namespaces used and what are their benefits?

Dharman
  • 30,962
  • 25
  • 85
  • 135
MYZ
  • 331
  • 2
  • 10