Larry Turtis Larry Turtis - 1 year ago 76
C++ Question

Unable to reference private static member variable: Compiler error

I'm new to C++, and I'm trying to use a static member variable as a "dictionary" in a translation program.

I have two files, alphabet.h, which looks like this:

#ifndef ALPHABET_H
#define ALPHABET_H
#include <map>
#include <vector>

class Alphabet {
typedef std::vector<std::string> letterType;
typedef std::map<std::string, letterType> alphabetType;
alphabetType getAlphabet();
static alphabetType m_alphabet;


And alphabet.cpp, which looks like this:

#include "alphabet.h"

static Alphabet::alphabetType m_alphabet = {{"ὁ",{"o"}}};

Alphabet::alphabetType Alphabet::getAlphabet() {
return Alphabet::m_alphabet;

For some reason when I attempt to compile, I get an error from g++.

In function
alphabet.cpp:6: undefined reference to
collect2: error: ld returned 1 exit status

I would appreciate any insight into what I'm doing wrong.

Answer Source
static Alphabet::alphabetType m_alphabet = {{"ὁ",{"o"}}};

should be

Alphabet::alphabetType Alphabet::m_alphabet = {{"ὁ",{"o"}}};

Here, you define an other variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download