【问题标题】:How do you use a function inside another function?你如何在另一个函数中使用一个函数?
【发布时间】:2013-11-30 18:56:27
【问题描述】:

我有两个 .cpp 文件,一个叫做 practice.cpp,另一个叫做 adder.cpp。它们都在 Source Files 文件夹中。

拳头代码如下:

// adder.cpp

#include "stdafx.h" //include library


int addition(int a, int b) //start function
{
    int r = 0; //declare variables
    r = a + b; //actual code
    return r; //output of function
}

第二个代码:

// Practice.cpp

#include "stdafx.h"
#include "adder.cpp"
#include <iostream>
using namespace std;


int main(void)
{
    int number1 = 0;
    int number2 = 0;
    int number3 = 0;

    do 
    {
        printf("\n\nInsert value for first number\n\n");
        scanf("%d",&number1);

        printf("\nthe value ");
        printf("%d ",number1);
        printf("has been stored in memory location ");
        printf("%d",&number1);

        printf("\n\nInsert value for second number\n\n");
        scanf("%d",&number2);

        printf("\nthe value ");
        printf("%d ",number2);
        printf("has been stored in memory location ");
        printf("%d",&number2);

        number3 = addition(number1,number2);

        printf("%d",number3);



    }
    while (1==1);

    return 0;
}

但是代码不会编译。我得到了错误:

1>------ Build started: Project: Practice, Configuration: Debug Win32 ------
1>  Practice.cpp
1>c:\users\craig\documents\3rd year work\progamable     systems\practice\practice\practice.cpp(25): warning C4996: 'scanf': This function or     variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use     _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304)     : see declaration of 'scanf'
1>c:\users\craig\documents\3rd year work\progamable     systems\practice\practice\practice.cpp(33): warning C4996: 'scanf': This function or     variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use     _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304)     : see declaration of 'scanf'
1>Practice.obj : error LNK2005: "int __cdecl addition(int,int)" (?addition@@YAHHH@Z)     already defined in adder.obj
1>C:\Users\Craig\Documents\3rd year work\Progamable Systems\Practice\Debug\Practice.exe     : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我一直在寻找整个网络,但看起来我做得正确。我能做些什么来解决这个问题?谢谢!

【问题讨论】:

  • 不包含 cpp 文件。
  • "already defined in adder.obj" 是告诉你你定义了两次函数,这违反了 ODR。
  • @user34248 您能否将任何答案标记为正确答案? :)

标签: c++ function object


【解决方案1】:

不要包含 .cpp 文件,这就是编译器告诉您多个定义的原因。您必须创建一个头 .h 文件并将其包含在您的两个 .cpp 文件中,并放入以下内容:

// adder.h

#ifndef ADDER_H
#define ADDER_H

int addition(int, int);

#endif

预处理语句将告诉编译器只定义一次方法addition。成功了,祝你好运:)

【讨论】:

    【解决方案2】:

    当您#include adder.cpp 时,此文件中的整个代码将复制到此位置。因此,您最终得到了 addition 函数的 2 个定义。

    这就是为什么建议不要包含 cpp 文件,而是包含包含保护的头文件。

    【讨论】:

    • 感谢您的回答,但是加法的两个定义在哪里?我在 adder.cpp 文件中定义它,但我只在 practice.cpp 文件中使用它。它定义一次,使用一次。还是我糊涂了?请你能准确解释我是如何定义它两次的吗?谢谢!
    • 正如我在回答中所说的,当你包含一个文件时,它与在 practice.cpp 中编写 adder.cpp 的确切代码相同。因此,添加的函数定义现在有效地出现在两个文件中。这就是链接器抱怨的原因。
    • @user34248 编译了两个文件:adder.cpp 和 Practice.cpp,我们称这些文件为编译单元。这两个独立的编译单元现在都有一个addition() 的定义。最后,所有编译单元都链接在一起。当链接器现在尝试链接您的项目时,他会咕哝说函数 addition() 有两个定义,因为它在 adder.cpp 和 Practice.cpp 中
    【解决方案3】:

    去掉#include "adder.cpp",只在main函数前使用前向声明:

    int addition(int a, int b);
    

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 2018-07-02
      • 2018-12-02
      • 1970-01-01
      相关资源
      最近更新 更多