I like to program a class, which saves a value on construction in a const member, but the compiler complains, that it's not initialized.
Test.h:
#ifndef TEST_H_
#define TEST_H_
class Test {
public:
const int testV;
Test() = delete;
Test(int in);
};
#endif
Test.cpp:
#include "Test.h"
const int testV;
Test::Test(int in) : testV(in) {}
int main() {
Test test(500);
int count = 0;
while(count < test.testV) {
count ++;
}
return 0;
}
g++:
g++ Test.cpp
error:
Test.cpp:3:11: error: uninitialized ‘const testV’ [-fpermissive]
3 | const int testV;
| ^~~~~
The default constructor is disabled in this class and the const member gets initialized in the second constructor, which requires a parameter input, which is used as a constant value for the const member. But the compiler returns only this error message and nothing else. It doesn't make any sense to me, because it has to be initialized on construction and there's no way around this. I've restarted the computer to reset the bug, but it was not gone so I guess I miss something.
Later this kind of code has to be included in another class.
The problem is that you've an extra const int testV;
which is uninitialized in your source file.
Thus, to solve this, just remove that extra const int testV;
from source file.