Thread: returning a vector in a function, how to?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    returning a vector in a function, how to?

    a few issues(questions) here. returning a vector in a function, what I've seen on the net in how to write it after I wrote it and it gets this error.

    compiler error
    Code:
    In file included from options.cpp:4:0:
    files.h:21:6: error: 'vector' in namespace 'std' does not name a template type
     std::vector <std::string> * fill_vector(char *file);
    the function. which I do not even think is written right, because it is
    going to be called multiple times before it is done filling the vector.
    that maybe ok , I don't I have not gotten that far yet.
    Code:
    std::vector <std::string> * fill_vector(char *file)
    {
        std::vector <std::string> FileList;
        std::string sfile(file);
        FileList.push_back(sfile);
    
        return FileList; 
    }
    But it seems logical to me to fill it, then one needs to get it so I can get the information contain within it and use it elsewhere.

    if I make it void I get no errors, I run the program (what I got written so far) and it 'fills the vector" but I'm guessing because I need to get a hold of it to run back through it again to see if this code is working.

    I do not want to put a loop inside of the function then it will just get called everything something is added.


    should that string get filled with a new name = file being passed into the function each time it is called? I am thinking yes, but doesn't hurt to ask.
    what is the proper way to getter done?


    MOD:
    o Kay, still figuring out headers for everything C++, that might be my problem.
    Last edited by userxbw; 10-05-2017 at 11:53 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,056
    > std::vector <std::string> * fill_vector(char *file)
    You need to remove the *
    You're not returning a pointer, you're returning an actual vector.

    Assuming your header file includes <vector> and <string>.

    Then make the parameter
    const char *file
    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
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    > std::vector <std::string> * fill_vector(char *file)
    You need to remove the *
    You're not returning a pointer, you're returning an actual vector.

    Assuming your header file includes <vector> and <string>.

    Then make the parameter
    const char *file
    Ok

    here is where I am at.
    Code:
     
    
    void  fill_vector(char *file)
    {
        std::vector <std::string> FileList;
        std::string sfile(file);
        FileList.push_back(sfile);
     
        int s = FileList.size();
        for( int i = 0; i < s ; i++) {
            std::cout << FileList[i]<< " vector"<< '\n';
        }
     
    
      //  return FileList; 
    }
    that works, I get output of every file in the directories .

    when I change the to return
    Code:
    std::vector <std::string>   fill_vector(char *file)
    {
        std::vector <std::string> FileList;
        std::string sfile(file);
        FileList.push_back(sfile);
     
    /*
        int s = FileList.size();
        for( int i = 0; i < s ; i++) {
            std::cout << FileList[i]<< " vector"<< '\n';
        }
     */
    
        return FileList; 
    }
    header is the same as the declaration
    Code:
    std::vector <std::string>   fill_vector(char *file);
    still getting error, I too added the include vector header file, as stated It is getting filled . and prints out by the use of that loop within the function.

    still gettting this
    Code:
    [Running] cd "/media/data/projects/VSC/C++/mh5000/" && g++ -Wall -std=gnu++1z *.cpp
    In file included from main.cpp:3:0:
    files.h:21:6: error: 'vector' in namespace 'std' does not name a template type
     std::vector <std::string>   fill_vector(char *file);
          ^~~~~~
    In file included from options.cpp:4:0:
    files.h:21:6: error: 'vector' in namespace 'std' does not name a template type
     std::vector <std::string>   fill_vector(char *file);
    both main.cpp, and options.cpp have the #include "files.h "

    so I got to stick #include <vector> in every cpp that is not even using it within the cpp of that file if one file (cpp) is using it?

    because that is what I did and the errors are no longer there.

    the pointer what a shot in the dark . but yeah. ok

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    well its not really working working, but I'm working on it.
    Last edited by userxbw; 10-05-2017 at 01:45 PM.

  5. #5
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    so I got to stick #include <vector> in every cpp that is not even using it within the cpp of that file if one file (cpp) is using it?
    No, you either get vector through the included header, or you include it explicitly. But you never need to include vector in a cpp file that doesn't use it.

    It seems to me like you're over-complicating things. You can push_back a char* directly into a vector<string>, no need for a function. Or are you writing these to explore your understanding of C++?

    p.s. As someone else pointed out, you should use -std=c++1z, not gnu++. It does not matter in this case, but you should stick to the other in general.

  6. #6
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Also -Wextra -pedantic are good options, especially when learning. Seriously, make use of them!

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by -Adrian View Post
    Also -Wextra -pedantic are good options, especially when learning. Seriously, make use of them!
    yeah I got a bunch but I am not using them until I get to that part of the program,
    remove that crap thing statement,
    and Wall seems to have a bunch of checks. need a college class semester just to cover them all, looks like.

    what I have written so far isn't really a lot just a few functions. so I'm not adding what I do know yet. just -Wall

    what is
    -Wextra -pedantic

    and where is that web sight?

    is there a set order or rule of thumb in what goes where in that line after g++ or gcc ?
    Last edited by userxbw; 10-05-2017 at 06:37 PM.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by -Adrian View Post
    No, you either get vector through the included header, or you include it explicitly. But you never need to include vector in a cpp file that doesn't use it.

    It seems to me like you're over-complicating things. You can push_back a char* directly into a vector<string>, no need for a function. Or are you writing these to explore your understanding of C++?

    p.s. As someone else pointed out, you should use -std=c++1z, not gnu++. It does not matter in this case, but you should stick to the other in general.
    that is what I thought, got a add it the the file it is being used in. but this thing complained, then I added it to the other files. and it stopped.

    what if it is using a function call within that file that the function call is using the vector?

    :
    note
    :
    try
    -std=c++1z

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning reference of vector
    By std10093 in forum C++ Programming
    Replies: 1
    Last Post: 09-06-2013, 08:02 AM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Returning a pointer to a vector
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 08-27-2009, 06:15 AM
  4. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  5. returning a vector
    By kes103 in forum C++ Programming
    Replies: 7
    Last Post: 02-13-2003, 10:15 AM

Tags for this Thread