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

Access vector object class in another class..

Suppose I have a code:
Class A have items which is pushed into a vector...I want to access Class A element from class B through A's vector..

class A
{
    friend class B;
    vector<A> data; //store info in vector
};

Class B
{
    void showDataInClassA();
    A a;

}
void B::showDataInClassA()
{
    vector<A>::iterator p= a.data.begin();
    while(p != a.data.end())
    {
        //cout<<p->getUserName();
        ++p;
      }

}

However, it doesnt show up. May I get any advice?
Thanks.

2
Contributors
1
Reply
2 Hours
Discussion Span
3 Months Ago
Last Updated
7
Views
GottaLove
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

the vector is not populated with anything.

#include <vector>
#include <string>
#include <iostream>

using namespace std;

class B;

class A
{
public:
    friend B;
    A(string n) {username = n;}
    A() {}
    static vector<A*> data; //store info in vector
    string getUserName() {return username;}
protected:
    string username;

};

class B
{
public:
    B() {}
    void showDataInClassA();
    A a;
};

void B::showDataInClassA()
{
    vector<A*>::iterator p= a.data.begin();
    while(p != a.data.end())
    {
        cout<< (*p)->getUserName();
        ++p;
      }
}
vector<A*> A::data; //store info in vector

int main()
{
    A::data.push_back(new A("One"));
    A::data.push_back(new A("Two"));
    A::data.push_back(new A("Three"));
    B b;
    b.showDataInClassA();
    return 0;
}
Ancient Dragon
Achieved Level 70
Team Colleague
32,137 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

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

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0585 seconds using 2.7MB