问题描述
我使用 VS2012 Express 用 C++ 编写了以下代码.
I wrote the following code in C++ using VS2012 Express.
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
被声明为全局(不在任何容器内).为什么我不能在这个地方打电话?我错过了什么吗?
I get the error identifier "ac_search" is undefined
at the function invoking line. I am a bit confused here. because the function ac_search
is declared as a global (not inside any container). Why can't I call it at this place? Am I missing something?
更新
我尝试忽略不相关的代码,然后逐渐包含它,发现一切都很好,直到我包含 ac_search
的外循环我收到上述错误.这是函数 ac_search
的更新代码:
I tried ignore irrelevant code and then included it gradually and found that everything is fine until I include the outer loop of ac_search
I get the aforementioned error. here is updated code of the function 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
我认为该错误与属于 Trie
类的 addWord
函数有关.当我注释掉这个函数时,我没有再收到错误.
I think the error has something to do with the function addWord
which belongs to the class Trie
. When I commented out this function, I did not get the error anymore.
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;
}
推荐答案
从更新2缩小问题范围后,我们很容易发现addWord
addWord.编译器永远不会明确识别这样的语法错误.相反,它将假定缺少的函数定义位于某个其他目标文件中.链接器会抱怨它,因此将直接归类到广泛的错误短语之一,即
identifier is undefined
.合理地,因为使用当前语法,下一个函数定义(在本例中为 ac_search
)将包含在 addWord
范围内.因此,它不再是一个全局函数.这就是为什么编译器不会在 addWord
之外看到这个函数,并且会抛出这个错误消息,指出没有这样的函数.可以在这篇文章
From the update 2 and after narrowing down the problem scope, we can easily find that there is a brace missing at the end of the function addWord
. The compiler will never explicitly identify such a syntax error. instead, it will assume that the missing function definition located in some other object file. The linker will complain about it and hence directly will be categorized under one of the broad the error phrases which is identifier is undefined
. Reasonably, because with the current syntax the next function definition (in this case is ac_search
) will be included under the addWord
scope. Hence, it is not a global function anymore. And that is why compiler will not see this function outside addWord
and will throw this error message stating that there is no such a function. A very good elaboration about the compiler and the linker can be found in this article
这篇关于标识符未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!