C++ 标识符未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19466014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identifier is undefined
提问by Hawk
I wrote the following code in C++ using VS2012 Express.
我使用 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;
}
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?
我identifier "ac_search" is undefined
在函数调用行收到错误。我在这里有点困惑。因为该函数ac_search
被声明为全局(不在任何容器内)。为什么我不能在这个地方打电话?我错过了什么吗?
Update
更新
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
:
我尝试忽略不相关的代码,然后逐渐包含它,发现一切都很好,直到我包含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;
}
// }
//}
}
Update 2
更新 2
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.
我认为该错误与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;
}
回答by Hawk
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
从更新2和缩小问题范围后,我们很容易发现函数末尾少了一个大括号addWord
。编译器永远不会明确识别这样的语法错误。相反,它将假定缺少的函数定义位于某个其他目标文件中。链接器会抱怨它,因此将直接归类到广泛的错误短语之一下,即identifier is undefined
. 合理地,因为使用当前语法,下一个函数定义(在本例中为ac_search
)将包含在addWord
范围内。因此,它不再是一个全局函数。这就是为什么编译器不会在外面看到这个函数addWord
并将抛出此错误消息,指出没有这样的功能。在这篇文章中可以找到关于编译器和链接器的非常好的阐述
回答by user1764961
Are you missing a function declaration?
您是否缺少函数声明?
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);
Add it just before your implementation of ac_benchmark_search.
在您实施 ac_benchmark_search 之前添加它。
回答by Don Larynx
You may also be missing using namespace std;
你也可能失踪 using namespace std;