【问题标题】:Compiler gives error about undefined reference to a function [duplicate]编译器给出关于未定义函数引用的错误[重复]
【发布时间】:2017-05-16 08:31:30
【问题描述】:

以下是我的代码编译器说 undefined reference to function 。请详细说明该怎么做。为什么它会给布尔函数 isPalindrome() 关于 undefined reference 的错误?

int main()
{    
        cout << "Please enter a string: ";
        cin >> input;

        cout<<"Vowel Count"<<vowelcount(input);
        while(1)
        {
                if(isPalindrome())
                {
                        palindrome_count++;
                }
        }
        cout<<"palindrome_count"<<palindrome_count;
}
bool isPalindrome(string input)
{
        do{

                if (input == string(input.rbegin(), input.rend())) {
                        cout << input << " is a palindrome\n";
                }
        }
        while(1);
}

【问题讨论】:

  • 下次请发一个完整的程序。您在main() 之前省略了一些与问题相关的内容

标签: c++ function boolean undefined-reference


【解决方案1】:

错误消息准确地告诉您您需要知道的内容。 IsPalindrome 在您使用之前未在您的代码中定义。确保在您引用它的位置(即在 main 上方)或原型函数上定义它。

【讨论】:

    【解决方案2】:

    我想,你忘记了函数声明。所以,把前面的声明放在main() 函数上面。喜欢:

    bool isPalindrome(string input);
    

    【讨论】:

    • 正是我忘记了将参数输入传递给函数。我解决了非常感谢。
    • 您是在推荐 op 调用 ub? O_o
    • 您误会了错误。如果函数没有被声明 - 你会得到关于 unknown identifiercompiler 错误,而不是 linker 错误。此错误表明函数已声明,但未定义。如果您查看函数定义的签名 (bool isPalindrome(string input)) 以及它的调用方式 (isPalindrome()) - 您会注意到它们不匹配,这很可能是错误的原因。
    猜你喜欢
    • 2018-11-29
    • 2018-04-15
    • 1970-01-01
    • 2015-03-07
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多