【问题标题】:Prototype Functions C++原型函数 C++
【发布时间】:2017-03-21 04:40:59
【问题描述】:

在 Xcode 中声明原型函数时出现构建错误。我正在用 C++ 编写。剧本摘自我教授的讲座。下面附上构建错误的图片以及脚本本身。 注意:我只在尝试声明原型函数时遇到构建问题。就好像 Xcode 试图从库中提取函数,但没有识别它。

Code: 
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <cstdlib>

using namespace std;

int calcSquare (int num) ;

int main ()
{

    int num = 5;

    int result;

    result = calcSquare(num);

    cout << "The Square of " << num << " is " <<  result << endl;

    return 0;

}

错误:https://farm3.staticflickr.com/2871/33406384892_68ee0843c7_b.jpg

【问题讨论】:

  • 您必须在项目中的任何 cpp 文件中定义您的函数。
  • 在你养成习惯之前,尽量避免使用using namespace std,而是使用std::前缀。它可以提供分离。
  • 你知道你必须实际编写函数,对吧?
  • 好的,我只在“int main()”上方包含了函数定义,构建成功。谢谢@Michael Nastenko,immibis。 tadman 我对编程很陌生,任何关于“std:: 前缀”的参考的建议,任何和所有 CPP 文档都非常感谢。

标签: c++ function-prototypes xcode8.2


【解决方案1】:

您的程序的问题是您稍后在源代码中忘记定义函数原型。 现在,你只是有一个函数原型( int calcSquare ),但你还是调用了那个函数。

这会引发链接器异常,如图所示。

换句话说,在源代码的某处定义函数以使用&lt;cmath&gt;, &lt;cctype&gt;, &lt;cstdlib&gt; 模块和库。

在函数原型之后,稍后声明函数:

int calcSquare (int num) {
   ...
}

我希望这会有所帮助! :)

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 2010-12-06
    • 2012-07-15
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多