-1

I am learning C++ at school and I am adding some functions to my school project. We need to code a program, which searches a word in files in folders.

I am at the point, where I want to list all directories and files and pass them back (with return), so I can display (output) them at the console.

So I used a vector type string and pushed back all the paths I found... Then I wanted to give (pass) them back with return, but I got the Error:

error: cannot convert `std::vector<std::string, std::allocator<std::string> >' to `std::string*' in return

Here is some Code:

string OpenFolder(string path, const string constSearchedWord)


vector<string> vIndex;         //Vektor erzeugen
vector<string>::iterator i;    //Iterator (zum durchlaufen)

...

while ( 0 != ( dirEntry = readdir( dirHandle ) ) )
    {
        string name = dirEntry->d_name;
        fullPath = path + '\\' + name;
        vIndex.push_back(fullPath);
    }
    //Den Ordner schliessen
    closedir( dirHandle );

... If I wanted to write it into the console in this function:

for (i = vIndex.begin(); i < vIndex.end(); ++i)
{
    cout << static_cast<string>(*i) << endl;
}

is working easily, but I don't want to write it in this function into the console.

I want to do

return vIndex;

I think the problem is in the header:

string OpenFolder(string path, const string constSearchedWord)

I tried

vector OpenFolder(string path, const string constSearchedWord)

But this didn't work either.

I don't know what to set here:

->???<- OpenFolder(string path, const string constSearchedWord)
Sinmson
  • 227
  • 1
  • 4
  • 13

3 Answers3

1

Change the return type of function OpenFolder

string OpenFolder(string path, const string constSearchedWord)
~~~~~ should be std::vector < std::string >

Also,

use const reference for function arguments

std::vector<std::string> OpenFolder(const string& path,
                                    const string& constSearchedWord )
P0W
  • 46,614
  • 9
  • 72
  • 119
0

if you want to return the vector of strings from a method then the return type needs to be a vector of strings i.e.

std::vector<std::string> OpenFolder(string path, const string constSearchedWord)

Paul
  • 94
  • 1
  • 9
0

A function definition/declaration generally takes the form of ...

[return type] [function name]([arguments])

If you're returning vIndex from the OpenFolder function then the return type needs to match the type of vIndex, which is vector<string>.

gmbeard
  • 674
  • 6
  • 19