Error in C++ program

Discussion in 'OT Technology' started by Kieffer87, Jan 30, 2007.

  1. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I got everything you guys suggested taken care of, now I get these errors. its never ending I tell ya! I wish I knew exactly what the hell was going on, I had my dads friend help me out with what I have so far. So once again guys I greatly appreciate the help, I :love: you guys!
    Code:
    xxx.cpp: In function 'int main()':
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:90: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:96: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:98: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:103: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:116: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:122: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:124: error: within this context
    xxx.cpp:25: error: 'char* String::ptr' is private
    xxx.cpp:129: error: within this context
    
    Here is my program:
    Code:
    #include<iostream>
    using namespace std;
    
    class String{
        int size;
    public:
        String & operator= (String& );
        String & operator+ (String& );
        String & operator== (String& );
    
        String(){
            ptr=NULL;
            size=0;
            }
        void set_size(int t){
            size=t;
            }
        int get_size(){
            return size;    
            }
        ~String(){
            ptr=NULL;
            }
    private:
        char * ptr;
        };
    
    //Operator overloading for assignment operator
    String & String ::operator= (String& se){
        int n;
        n=size;
        char *ne;
        ptr=new char[size];
        ne=ptr;
        int x=0;
        while(n!=0){
             ptr[x]=se.ptr[x];
            n--;
            x++;
            }
        return se;
        }
    //operator concatenentes two strings
    String & String ::operator+ (String& st){
        int n=0;
        int b=st.get_size();
        while(n<size){
            st.ptr[b]= ptr[n];
            b++;
            n++;
            }
        cout<<"The concatenated string is ";
        cout<<st.ptr;
         return st;
        }
    //comparision function overloaded
    String & String :: operator== (String& se){
        int n=0;
        bool flag=true;
        while((flag==true)&&(n<size)){
            if(se.ptr[n]==ptr[n]){
                n++;        
                }
            else{
                if(se.ptr[n]<ptr[n]){
                    cout<<"String 1 is greater than string 2"<<endl;
                    }
                else{
                    cout<<"String 2 is greater than string 1"<<endl;
                    }
                
                    flag=false;
                }
            }
        if(flag==true){
            cout<<"Both strings are equal"<<endl;
            }
        return se;
        }
    int main(){
        char *ch;        //to hold value of character
        int t=0;
        ch = new char;
        String st;        // object of string
        String temp;    //temporary object
        cout<<"Enter a string1 : ";
        while(*ch!='\n'){
            if(t!=0){
                temp.ptr=new char [t];  //allocating new mwmory
                temp=st;
                }
            cin.get(*ch);    //get value from string entered
            if(*ch!='\n'){
                 t++;
                st.ptr=new char[t];
                if(t==1){
                    st.ptr=ch; // assignment of 1 string to the other
                    }
                else{
                    t--;
                    st=temp;
                    st.ptr[t]=*ch;
                    t++;
                    }
                temp.set_size(t);
                st.set_size(t);
                }
            }
        st.set_size(t);
        t=0;
        cout<<"Enter another string2: ";
        String s;
        do{
            if(t!=0){
                temp.ptr=new char [t];  //allocating new mwmory
                temp=s;
                }
            cin.get(*ch);
            if(*ch!='\n'){
                t++;
                s.ptr=new char[t];
                if(t==1){
                    s.ptr=ch; // assignment of 1 string to the other
                    }
                else{
                    t--;
                    s=temp;
                    s.ptr[t]=*ch;
                    t++;
                    }
                temp.set_size(t);
                s.set_size(t);
                }
            }
        while(*ch!='\n');
        st==s;        //to call comparision function
        st=s+st;    //to call concatenation funtion
        cout<<endl;
        }
    I greatly appreciate any help.
     
    Last edited: Jan 30, 2007
  2. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Ouch, that's rough to read. Your ptr shouldn't be public, you should have methods to take care of that for you. You also shouldn't be modifying the objects that you've passed as references (in your overloaded operators).
     
  3. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Is the goal of your operator+ to overwrite one of the original strings? I would imagine you would want to return a new String object instead of clobbering one of the original strings.
     
  4. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    use:


    String& operator= (const String& );
    String& operator+ (const String& );
    String& operator== (const String& );


    Like said above you don't want to be modifying right hand side objects. When you say int temp = temp1 + temp2; you don't want anything to happen to temp1 and temp2, just that their values are used to compute a new value for temp. char * prt needs a char * getPtr(); and void setPtr(char* ) functions and be made private instead of the way you have it now.
     
  5. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    Anybody got any ideas?
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    All your errors are caused by your attempts to directly manipulate a private member. You can't assign things to Sting::ptr outside of String's scope since ptr is private. If you want to allow that type of action, it should take place via a member function.
     
  7. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I just figured that out as well. Thanks for you help though.
     

Share This Page