Problem with VS 2010?

Or is it my code? Can someone compile this and let me know if it works for them?
I'm getting errors saying it can't access the protected members in the String class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* String.cpp
Program: convert between ordinary strings to special protected strings class Pstring2 with substring functions to fetch a part of the string.
*/

#include <iostream>
#include <cstring>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
class String	//user-defined string type
{
	protected:
	enum {SZ = 80};	//size of all String objects
	char str[SZ];	//holds a C-string
	
	public:
	String()	//no-arg constructor
	{
             str[0] = '\0'; //initialize String with a NULL character
    }
	String(char s[])		//1-arg constructor
	{
                strcpy(str, s);	//convert C-string to String
    }

	void display() const	//display the String
	{
         cout<< str;
    }

	operator char*()		//conversion operator
	{
             return str;		//convert String to C-string
    }

}; //End of class String

////////////////////////////////////////////////////////////////////////////////
class Pstring : public String		//user-defined derived protected string type
{	
	public:
	Pstring():String()	//no-arg constructor
	                                        //with call to String class constructor in intializer list
	{}

	Pstring(char s[])	//one-arg constructor
	{	
		int i;
		for(i=0;(i<(SZ-1))&&(s[i]!='\0');i++)      //copy first SZ-1 characters into str 
        {                                                  // or all characters upto NULL character if the string is smaller than SZ. 
			str[i]=s[i];
		}
		str[i]='\0';		                      // Append NULL character to the end of string.
	}
};

////////////////////////////////////////////////////////////////////////////////
class Pstring2 : public Pstring		//user-defined derived protected string type with substring functions 
{	
	public:
	Pstring2():Pstring()	//no-arg constructor
  	                                            //with call to Pstring class constructor in initializer list 
	{}
	Pstring2(char s[]):Pstring(s)	//one-arg constructor
	                                                     //with call to Pstring class one-arg constructor in initializer list 
	{}

	protected:
	void left(Pstring2 text, int n)     //Fetch the n leftmost characters from text
	{
        int i;
		for(i=0;i<n;i++)
			str[i]=text.str[i];	        //copy first n characters into str
		str[i]='\0';               //append NULL character at the end of str
	}
	void mid(Pstring2 text,int s, int n) //Fetch n characters from text starting from s position
	{
         int i;
		for(i=0;i<n;i++)
			str[i]=text.str[s+i];	        //copy n characters into str starting from s position
		str[i]='\0';               //append NULL character at the end of str
	}
	void right(Pstring2 text, int n)  //fetch the n rightmost characters from text
	{
		int j;
		for(j=0;text.str[j]!='\0';j++);       //count total characters in Pstring2 text

		for(int i=j-n;i<j;i++)                //loop from j-n position till j position        
			str[i-j+n]=text.str[i];	        //copy last n characters into str
		str[n]='\0';               //append NULL character at the end of str
	}
};

////////////////////////////////////////////////////////////////////////////////
int main()
{
	cout<<"TEST 1 : "<<"THIS SENTENCE IS SHORTER THAN SZ:"<<endl;
    Pstring2 s1;		//use no-arg constructor
					//create and initialize C-string
	char xstr[] = "Hello world!";
	s1 = xstr;		//use 1-arg constructor to convert C-string to String
	s1.display();	//display String
    cout<<endl << endl;	

    cout<<"TEST 2: "<<"THIS SENTENCE IS LONGER THAN SZ:"<<endl;
	Pstring2 s2 = "Lets see if this larger than SZ characters string would be copied to a string of SZ characters.";		//uses 1-arg constructor to initialize String
	cout << static_cast<char*>(s2);	//use conversaion operator
	cout << endl << endl;					//to convert String to C-string before sending to << op

    cout<<"TEST 3: "<<"THIS SENTENCE DEMONSTRATES THE LEFT, MID AND RIGHT FUNCTIONS:"<<endl;
    Pstring2 s3 = "A long time ago in a galaxy far, far away.";		//uses 1-arg constructor to initialize String
	cout << static_cast<char*>(s3);	//use conversaion operator	
	cout << endl << endl;					//to convert String to C-string before sending to << op					
    
    cout<<"TEST 3 (i): "<<"THE LEFTMOST 10 CHARACTERS ARE:"<<endl;
    s1.left(s3,10);    //fetch first 10 characters from Pstring2 s3
    cout << static_cast<char*>(s1);	//use conversaion operator	
    cout<<endl << endl;						//to convert String to C-string before sending to << op

    cout<<"TEST 3 (ii): "<<"THE SUBSTRING BEGINNING IN POSITION 16 FOR A LENGTH OF 10 IS:"<<endl;
    s1.mid(s3,16,10);    //fetch 10 characters from Pstring2 s3 starting from position 16
    cout << static_cast<char*>(s1);	//use conversaion operator	
    cout<<endl << endl;						//to convert String to C-string before sending to << op

    cout<<"TEST 3 (iii): "<<"THE RIGHTMOST 14 CHARACTERS ARE:"<<endl;
    s1.right(s3,14);    //fetch last 14 characters from Pstring2 s3
    cout << static_cast<char*>(s1);	//use conversaion operator	
    cout<<endl << endl;						//to convert String to C-string before sending to << op

system("pause");
	return 0;
}
	
It's your code.

The error is just what it says. You're trying to access protected members from outside the class, which isn't allowed.

Specifically:

1
2
3
Pstring2 s1;
//...
s1.left(s3,10);    //fetch first 10 characters from Pstring2 s3 


note that Pstrin2::left is protected.

If you want it usable from outside the class, make it public.
Oh, of course. Thanks!
Topic archived. No new replies allowed.