We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,183 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ vector object toString

I have 2 classes.
AddressBook and SingleAddress.
AddressBook is supposed to contain SingleAddress.
I'm omitting the #include and some of the extra functions.

Here's SingleAddress.h

class SingleAddress
{
private:
    string lastName,firstName,strAdd,city,country,email;
    int postCode,homeNum,mobileNum;
public:
    SingleAddress(
    string s1,string s2,string s3,
    string s4,string s5,string s6,
    int i1,int i2,int i3);
    string toString();
};

SingleAddress.cpp

SingleAddress::SingleAddress(string s1,string s2,string s3,string s4,string s5,string s6, int i1,int i2,int i3){
    firstName=s1;
    lastName=s2;
    strAdd=s3;
    city=s4;
    country=s5;
    email=s6;
    postCode=i1;
    homeNum=i2;
    mobileNum=i3;
}

string SingleAddress::toString()
{
    stringstream info;
    //last name, first name, street address, city, country, postal code, home phone number, mobile phone number, email address
    info<<"First name: "  <<firstName
        <<"\nLast name: " <<lastName
        <<"\nStreet address: "<<strAdd
        <<"\nCity: "      <<city
        <<"\nCountry: "       <<country
        <<"\nEmail address: "<<email
        <<"\nPostal code: "<<postCode
        <<"\nHome number: "<<homeNum
        <<"\nMobile number: "<<mobileNum;
    return info.str(); // To convert the above stringstream to string to be displayed out.
}

Here's my AddressBook.h

class AddressBook{
private:
    vector<SingleAddress*>addressBook;
public:
    AddressBook();
    void addAddress(SingleAddress* newAddress);
    void returnListOfAddresses();
};

AddressBook.cpp

AddressBook::AddressBook(){
}

void AddressBook::addAddress(SingleAddress* newAddress){
    addressBook.push_back(newAddress);

And this is where I'm awfully stuck.

void AddressBook::returnListOfAddresses(){

    for(int t=0; t<(int)addressBook.size(); t++){
        cout<<SingleAddress::toString(); //???????
    }
}

int main()

int main(){
        AddressBook*aB=new AddressBook();

        //Create address
        SingleAddress * sA = new SingleAddress(
        "bla","bla","bla","bla","bla","bla",
        1,2,3);

        //Display address
        cout<<sA->toString()<<endl;

        //Add address to AddressBook
        aB->addAddress(sA);

        //Display
        aB->returnListOfAddresses();

return 0;
}

So, the problem is that I don't know how to display all the entries in aB (the AddressBook).
And the question states that my returnListOfAddresses() must not take any input.
I tried adding #include "SingleAddress" and tried to use toString() from the SingleAddress class but it's not working.

I'm at a total loss right now.
Help plz~!!

3
Contributors
3
Replies
1 Day
Discussion Span
4 Months Ago
Last Updated
6
Views
nullifyQQ
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

returnListOfAddresses() should look like this:

for(int t=0; t<(int)addressBook.size(); t++)
{        
    cout<<addressBook[t]->toString();
}
NathanOliver
Posting Virtuoso
1,516 posts since Apr 2009
Reputation Points: 281
Solved Threads: 277
Skill Endorsements: 3

thanks.
mods plz delete this.

nullifyQQ
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

mods plz delete this.

No can do.

TOS:

Members may edit their posts for a limited time period immediately after, for the purpose of correcting spelling and grammar mistakes and accidental ommissions. After this initial period expires, posts may only be edited or deleted by DaniWeb team members, and only in cases where they do not comply with our forum rules for the purpose of making said content comply with all rules.

You could delete your profile here

Nick Evan
Cold-a$$ donkey
Moderator
10,261 posts since Oct 2006
Reputation Points: 4,155
Solved Threads: 416
Skill Endorsements: 22

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1022 seconds using 2.71MB