0

I can't figure out where I've gone wrong here. In my header:

static const char* ACH_DEBIT;

and then, in the constructor:

ACH_DEBIT = "ach_debit";

I get this error on compilation:

 error LNK2001: unresolved external symbol "public: static char const * const MyClass::ACH_DEBIT" (?ACH_DEBIT@MyClass@@2PEBDEB)

Please, someone reteach me CS101.

djechlin
  • 59,258
  • 35
  • 162
  • 290
MrDuk
  • 16,578
  • 18
  • 74
  • 133
  • possible duplicate of ["static const int" causes linking error (undefined-reference)](http://stackoverflow.com/questions/5508182/static-const-int-causes-linking-error-undefined-reference) – Ben Voigt Sep 04 '13 at 15:49

5 Answers5

3

This:

static char const* ACH_DEBIT;

is a declaration. You also need to separately define static constants in the implementation file:

char const* MyClass::ACH_DEBIT = "ach_debit";

… and you probably don’t want to reassign it in the constructor. And you probably want to enforce this by making ACH_DEBUG const (rather than just its contents). So change the declaration to

static char const* const ACH_DEBIT;

And the definition to

char const* const MyClass::ACH_DEBIT = "ach_debit";
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    Probably a top level `const` would also be appropriate. – Ben Voigt Sep 04 '13 at 13:45
  • Just making sure you did indeed mean to put `char const* const` and not `const char* const`? – MrDuk Sep 04 '13 at 14:25
  • @ctote Oh, good catch, but they are actually the same. I’ll correct it anyway to make it consistent. Furthermore I’m using `char const` everywhere to make it clear which `const` modifies what (i.e. the first modifies the pointee – `char` – while the second `const` modifies the pointer, i.e. `char const*`). – Konrad Rudolph Sep 04 '13 at 14:36
  • So a quick followup - how would someone, maybe accidentally, get around code that just uses the single const? I.e., how does adding the second const _enforce_ const-correctness while the single does not? – MrDuk Sep 09 '13 at 20:43
  • @ctote The two `const`s do different things, removing the outer `const` doesn’t violate `const`-correctness, it just makes it harder to enforce. Removing the inner `const` is invalid in C++11 and deprecated in C++03. – Konrad Rudolph Sep 09 '13 at 22:05
2

Your cpp file must contain a definition of ACH_DEBIT like so:

const char* ClassName::ACH_DEBIT;
user1233963
  • 1,450
  • 15
  • 41
1

You need to provide the definition for the static member. In the source file at global scope, do

const char* ClassName::ACH_DEBIT = "ach_debit";
Mahesh
  • 34,573
  • 20
  • 89
  • 115
1
static const char* ACH_DEBIT; 

is a static member declaration, and while you can assign to this variable in a constructor, that's probably not what you want.
Anyway you need to define (implement) the static member somewhere, for example in the cpp file:

const char* MyClass::ACH_DEBIT = "ach_debit";
spiritwolfform
  • 2,263
  • 15
  • 16
  • 1
    That doesn't define a member. – Ben Voigt Sep 04 '13 at 13:45
  • sure it doesn't, neither the question tells it is a member – spiritwolfform Sep 04 '13 at 15:09
  • 1
    The error message in the question talks about "`public: static char const * const MyClass::ACH_DEBIT`" which is clearly a member. But, ignoring whether the question said it was a member, your answer does. The first sentence of your answer says it "is a static member declaration". And then you said "you need to define the static member somewhere"... but then give a code snippet that **cannot do what you said it does**. – Ben Voigt Sep 04 '13 at 15:45
1

Static class member is not same thing as static variable or function. For class members it means, there is just one of the static member in existence, and it exists independent of any objects of the class. Static class member is basically same as normal exported global variable or function. So it must also exist and be initialized, even if no constructor of that class is ever executed.

If you want it to be a "normal" static variable, then do not put it inside a class definition, and do not put it into a .h file at all (because it will be duplicated in every .cpp which includes it).

If you want to have static class member, then in addition to declaring it in the defintion, you need to define the static member itself somewhere, in exactly one .cpp file, with line like this outside any method:

const char* ClassName::ACH_DEBIT = "ach_debit";

If you define it in many .cpp files (for example because you put that into an include file and include it to many .cpp files), linker will complain about multiple definitions. If you define it nowhere, you get the error in the question.

hyde
  • 60,639
  • 21
  • 115
  • 176