Thread: Return pointer to a vector of objects as a function return

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    14

    Return pointer to a vector of objects as a function return

    I hope I can explain what I need to do, that is how lost I am.
    Let's say you have a simple class User. For this purpose let's say really simple class.
    What I am trying to do is to return a vector pointer from a function that points to a vector of Users.
    I am trying to return this vector pointer in main function to a vector pointer or what that is necessary.
    I have create the vector on the go. Let's say I only want to create User vector based on gender. So this will be
    determined while the program is running. All the data is saved to a text file. I am reading from the file. Only get those users
    who are female or male.

    For this purpose, let's assume that the class is like the one below
    Code:
    Class User
        private:
            name;
            age;
            gender;
        public:
            getName;    
            setName;
            getAge;
            setAge;
            getGender;
            setGender;


    Ask user for the input (Male or female)
    call function findMatches
    starts reading from the file
    looks at gender info then creates vector of objects, also sets the name and age for each match with the help of pointer
    and returns the pointer to the vector of objects to a receiver in main function.
    Back in main the data type receives this vector pointer by a necessary data type. Not sure what this would be.


    Any suggestions on how to do this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,568
    How about.
    Code:
    void readFile ( std::vector<User> &users ) {
      ...
      users.push_back( aUser );
      ...
    }
    No messy pointers, no messy dynamic allocation, and no messy extra copies in memory, since all that is passed is a reference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    How can I access to the class methods though?
    For instance:
    users->setName(extract name from file);
    users->setAge(extract age from file);

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,585
    Looks to be posted here as well, getting the same basic answers as well.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Nobody has answered how to access the class methods through a pointer to a vector though.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,585
    That's probably because you've not provided any actual code showing what you're trying to do.

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Adaptron View Post
    Nobody has answered how to access the class methods through a pointer to a vector though.
    A vector is just like any other class, code sample.

    Code:
    // VectorStuff.cpp : Defines the entry point for the console application.
    //
    
    
    #include "stdafx.h"
    #include <vector>
    #include <iostream>
    
    
    
    
    class Data
    {
    public:
    	void print ( )
    	{
    		std::cout << "This is it" << std :: endl;
    	}
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::vector <Data*> * data = new std::vector<Data*> ( );
    
    
    	for ( int i = 0; i < 10; i++ )
    		data->push_back(new Data());
    
    
    	for ( int i = 0; i < 10; i++ )
    		data->at ( i )->print ( );
    }

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Ok. Finally got this I think.
    I am doing something like this
    Code:
    // Run the matching algorithm
            vector<Match*> matchingUsers = showMatches(client);
            cout << matchingUsers.at(0)->getMatchInfo();
            cout << matchingUsers.at(1)->getMatchInfo();

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why pointers?
    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.

  10. #10
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    What options do I have? I am trying to create a dynamic vector of objects and then return a pointer this array in another function.
    Actually, this method also didn't work because I am having problems with memory segmentation fault. I am completely lost at this point.

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Actually, I messed it up. I have to use vector of vectors, I think.
    Because, I can get the first vector element but after that I get segmentation fault error.

  12. #12
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Adaptron View Post
    Actually, I messed it up. I have to use vector of vectors, I think.
    Because, I can get the first vector element but after that I get segmentation fault error.
    You could get better answers if you posted some of the code you're using and explained what you're trying to do.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're getting segmentation faults because you're complicating your code. Pointers should be treated as an evil, used only when necessary. Often there are better solutions.
    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.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by Adaptron View Post
    How can I access to the class methods though?
    For instance:
    users->setName(extract name from file);
    users->setAge(extract age from file);
    Read the name and age into a spare string and number type first, and then call those functions. Then finally push into the vector like Salem showed. You are making this unnecessarily difficult.

  15. #15
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Alright, I solved this problem. Works perfect. A little complicated for my level but figured out finally.
    Segmentation fault is solved by using a loop structure for the vector.
    I was giving random numbers to the index which was out of the range.
    Code:
    for(int index = 0 ; index < matchingUsers.size(); index++)
            {
                cout << "\t\tMatch " << index + 1 << endl;
                cout << "---------------------------------\n";
                cout << matchingUsers.at(index)->getMatchInfo() << endl;
                cout << "Press any button to continue..." << endl;
                getch();
            }
    Thanks for everybody's help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about return function of vector
    By coffee_cups in forum C++ Programming
    Replies: 9
    Last Post: 06-06-2013, 05:57 AM
  2. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. return vector<string> function
    By quizkiwi in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2005, 08:47 AM
  5. Can objects contain/return values like a function?
    By Panopticon in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 07:44 PM

Tags for this Thread