So, yesterday I looked at SO and could not find the answer to the following. This situation came from some code that I am working with, but here is MCVE to demonstrate it.
I have a class A defined in Ah that has only one static const in it. I already initialized it in the header.
#ifndef A_H_
#define A_H_
class A {
public:
static const int test = 5;
~A(){};
};
#endif
Then I have class B, which needs to access the static const const from class A. In this example, it will deeply copy the value into the vector.
#ifndef B_H_
#define B_H_
#include "A.h"
#include <vector>
#include <iostream>
class B {
private:
std::vector<int> testVec;
public:
B(){
testVec.push_back((const int)A::test);
std::cout<< testVec.front() << std::endl;
}
~B(){};
};
#endif
Then basically I just call the constructor of class B.
#include "B.h"
int main() {
B b;
return 0;
}
: , . extern, . , ? ( , ).
:
Invoking: Cygwin C++ Linker
g++ -o "S_Test_p1.exe" ./src/S_Test_p1.o
./src/S_Test_p1.o:S_Test_p1.cpp:(.rdata$.refptr._ZN1A4testE[.refptr._ZN1A4testE]+0x0): undefined reference to `A::test'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: S_Test_p1.exe] Error 1
: , , A:: test B.h( , ). , . - . , ?