我有这个 C++ 片段,我试图理解:
在 .hh 文件中:
class A
{
private:
//recordDelimiter is a '+' character
// I can do this because char is an integral type!
static const char recordDelimiter = '+';
void f()
{
....
//serializedData is a std:;string
//Get number of times A::recordDelimiter is found i.e. Number of objects
// Non-Functional
int times = (int) std::count (serializedData.begin(), serializedData.end(), A::recordDelimiter);
// Functional
const char recDel = A::recordDelimiter;
int times = (int) std::count (serializedData.begin(), serializedData.end(), recDel);
// Functional
int times = (int) std::count (serializedData.begin(), serializedData.end(), '+');
....
}
};
从 std::count 参考这是函数的签名:
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val)
所以我不明白为什么使用A::recordDelimiter
代替'+'
是一个问题。编译给了我 undefined reference to
A::recordDelimiter'`
所以我的问题基本上是,为什么我上面的非功能代码不起作用?以及如何做到这一点:
const char recDel = USerializer::recordDelimiter;
然后将其传递给 std::count 是否有效?