0

I have a problem where I have declared a static string vector in the .h file inside a class ,

.h file

static std::vector<std::string> VHDSigBuffer;

How to use this vector in my class function Implementations in cpp file?

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Shafi
  • 33
  • 2
  • 7

3 Answers3

2

In my_class.h header

class my_class
{  
public:
   // Declaration
   static std::vector<std::string> VHDSigBuffer;
};

In my_class.cpp implementation

// Definition
std::vector<std::string> my_class::VHDSigBuffer;

Now you can freely use my_class::VHDSigBuffer.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • thanks I have done this ,But while pushing values into it have problems , CAn yu help me on that too plzz ? – Shafi Nov 30 '13 at 08:04
  • 1
    @Shafi You need to show the code you are using when 'pushing values' and explain exactly what the problems are. Don't expect people to guess at the problems you are having. – john Nov 30 '13 at 08:17
  • IN CPP FILE class::myfunction(){ vectorvariable.push_back(mystring);} But when I debug the code I found that symbol has not been created so , nothing happens ,but the code compiles – Shafi Nov 30 '13 at 08:28
  • 1
    @Shafi This could be a problem with *static initialisation order*, but I'd need to see more code to be sure. In particular I'd need to know how and when you are calling class::myfunction. I think you should make a new question of this, and *post a lot more code*. – john Nov 30 '13 at 08:36
0

You need to instantiate it within your CPP file, with a line like this:

std::vector<std::string> MyClass::VHDSigBuffer;

This line will be outside any function definition in the CPP file.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • thanks I have done this ,But while pushing values into it have problems , CAn yu help me on that too plzz ? – Shafi Nov 30 '13 at 08:00
0

If I understand what you're saying correctly,

If you want to use this vector in its own class you can access it as VHDSigBuffer, or if you want to use it in another class you can access it as youClass::VHDSigBuffer.

Vahid Nateghi
  • 576
  • 5
  • 14