jejej
jejej

Reputation: 51

Why is my string vector empty

Wher is my mistake I want to read a directory into a string vector. using the memberfunction getFileList. Iterating it in the main the string vector is empty. I fill in only one string (buffer) to check the vector and no files are being listed. Only the following output comes:

Singleton cstr
//verify success in opening dir
opened? [0x1d92630 ]
[ buffer ]
itVect[ buffer ]

Why?

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
#include <dirent.h>
#include "gtest/gtest.h"

class Singleton
{

public:
   std::vector<std::string> singletonVect;
   Singleton();
   void buildFileList(std::vector<std::string> filesVect);
   std::vector<std::string> getFileList(void);
   static Singleton& getInstance();
};

std::vector<std::string> strings = {"lkhdf","lfdjasdlk"};
Singleton& singleObj(Singleton::getInstance());

std::vector<std::string> openDirectory(std::string path) //opening any folder and saving all file-names in a vector<string>
{
   DIR* dir;
   dirent* pdir;
   std::vector<std::string> files;
   dir = opendir(path.c_str());

   std::cout << "opened? [" << dir << " ]\n";

   while (pdir = readdir(dir)) {
      files.push_back(pdir->d_name);
   }
   return files;
}

void Singleton::buildFileList(std::vector<std::string> filesVect)
{
   std::vector<std::string> f;
   std::string buffer = "";
   f = openDirectory("myFiles"); // pass which dir to open
   for (auto i = f.begin(); i != f.end(); ++i) {
      if ((*i).find(".exe") != std::string::npos) {
         buffer = "myFiles/" + (*i);
         filesVect.push_back(buffer);
      }
   }
}

std::vector<std::string> Singleton::getFileList(void)
{
   return singletonVect;
}

Singleton::Singleton()
{
   std::cout << "Singleton cstr\n";

   buildFileList(singletonVect);
}

Singleton& Singleton::getInstance()
{
   static Singleton singleObj;
   return singleObj;
}

int main(int argc, char **argv) {
   singleObj.singletonVect = singleObj.getFileList();

   singleObj.singletonVect.push_back("buffer");

   std::cout <<"[ "<< (*singleObj.singletonVect.begin())  << " ]\n";

   for (auto itVect = singleObj.singletonVect.begin(); itVect != singleObj.singletonVect.end(); itVect++) {
      std::cout << "itVect[ " << (*itVect) << " ]\n";
   }

}

Upvotes: 0

Views: 99

Answers (2)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

One problem is here:

void Singleton::buildFileList(std::vector<std::string> filesVect)

You're passing filesVect by value, which means that the function is working with a temporary. When the function returns, all of your hard work you have adding items to the vector goes up in smoke and disappears.

Pass by reference instead:

void Singleton::buildFileList(std::vector<std::string>& filesVect)

This is no different than if you did this:

int foo(int x)
{
   x = 10;
}

int main()
{
   int myInt = 0;
   foo(myInt);
   // myInt is still 0, not 10
}

Note that foo() takes the int parameter by value. No change was done to the caller's int, even though foo() changes the parameter.

Upvotes: 3

jejej
jejej

Reputation: 51

Well simple mistake. Dont do this again. By reference:

change

void buildFileList(std::vector<std::string> filesVect);

to

void buildFileList(std::vector<std::string>& filesVect);

the same here:

void Singleton::buildFileList(std::vector<std::string>& filesVect)
{
   std::vector<std::string> f;
   std::string buffer = "";
   f = openDirectory("myFiles"); // pass which dir to open
   for (auto i = f.begin(); i != f.end(); ++i) {
      if ((*i).find(".exe") != std::string::npos) {
         buffer = "myFiles/" + (*i);
         filesVect.push_back(buffer);
      }
   }
}

and it works.

Upvotes: 2

Related Questions