Solved

C++ global class, unresolved external symbol

Posted on 2013-11-18
3
480 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 33

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 33

Expert Comment

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

i should make a refresh more often :-)

Sara
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 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