Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ global class, unresolved external symbol

Posted on 2013-11-18
3
Medium Priority
?
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

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

Language i18n::currentLanguage = ENGLISH;
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 1000 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 35

Expert Comment

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

i should make a refresh more often :-)

Sara
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 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