Thread: Can't access protected data member from base class in derived class

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    45

    Can't access protected data member from base class in derived class

    Hi everyone, I have a base class "String" and derived the class "ReverseString" from "String". When I try to use member variables from String in the ReverseString class, it is telling me "ReverseString has no member ' ' "

    Here is my ReverseString class:
    Code:
    class ReverseString : public String{
    public:
        ReverseString() : String();
        ReverseString(const ReverseString&) : String(const String&);
        ReverseString(char*) : String(char*);
    
        friend ReverseString operator+(const ReverseString&, const ReverseString&);
        ReverseString operator~();
    };
    In the .cpp file for my ReverseString class, it is telling me "ReverseString has no member 'length'"

    Code:
    ...
    ...
    ReverseString operator+(const ReverseString& strA, const ReverseString& strB){
        int len = strA.length + strB.length;
        int count = 0;
        char* temp;
        temp = new char[len];
        for(int i = 0; i < strA.length; i++){
            temp[i] = strA.buf[i];
            count++;
        }
        for(int i = 0; i < strB.length; i++){
            temp[i+count] = strB.buf[i];
        }
        return temp;
    }
    ...
    ...
    Edit: Here is my String class in case you need to see it:
    Code:
    class String{
    protected:
        int length;
        char* buf;
    public:
        String();
        String(char*);
        String(char);
        String(int);
        String(const String&);
        String(char, int);
        ~String();
    
        String& operator=(const String&);
        String& operator=(const char*);
        friend String operator+(const String&, const String&);
        friend String operator+(const String&, const char*);
        friend String operator+(const char*, const String&);
        friend String operator+(const String&, char);
        friend String operator+(char, String&);
        String& operator+=(const String&);
        String operator+() const;
        friend int operator==(const String&, const String&);
        friend int operator!=(const String&, const String&);
        friend int operator<(const String&, const String&);
        friend int operator<=(const String&, const String&);
        friend int operator>(const String&, const String&);
        friend int operator>=(const String&, const String&);
        char& operator[](int);
        friend char* operator+(const String&, int);
        friend char* operator+(int, const String&);
        String& operator++();
        String& operator--();
        String operator++(int);
        String operator--(int);
        friend ostream& operator<<(ostream&, const String&);
    
        int getLength(const char*);
        String substr(int, int);
        void print();
    };
    length is a protected member variable in my String class, so shouldn't it be automatically included in the ReverseString class?
    (By the way, this isn't the string class from the C++ library)

    Thanks in advance!
    -Ryan
    Last edited by ryanmcclure4; 11-16-2012 at 03:45 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That problem is not evident from what you have shown, but please do not paraphrase compiler error messages. Always copy and paste the exact error you are getting - ALWAYS!

    However, it may be a case of fix the errors starting from the first one reported as each error may cause spurious errors further down.

    One very obvious problem I see is this:
    Code:
        ReverseString(const ReverseString&) : String(const String&);
        ReverseString(char*) : String(char*);
    You wouldn't have made such a silly mistake if you stopped that bad practice of removing parameter names.

    It looks like you would also have memory leak issues.
    Last edited by iMalc; 11-16-2012 at 04:24 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    I'm suffering from memory leaks just looking at this!

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Sorry about that! I always forget to do that when I declare the constructors and functions.

    This is my new string class:
    Code:
    class String{
    protected:
        int length;
        char* buf;
    public:
        String();
        String(char* str);
        String(char a);
        String(int i);
        String(const String& temp);
        String(char a, int len);
        ~String();
    
        String& operator=(const String& str);
        String& operator=(const char* a);
        friend String operator+(const String& strA, const String& strB);
        friend String operator+(const String& str, const char* a);
        friend String operator+(const char* a, const String& str);
        friend String operator+(const String& str, char a);
        friend String operator+(char, String& str);
        String& operator+=(const String& str);
        String operator+() const;
        friend int operator==(const String& strA, const String& strB);
        friend int operator!=(const String& strA, const String& strB);
        friend int operator<(const String& strA, const String& strB);
        friend int operator<=(const String& strA, const String& strB);
        friend int operator>(const String& strA, const String& strB);
        friend int operator>=(const String& strA, const String& strB);
        char& operator[](int index);
        friend char* operator+(const String& str, int index);
        friend char* operator+(int index, const String& str);
        String& operator++();
        String& operator--();
        String operator++(int);
        String operator--(int);
        friend ostream& operator<<(ostream& os, const String& str);
    
        int getLength(const char* a);
        String substr(int a, int b);
        void print();
    };
    My new Reversestring class:
    Code:
    class ReverseString : public String {
    public:
        ReverseString();
        ReverseString(const ReverseString& strA) : String(strA) {}
        ReverseString(char* a) : String(a) {}
    
        friend ReverseString& operator+(const ReverseString& strA, const ReverseString& strB);
        ReverseString operator~();
    };
    And in the .cpp file:
    Code:
    ReverseString operator+(const ReverseString& strA, const ReverseString& strB){
        int len = strA  .length   + strB. length ; 
        int count = 0;
        ReverseString temp( len );
        for(int i = 0; i < strA. length ; i++){
            temp. buf [i] = strA.buf[i]; 
            count++;
        }
        for(int i = 0; i < strB.length; i++){
            temp.buf[i+count] = strB.buf[i];
        }
        return temp;
    }
    It's giving me errors on the bold parts ^^
    I'm just learning about inheritance...is there something I'm missing (other than the parameter name mistake)?

    Thanks

    EDIT: I just realized the errors start at the top where I have "#include "string.h"" It says "Error: PCH warning: header stop not at file scope. An intellesense PCH file was not generated."
    Last edited by ryanmcclure4; 11-17-2012 at 04:12 PM.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    I just realized I was overloading the wrong operator (I was supposed to be overloading the assignment operator for this part of my assignment) and changed it - now it works fine...Sorry for the confusion!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Ah very good.
    Your PCH warning will go away if you put #include "StdAfx.h" as the very first thing in the cpp file, or turn off precompiled headers for that file or the whole project.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Alright thanks, good to know!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2011, 10:03 PM
  2. Access to members of protected base class
    By DL1 in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2009, 10:30 AM
  3. Access protected base class template variable
    By pjotr in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2007, 05:34 AM
  4. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  5. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM