0

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;
}
favo
  • 5,426
  • 9
  • 42
  • 61
Md Faisal
  • 2,931
  • 8
  • 29
  • 34

2 Answers2

1

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

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).

jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • when i uncomment //cout< – Md Faisal Jan 19 '12 at 05:28
  • That's because you're trying to print an entire `std::vector`. You should print each of its elements instead. E.g.: `vector result = bc.decode("12310122"); cout << result[0] << endl`. – jweyrich Jan 19 '12 at 05:31
  • why vector &result=bc.decode("dssdf") i have done it without '&' and its working and with '&' its not working – Md Faisal Jan 19 '12 at 06:28
  • There's no `&` there. I also recommend you to read a [good C++ introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – jweyrich Jan 19 '12 at 06:35