error in vector member class

I am trying to make a class for all files and dirs of folder. Trying

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class DIRSandFILES
{
public:
        vector<char*>Dirs;
        vector<char*>Files;
        vector<char*>ChooseFiles;
        DIRSandFILES(char *dir);
        virtual ~DIRSandFILES();
protected:
private:
        bool CheckPath(const char* userPath);
        void dirsAndFilesToStack(char * dir);
        void chooseFiles(char* ext);
        void PuchDirsAndFiles(char * directory);
};


I have problem in
vector<char*>Dirs;

The program works fine before all stuff insert in DIRSandFILES class. Any ideas?
Last edited on
I'm gonna go out on a limb and say that you're not actually copying the arrays, you're only assigning pointers to string literals.
I would strongly suggest using std::string instead of char*.

It will drastically simplify things for you.
How i can do that? An example to a new to c and cpp?
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<std::string> dirs;

dirs.push_back( "/usr" );  // This works; std::string is constructible from a const char*
dirs.push_back( std::string( "/usr" ) );  // Same thing, but construction is explicit

// Now comparing strings is easier than char*:
if( dirs.front() == "/root" )
    std::cout << "Root directory\n";

// compared to:
if( strcmp( dirs.front(), "/root" ) == 0 )
    std::cout << "Root directory\n";
No .c_str() on line 11?
I agree with helios but the other point is still valid. Comparing string to const char* is much easier and intuitive with operator==. Of course this is all conjecture without being able to see an example that demonstrates the original problem. There could be many other problems, although I agree with the suggestions. std::string will make life easier and the program will be more intuitive, safer, and easier to maintain. It'll also allow you to focus on the requirements of the program instead of on reinventing the wheel.
Line 11 is the vector<char*> case, so no, no .c_str().
Topic archived. No new replies allowed.