?
Solved

unresolved external symbol "private: static char

Posted on 2001-09-10
2
Medium Priority
?
1,504 Views
Last Modified: 2009-05-19
I have created an application consisting of 1 base class and a number of sub classes. The definition of the base class is like:
class StaticBase {
    static char myC;
  public:
    static void setMyC();
};
When linking the program with MS VC++ 5.0, I get this error:
StaticBase.obj : error LNK2001: unresolved external symbol "private: static char  StaticBase::myC" (?myC@StaticBase@@0DA)
What am I doing wrong?

0
Comment
Question by:arefe
[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 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6469954
missing ; on the end of class declaration

class base {
   ...
};
0
 
LVL 1

Accepted Solution

by:
glebspy earned 400 total points
ID: 6470244
In c++, you must declare static data not only within the class itself but also outside.

This is the correct way to do it...

class StaticBase {
  static char myC;
public:
  static void setMyC();
};

char StaticBase::myC;
void StaticBase::setMyC(){...}



Don't worry.. myC is still private. But if you didn't include the line "char StaticBase::myC;" then *even members of StaticBase* (like setMyC()) wouldn't know what myC was, unless they were defined in class scope.

An alternative fix is therefore:

class StaticBase {
  static char myC;
public:
  static void setMyC(){
   //**DEFINE setMyC() HERE NOT OUTSIDE CLASS SCOPE**//
  }
};

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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.
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.

741 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