why I cannot access private data of a friend

This is a discussion on why I cannot access private data of a friend within the C++ Programming forums, part of the General Programming Boards category; the only thing wrong about the following code is friendship, once I chance the data from private to public, it ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    why I cannot access private data of a friend

    the only thing wrong about the following code is friendship, once I chance the data from private to public, it compiles. so why the friend declaration did let me go through?
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;
    
    class READER{
    class DATA {
    	vector<string> rowtypes;
    	vector<string> rownames;
           class rows;
           friend class rows;
    };
    
    class state {
    public:
      virtual bool parse(const char* a, DATA& r) = 0;
    };
    
    class rows: public state {
    public:
      bool parse(const char* a, DATA& r) {
    		istringstream is(a);
    		string s1, s2;
    
    		is >> s1;
    		is >> s2;
    		if (is.fail())
    			return false;
    		r.rowtypes.push_back(s1);
    		r.rownames.push_back(s2);
    		return true;
    	}
    };
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    The READER::DATA class declared a DATA::rows class, and then declared it as a friend. What you want to do is to declare the READER::rows class as a friend. One way is to simply remove that declaration of the rows from from the DATA class definition, possibly moving it to before the DATA class definition.

    By the way, fully capitalised names are conventionally reserved for macro names. You might want to use the more common conventions of lower case names, or capitalised names (my personal preference), or a C prefix, for class names.

    Also, it is good that you used code tags, but please indent your code consistently.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    thanks

    thanks, it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. runtime error with vector
    By te5la in forum C++ Programming
    Replies: 26
    Last Post: 07-17-2008, 08:57 AM
  2. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  3. vector as private data
    By robquigley in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2003, 02:30 PM
  4. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM
  5. Stack or Heap Private Data...?
    By GrNxxDaY in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2002, 05:13 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21