Solved

C++ global class, unresolved external symbol

Posted on 2013-11-18
3
470 Views
Last Modified: 2013-11-18
When I compile the following it give me a link error:
Error      1      error LNK2001: unresolved external symbol "private: static enum Language i18n::currentLanguage" (?currentLanguage@i18n@@0W4Language@@A)      C:\Users\Public\0SBCS\VS\test\i18ntest\i18ntest\MenuItemONE.obj      i18ntest
My idea is to:
  + enumerate the languages
  + store the current language setting in a static class
  + later return a string based on the current language setting

C++ doesn't have static classes, so I'm using a class with static members and static fields. (Any suggested improvements?)
Language.h
#pragma once

  enum Language
  {
    ENGLISH,
    SPANISH,
    FRENCH
  };

Open in new window


i18n.h
#pragma once
#include "language.h"

class i18n
{
private:
  static Language currentLanguage;
public:
  static Language GetCurrentLanguage()
  {
      return currentLanguage;
  }
  static void SetCurrentLanguage(Language lang)
  {
      currentLanguage = lang;
  }
};

Open in new window


i18n.cpp
#include "StdAfx.h"
#include "i18n.h"

Open in new window


MenuItemONE.h
#pragma once
#include <string>
#include <iostream>
#include "Language.h"

class MenuItemONE
{
public:
    std::string GetText();
    Language GetCurrentLanguage();
};

Open in new window


MenuitemONE.cpp
#include "StdAfx.h"
#include "MenuItemONE.h"
#include "Language.h"
#include "i18n.h"


Language MenuItemONE::GetCurrentLanguage()
{
    return i18n::GetCurrentLanguage();
}


std::string MenuItemONE::GetText()
{
    Language cl = GetCurrentLanguage();
    switch(cl)
    {
        case ENGLISH:
        default:
            return "English ONE menu text";
            break;
        case SPANISH:
            return "Spanish ONE menu text";
            break;
        case FRENCH:
            return "French ONE menu text";
            break;
    }  
}

Open in new window

0
Comment
Question by:deleyd
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 39657340
You need to define your static class member currentLanguage declaration in your .cpp file:

Language i18n::currentLanguage = ENGLISH;
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 39657363
to add to before comment:

the statement phoffric posted should added to the i18n.cpp file and you should assign a default language.

#include "StdAfx.h"
#include "i18n.h" 

Language i18n::currentLanguage = English; 

Open in new window

i would omit including stdafx.h in that file and use the option 'do no use precompiled header' for that file in the c++ - precompiled header settings (both release and debug configuration).

note, static member functions can be defined in the h file directly. static member variables however need an instantiation in one (and only one) cpp file. it cannot be instantiated in the h file cause this could be included by multiple cpp files.

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 39657368
Language i18n::currentLanguage = ENGLISH;

i should make a refresh more often :-)

Sara
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now