returning a vector

This is a discussion on returning a vector within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out how I can return a vector from a method so that I can print the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    returning a vector

    I'm trying to figure out how I can return a vector from a method
    so that I can print the results in main().
    Example:
    Code:
    class test{
        public:
        vector <string> A(vector <string> a, vector <string> b){
                vector <string> retval;
                 retval.push_back("test");
                 return (retval);
        }
    };
    //
    int main(void){
         test p;
         p.A;
         cout<<A[0];
    }
    When I do this I get a compiler error until I copy
    Code:
    vector <string> retval;
    into the main function. However, this reinitializes the retval vector. How, can I code this so that I don't have to reinitialize the vector in main?
    Thanks in advance.

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Um, you're not passing anything to the method A()...
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    and also when you do call functions even thought it takes no arguments always end in parethesis

    like

    a.blah();
    nextus, the samurai warrior

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    OK, sorry. that code should have been----
    Code:
    class test{
        public:
        vector <string> A(vector <string> a, vector <string> b){
                vector <string> retval;
                 retval.push_back("test");
                 return (retval);
        }
    };
    //
    int main(void){
         test p;
         p.A(a,b);
         cout<<retval[0];
    }

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Umm...where are a and b being declared? and retval for that matter.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    ok, again, of course a, b have to be declared in main() as well, and I suppose retval does too, like so:
    Code:
    class test{
        public:
        vector <string> A(vector <string> a, vector <string> b){
                vector <string> retval;
                 retval.push_back("test");
                 return (retval);
        }
    };
    //
    int main(void){
         vector <string> a;
         vector <string> b;
         vector <string> retval;
         test p;
         p.A(a,b);
         cout<<retval[0];
    }
    but the point is, when I redeclare retval in main, it is reintialized
    and the return value(s) are lost. How do I avoid this?

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Retval goes out of scope when the function terminates. If you want to access it the way you are trying to access it, you need to make it a member of the test class, not declare it within the function. OR if you don't want it a member and only want it to return a vector, then you need to declare a vector in main and have the function return a value to it like so:

    vector<string> retVal;
    retVal=p.A(a,b);
    cout<<myVec[0];

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Like PJYelton said, you need to assign a value to retval. It is just like any other function with a return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21