gpt4 book ai didi

c++ - 标识符未定义

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:48:08 27 4
gpt4 key购买 nike

我使用 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

我认为错误与属于类Trie 的函数addWord 有关。当我注释掉这个函数时,我没有再得到错误。

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;
}

最佳答案

从update 2来看,缩小问题范围后,我们很容易发现函数addWord的末尾少了一个大括号。编译器永远不会显式识别此类语法错误。相反,它将假定缺少的函数定义位于其他某个目标文件中。链接器会提示它,因此直接将其归类为广泛的错误短语之一,即 identifier is undefined。合理的是,因为使用当前语法,下一个函数定义(在本例中为 ac_search)将包含在 addWord 范围内。因此,它不再是全局函数。这就是为什么编译器不会在 addWord 之外看到这个函数,并且会抛出这条错误消息,指出没有这样的函数。关于编译器和链接器的详细说明可以在此 article 中找到

关于c++ - 标识符未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com