[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ global class, unresolved external symbol

Posted on 2013-11-18
3
Medium Priority
?
501 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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

872 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