我使用 VS2012 Express 用 C++ 编写了以下代码。
void ac_search(
uint num_patterns, uint pattern_length, const char *patterns,
uint num_records, uint record_length, const char *records,
int *matches, Node* trie) {
// Irrelevant code omitted.
}
vector<int> ac_benchmark_search(
uint num_patterns, uint pattern_length,
const char *patterns, uint num_records, uint record_length,
const char *records, double &time) {
// Prepare the container for the results
vector<int> matches(num_records * num_patterns);
Trie T;
Node* trie = T.addWord(records, num_records, record_length);
// error line
ac_search(num_patterns, pattern_length, patterns, num_records,
record_length, records, matches.data(), trie);
// Irrelevant code omitted.
return matches;
}
我在函数调用行收到错误identifier "ac_search" is undefined
。我在这里有点困惑。因为该函数ac_search
被声明为全局函数(不在任何容器内)。为什么我不能在这个地方打电话?我错过了什么吗?
更新
我尝试忽略不相关的代码,然后逐渐包含它,发现一切都很好,直到我包含ac_search
我得到上述错误的外循环。这是函数的更新代码ac_search
:
void ac_cpu_string_search(uint num_patterns, uint pattern_length, const char *patterns,
uint num_records, uint record_length, const char *records, int *matches, Node* trie)
{
// Loop over all records
//for (uint record_number = 0; record_number < num_records; ++record_number)
//{
// // Loop over all patterns
for (uint pattern_number = 0; pattern_number < num_patterns; ++pattern_number)
{
// Execute string search
const char *ptr_record = &records[record_number * record_length];
const char *ptr_match = std::strstr(ptr_record, &patterns[pattern_number * pattern_length]);
// If pattern was found, then calculate offset, otherwise result is -1
if (ptr_match)
{
matches[record_number * num_patterns + pattern_number] = static_cast<int>(std::distance(ptr_record, ptr_match));
}
else
{
matches[record_number * num_patterns + pattern_number] = -1;
}
// }
//}
}
更新 2
我认为该错误与addWord
属于该类的函数有关Trie
。当我注释掉这个函数时,我没有再收到错误了。
Node* Trie::addWord(const char *records, uint num_records, uint record_length)
{
// Loop over all records
for (uint record_number = 0; record_number < num_records; ++record_number)
{
const char *ptr_record = &records[record_number * record_length];
string s = ptr_record;
Node* current = root;
if ( s.length() == 0 )
{
current->setWordMarker(); // an empty word
return;
}
for ( int i = 0; i < s.length(); i++ )
{
Node* child = current->findChild(s[i]);
if ( child != NULL )
{
current = child;
}
else
{
Node* tmp = new Node();
tmp->setContent(s[i]);
current->appendChild(tmp);
current = tmp;
}
if ( i == s.length() - 1 )
current->setWordMarker();
}
return current;
}
void ac_search(
uint num_patterns, uint pattern_length, const char *patterns,
uint num_records, uint record_length, const char *records,
int *matches, Node* trie) {
// Irrelevant code omitted.
}
vector<int> ac_benchmark_search(
uint num_patterns, uint pattern_length,
const char *patterns, uint num_records, uint record_length,
const char *records, double &time) {
// Prepare the container for the results
vector<int> matches(num_records * num_patterns);
Trie T;
Node* trie = T.addWord(records, num_records, record_length);
// error line
ac_search(num_patterns, pattern_length, patterns, num_records,
record_length, records, matches.data(), trie);
// Irrelevant code omitted.
return matches;
}