ヘッダーファイルで宣言され、ソースファイルに入力されたメインファイルの静的ベクター変数にアクセスするにはどうすればよいですか。実際のコードは少し大きいので、以下は私が試した例です。
class.h
class a{
private:
int x;
public:
friend void createVec();
};
void createVec();
static std::vector<a> vec;
ソース.cpp
include"class.h"
extern std::vector<a> vec; //if i remove this line code runs and don't show any error
// -of undefined variable
void createVec()
{
for(int i=0; i<9; i++)
{
vec.push_back(i); //while debugging and watching vec, code is able to populate vec
//but i doubt whether is it vector declared in class.h or not
}
}
main.cpp
#include "class.h"
extern std::vector<a> vec;
int main()
{
createVec();
cout<<vec.size() //it prints 0 instead of 10
}