Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7758403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:21:04+00:00 2026-06-01T13:21:04+00:00

I am actually testing a file and I have a situation, where I need

  • 0

I am actually testing a file and I have a situation, where I need to access some of the protected members of the class from main.cpp. I tried to add, main() as friend, didn’t work out and learned that it wont work, so I moved everything in the main() to a test () and made the test() as friend. still it shows the error.

Example would be

 //--File.hpp

 namespace Files {

 class File {
          public:
                File(long word_):word(word_) {}
          protected:
                long word;
          private:
                friend int test();
 };
 }//ns:Files


 //--List_File.hpp

 namespace Files {
 class List_File :public File {
         public:
               List_File() : File(sizeof(int) + sizeof(long)) {}
         private:
              friend int test();
 };  
 }//ns:Files 



//--main.cpp

  using namespace Files;

  int test() {

       File *pd = new List_File();
       assert(pd->word == 12); //LINE 34
       return 0;
  }

  int main() {
       test();
       return 0;
  }

//it says error on Line 34: Base::value is protected. Please advice.

    g++ -O -Wall -Wno-unused -o a.out File.cpp List_File.cpp Data_File.cpp               
    Free_List_File.cpp main.cpp
    File.hpp: In function ‘int test()’:
    File.hpp:30:7: error: ‘long int Files::File::word’ is protected
    main.cpp:34:16: error: within this context
    make: *** [a.out] Error 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:21:05+00:00Added an answer on June 1, 2026 at 1:21 pm

    No, it definitely doesn’t have to be in the same file, but it obviously has to “know” what the class is, i.e.: the header that has the class definition should be included in the file where the function is implemented. Your code should be fine, as commented.

    after you added some context

    Your test function is not in the Files namespace. If you want it to be in the global context, you should treat it as “::test” within the namespace, otherwise the compiler might expect the “Files::test” to be the friend function, and not the “::test” as in your case. Can’t find the formal standard reference, but I’m pretty sure that’s the case. Note that you’re performing a forward declaration here, so there’s no default fall-back to the upper level of scope for name resolution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am integrating with a webservice who have provided a wsdl file to access
I'm new to jsp and have ran into some trouble. Initially, the jsp file
We have some methods that call File.Copy, File.Delete, File.Exists, etc. How can we test
I have a Player class that derives from Humanoid and Humanoid derives from Sprite
I'm having a rare CSS issue in Internet Explorer 7 (I'm actually testing it
Actually I have two questions. (1) Is there any reduction in processing power or
Actually, I have the task of finding the employees based on the salary rank.
I have some fairly generic code which uses preprocessor macros to add a certain
Doing some R&D for my company. We are trying to have a listView that
I have a Silverlight testing project using the Silverlight Unit Test Framework . I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.