static-members

error LNK2001: unresolved external symbol public: static class [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-24 07:29:04
问题 This question already has answers here : What is an undefined reference/unresolved external symbol error and how do I fix it? (32 answers) Closed 5 years ago . I cannot figure out why i am recieving this error. Can anyone lend a hand. I need to declare VideoCapture capture in the header file and call it in Video.cpp Video.h class Video { public: static VideoCapture capture; //Default constructor Video(); //Declare a virtual destructor: virtual ~Video(); //Method void Start(); private: };

C++ static const variable and destruction

ε祈祈猫儿з 提交于 2019-12-24 06:29:06
问题 I have encountered a strange behavior with a simple C++ class. classA.h class A { public: A(); ~A(); static const std::string CONST_STR; }; classA.cpp #include "classA.h" #include <cassert> const std::string A::CONST_STR("some text"); A::A() { assert(!CONST_STR.empty()); //OK } A::~A() { assert(!CONST_STR.empty()); //fails } main.cpp #include <memory> #include <classA.h> std::auto_ptr<A> g_aStuff; int main() { //do something ... g_aStuff = std::auto_ptr<A>(new A()); //do something ... return

C++ static const variable and destruction

吃可爱长大的小学妹 提交于 2019-12-24 06:28:17
问题 I have encountered a strange behavior with a simple C++ class. classA.h class A { public: A(); ~A(); static const std::string CONST_STR; }; classA.cpp #include "classA.h" #include <cassert> const std::string A::CONST_STR("some text"); A::A() { assert(!CONST_STR.empty()); //OK } A::~A() { assert(!CONST_STR.empty()); //fails } main.cpp #include <memory> #include <classA.h> std::auto_ptr<A> g_aStuff; int main() { //do something ... g_aStuff = std::auto_ptr<A>(new A()); //do something ... return

Why can't a static constexpr member variable be passed to a function?

南笙酒味 提交于 2019-12-24 05:58:30
问题 The following code produces an undefined reference to 'Test::color' . #include <iostream> struct Color{ int r,g,b; }; void printColor(Color color) { //printing color } class Test { static constexpr Color color = {242,34,4}; public: void print(){ printColor(color); } }; int main() { Test test; test.print(); return 0; } Why does this code produce the above error and what is the best way to avoid it, considering I want to use the latest version of the standard, C++17? Should I define the static

undefined reference error due to use of static variables [duplicate]

╄→гoц情女王★ 提交于 2019-12-24 04:59:21
问题 This question already has answers here : static variable link error (2 answers) Closed 4 years ago . I asked a question earlier today about singletons, and I'm having some difficulties understanding some errors I encountered. I have the following code: Timing.h class Timing { public: static Timing *GetInstance(); private: Timing(); static Timing *_singleInstance; }; Timing.cpp #include "Timing.h" static Timing *Timing::GetInstance() { //the first error if (!_singleInstance) { _singleInstance

Linker gives error “undefined symbol” for integral static const members used in certain contexts [duplicate]

拜拜、爱过 提交于 2019-12-23 21:13:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++ - defining static const integer members in class definition Note: There are several extant questions re similar issues, but I have reviewed many of them and cannot find an answer that explains this behavior: Say I have code such as the following (in a header file) class Foo { static const int TEST = 33; public: void willItWork(void) { printf("%d", std::max(TEST, 75)); // embedded platform, no streams } };

Calling subclass constructor from static base class method

北战南征 提交于 2019-12-23 18:21:24
问题 Ok... in Objective C you can new up a subclass from a static method in the base class with 'new this()' because in a static method, 'this' refers to the class, not the instance. That was a pretty damn cool find when I first found it and I've used it often. However, in C# that doesn't work. Damn! So... anyone know how I can 'new' up a subclass from within a static base class method? Something like this... public class MyBaseClass{ string name; public static Object GimmeOne(string name){ //

Why are static members of template classes not unique

倖福魔咒の 提交于 2019-12-23 12:18:54
问题 Take a look at the following code: #include <iostream> template <typename T> class Foo { public: static T bar; }; template <typename T> typename T Foo<T>::bar; int main() { std::cout << "Foo<int>::bar : " << &Foo<int>::bar << std::endl; std::cout << "Foo<double>::bar : " << &Foo<double>::bar << std::endl; return 0; } This will print out 2 different addresses. I can understand why in this case, bar is of type T and thus instantiations of different T's in Foo<T> will get you different static

“Static counter” for types behaves weirdly

丶灬走出姿态 提交于 2019-12-23 12:13:24
问题 I'm developing an entity-based component system, and I'm trying to assign a certain index to component types: static std::size_t getNextTypeId() { static std::size_t lastTypeIdBitIdx{0}; ++lastTypeIdBitIdx; // This line produces the output at the end of the question std::cout << lastTypeIdBitIdx << std::endl; return lastTypeIdBitIdx; } // I'm assuming that TypeIdStorage<T1>::bitIdx will always be different // from TypeIdStorage<T2>::bitIdx template<typename T> struct TypeIdStorage { static

Static table generation works with GCC but not clang; is clang bugged?

大兔子大兔子 提交于 2019-12-23 09:59:46
问题 I wrote some code once upon a time that generated a static table/array at compile time for some template metaprogramming (the idea is that C-style strings can be built at compile time (they're just char arrays)). The idea and the code is based off David Lin's answer: #include <iostream> const int ARRAY_SIZE = 5; template <int N, int I=N-1> class Table : public Table<N, I-1> { public: static const int dummy; }; template <int N> class Table<N, 0> { public: static const int dummy; static int