Thread: Class Inheritance

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12

    Post Class Inheritance

    Attention: I figured it out. I forgot to change A to be c instead when I was messing around with my classes.

    I'm trying to figure out how to link classes by inheritance but I keep getting an error:

    Code:
    Line 41 | error: expected unqualified-id before '-' token
    Here is my sample code:

    Edit: Forgot to include the methods for set, so I included those as well.

    Code:
    #include <iostream>
    
    #define CONTACTS 50
    
    using namespace std;
    
    class A {
        private:
            string name[CONTACTS];
        public:
            void set_name(string, int);
            string get_name(int x){return name[x];}
    };
    
    void A::set_name(string s, int x){name[x] = s;}
    
    class B: public A {
        private:
            string number[CONTACTS];
        public:
            void set_number(string, int);
            string get_number(int x){return number[x];}
    };
    
    void B::set_number(string s, int x){number[x] = s;}
    
    class C: public B {
        private:
            string email[CONTACTS];
        public:
            void set_email(string, int);
            string get_email(int x){return email[x];}
    };
    
    void C::set_email(string s, int x){email[x] = s;}
    
    int main(){
        C c;
        int index = 0;
    
        c.set_name("First Last", index);
        c.set_number("1-234-567-8901", index);
        c.set_email("[email protected]", index);
        cout << c.get_name(index) << endl;
        cout << c.get_number(index) << endl;
        cout << c.get_email(index) << endl;
    
        return 0;
    }
    Last edited by Khaltazar; 11-28-2012 at 06:28 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I don't see a '-' token in your code. There are '-' characters, but they are part of a string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12
    Quote Originally Posted by laserlight View Post
    I don't see a '-' token in your code. There are '-' characters, but they are part of a string literal.
    That's why I was confused. I figured it out by changing

    Code:
    A.set_name("First Last"), index);
    to

    Code:
    c.set_name("First Last"), index);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Well, I don't see A.set_name in your original code either. The error should have been "expected unqualified-id before '.' token" in that case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your brackets don't line up in those statements.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Inheritance
    By dhuan in forum C# Programming
    Replies: 8
    Last Post: 10-27-2010, 04:56 PM
  2. Class Inheritance
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2008, 04:37 AM
  3. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  4. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 04:31 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM