Thread: Classes:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Classes:

    I am having a hard time with classes. What is going on with this code. It has errors all over it.
    Code:
     #include <iostream>
    #include <string>
    
    using namespace std;
    
    class addressBook {
        
    public:
       explicit addressBook(string streetaddy, string phoneNumber)
        : street(streetaddy) 
        : phone (phoneNumber)
        {
        
        }
        
        void Address(string streetAddy)
        {
            street = streetAddy;
        }
        
        void PhoneNum(string phoneNumber)
        {
            phone = phoneNumber;
        }
        string getPhoneNum() const
        {
            return phone;
        }
        string getAddress() const
        {
            return street;
        }
        void displayAddressBook()
        {
        
        }
    private:
        string street;
        string phone;
    };
    
    
    
    int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        cout << "The first address is:"<<addressBook1.getAddress<<"\n"<< addressBook1.getPhoneNum<<endl;
        
        return 0;
    }
    Last edited by jocdrew21; 09-08-2013 at 11:17 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What might the error messages be?
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    line 10,11 - you need to remove string -
    Code:
    : street(streetaddy),phone (phoneNumber)
    and use : only once, after that only ,

    line 49 - there is function call but missing ()
    Code:
    addressBook1.getAddress()
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class addressBook {
        
    public:
       explicit addressBook(string streetaddy, string phoneNumber)
        : street(streetaddy), phone (phoneNumber)
         
        {
        
        }
        
        void Address(string streetAddy)
        {
            street = streetAddy;
        }
        
        void PhoneNum(string phoneNumber)
        {
            phone = phoneNumber;
        }
        string getPhoneNum() const
        {
            return phone;
        }
        string getAddress() const
        {
            return street;
        }
        void displayAddressBook()
        {
        
        }
    private:
        string street;
        string phone;
    };
    
    
    
    int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        cout << "The first address is:"<<addressBook1.getAddress()<<"\n"<< addressBook1.getPhoneNum()<<endl;
         cout << "The second address is:"<<addressBook2.getAddress()<<"\n"<< addressBook2.getPhoneNum()<<endl;
        return 0;
    }
    Error- Use of undeclared identifier (lines 49 and 50)

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    guess it would help if displayAddressBook was not blank!!!

    O man....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    address1 and addressBook1 are different names.
    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

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
     int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        cout << "The first address is:"<<address1.getAddress()<<"\n "<< address1.getPhoneNum()<<endl;
         cout << "The second address is:"<<address2.getAddress()<<"\n "<< address2.getPhoneNum()<<endl;
        return 0;
    }
    GOT IT THANK YOU!!!!

    I'll make a few more programs up to make sure I got it. Just started to learn about .h files and classes. I think I got a grip of the concept now, just have to practice.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why do you have a class called "addressBook" that only stores one address? it may be a minor point, but on this forum, we like to emphasize the idea of naming things in ways that properly represent them. often this gets applied to things like single-letter variable names, but I think this case should also be addressed. in the real world, an address book can contain many addresses. if you plan to do this as a career someday, it would be good to keep this sort of thing in mind.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Will do thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  3. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Classes inside Classes
    By LostGeneration in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2003, 12:02 PM