Md Faisal
Md Faisal

Reputation: 2991

can not find bug in the code vector usage

I can't find bug in this code

In function `BinaryCode::decode(std::string)':

undefined reference to `BinaryCode::m_vecStr'
undefined reference to `BinaryCode::m_vecStr'
undefined reference to `BinaryCode::m_vecStr'
undefined reference to `BinaryCode::m_vecStr'
undefined reference to `BinaryCode::m_vecStr'
more undefined references to `BinaryCode::m_vecStr' follow

http://codepad.org/PtZkGx6W

output is in above site:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;

class BinaryCode{
public:
BinaryCode(void);
~BinaryCode(void);
static vector<string> m_vecStr;

vector<string> decode(string message);

};

BinaryCode::BinaryCode(void){
}
BinaryCode::~BinaryCode(void){
}

vector<string> BinaryCode::decode(string message){
m_vecStr.clear();
char szNone[]={"NONE"};
m_vecStr.push_back(szNone);
m_vecStr.push_back(message);
return m_vecStr;
}

int main(){
BinaryCode bc;
  //cout<<bc.decode("12310122");
return 0;
}

Upvotes: 0

Views: 100

Answers (2)

jweyrich
jweyrich

Reputation: 32260

You must define the static member outside of the class declaration. Try adding this after the class declaration:

vector<string> BinaryCode::m_vecStr;

If you're declaring your class in a distinct file, make sure you define the static members in the implementation file (generally .cpp), not in the header file (.h).

Upvotes: 1

Alok Save
Alok Save

Reputation: 206616

It is not a bug it is a linker error which tells you that linker cannot find definition for m_vecStr.

You need to define a static variable, in your code you just declared it but forgot to define it.
Add the following definition:

vector<string> BinaryCode::m_vecStr;

only once in your source file.

Upvotes: 1

Related Questions