I have a class that requires a const size_t
parameter in its constructor. It works as intended when I use it to declare a variable on the stack. But I get a compiler error if I use it to declare a member of another class. Typically, I get "unknown type name" when I use a named variable for the parameter, or "Expected ')'" when I use an integer constant.
The parameter isn't used for allocation; it is simply stored in a member variable.
This tiny program segment illustrates the problem:
#include <iostream>
#include "QuickBin.h"
class useless
{
const size_t m_size;
public:
useless(const size_t s) : m_size(s)
{
}
QuickBin m_store1(m_size); // Unknown type name 'm_size'
QuickBin m_store2(40); // Expected a right paren
};
int main(int argc, const char * argv[]) {
QuickBin locker(40); // No problem here; this works fine
std::cout << "Hello, World!\n";
return 0;
}
And here is the constructor for QuickBin:
QuickBin::QuickBin(const size_t s)
{
m_size = s;
}
I feel certain that I am missing something obvious, because I get errors on this code both from Visual Studio and from Xcode.
My final solution has to be compatible with C++11.
There is other stuff in the actual class but it is irrelevant to this problem. No memory allocation takes place.
I've gotten bleary-eyed in the last 2 days trying to find documentation to cover this and I have come up empty. I'll emphasize that even when I have used a named variable as a parameter, it is a constant whose value is known at compile time. Any ideas?