?
Solved

unresolved external symbol "private: static char

Posted on 2001-09-10
2
Medium Priority
?
1,519 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
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

809 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