need help with error

I was executing this programe and received error at line 34 saying "name lookup of changed for ISO 'for scoping [-fpermissive]". I am beginner. the code is to prevent buffer overflow when string constant with too long charcters is used. can anyone help me?

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
  //inheritance from String class
#include <iostream>
#include <cstring> //for strcpy(), etc.
using namespace std;
////////////////////////////////////////////////////////////////
class String //base class
{
protected: //Note: can’t be private
enum { SZ = 80 }; //size of all String objects
char str[SZ]; //holds a C-string
public:
String() //constructor 0, no args
{ str[0] = '\0'; }
String( char s[] ) //constructor 1, one arg
{ strcpy(str, s); } // convert string to String
void display() const //display the String
{ cout << str; }
operator char*() //conversion function
{ return str; } //convert String to C-string
};
////////////////////////////////////////////////////////////////
class Pstring : public String //derived class
{
public:
Pstring( char s[] ); //constructor
};
//--------------------------------------------------------------
Pstring::Pstring( char s[] ) //constructor for Pstring
{
if(strlen(s) > SZ-1) //if too long,
{
    for(int j=0; j<SZ-1; j++) //copy the first SZ-1
str[j] = s[j]; //characters “by hand”
str[j] = '\0'; //add the null character
}
else //not too long,
String(s); //so construct normally
}
int main()
{ //define String
Pstring s1 = "This is a very long string which is probably "
"no, certainly--going to exceed the limit set by SZ.";
cout << "\ns1="; s1.display(); //display String
Pstring s2 = "This is a short string."; //define String
cout << "\ns2="; s2.display(); //display String
cout << endl;
return 0;
}
Why don't you do it the easy way :) ...

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
#include<iostream>
#include<cstring>
using namespace std;

#define n 80

class String{

    protected:
        char *str;

    public:
        String(){
            //Instantiating String
            str = new char[n];
        }
        String(char *s){
            //Allocate Memory w.r.t the size of parameter string i.e. char *s
            str = new char[strlen(s)+1];
            str = s;
        }
        void Display(){
            cout << str << endl;
        }

};

class Pstring : public String{

    public:
        Pstring(char *s){

            str = new char[n+1];
            str[n] = '\0';

            //Checking for Length
            if(strlen(s) <= n)
                str = s;
            else
                for(int i=0;i<n;i++)
                    str[i] = s[i];
        };

};

int main()
{
    //Instantiating
    char *title = (char *)"This is a small string!";

    //Testing Base Class
    String bobj = title;
    Pstring pobj = title;
    Pstring pobj2 = (char *)"This is a very long string and this string should be trim by the checks I implemented in the constructor!";

    //Displaying String
    bobj.Display();
    pobj.Display();
    pobj2.Display();

    cout << endl;
    cin.ignore();
    return 0;
}
@OP Line 34 j is undefined. Though you could use str[SZ] = 0; Also, your indentation is so obfuscated it makes my eyes bleed.
Topic archived. No new replies allowed.