how to inititate vector

Jun 30, 2014 at 9:06am
I have class which has member Files. This should be vector of strings. Instance of class SRC should hold names of files which will be read from directory (later).

src.h
1
2
3
4
5
6
7
class SRC {  
public:
	SRC();
	static std::vector<std::string> Files; // source file(s)
	static std::vector<std::string>::iterator fileit; // File iterator
	static std::string file; // file name
};


src.cpp

1
2
3
4
5
6
7
#include "stdafx.h"
SRC::SRC(){
};

// static variables:
std::vector<std::string>::iterator SRC::fileit = Files.begin();
std::string SRC::file = "";// (*SRC::fileit); 


I have this error:
main.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > SRC::Files" (?Files@SRC@@2V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A)

It probably is because the Files is not defined. But how could I define it when it should be filled later? Just to give you idea about my program - here is how the SRC is used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* Meyers Singleton */
class S
{
public:
    static S& I(); // instance

    STAT Stat;
	
    std::vector<SRC> sources;
    std::vector<TARGET> destination;
    CLParser CLPars;

private:
    S();
    S(const S&);
    ~S();
	S& operator= (const S&);
    struct Params
    {
        int argc;
        char** argv;
    };
	static Params* param;
};


I start my program with command
S::I().CLPars.ParseArgs(argc, argv);
which will parse arguments. When it finds argument to read files, it will search for files in folder and then I need to save the names of the files into the vector Files.

So my question is, how to fix the linker errors. So that I could run the program and fill the vector with values.
Last edited on Jun 30, 2014 at 9:09am
Jun 30, 2014 at 9:16am
It probably is because the Files is not defined.
Yes.

You don't need fileit as a static variable. You can get the iterater at anytime later from Files

When you change the content of Files fileit is probably invalid anyway
Last edited on Jun 30, 2014 at 9:30am
Jun 30, 2014 at 9:26am
I don't know how to define Files in src.cpp.
Jun 30, 2014 at 9:31am
1
2
3
4
5
6
7
8
#include "stdafx.h"
SRC::SRC(){
};

// static variables:
std::vector<std::string> SRC::Files;
std::vector<std::string>::iterator SRC::fileit = Files.begin();
std::string SRC::file = "";// (*SRC::fileit);  
Jun 30, 2014 at 9:39am
Ah, so. I thought it would be redefinition.
Jun 30, 2014 at 10:53am
And you probably thought so, because you already have something in the header file. However, if that something would be a definition, then you could not include that header to more than one translation unit. You can, so it is not.

Furthermore, you already did have the SRC::file, which is an object just like the SRC::Files is.
Topic archived. No new replies allowed.