【问题标题】:How i can use functions in multiple files, c++我如何在多个文件中使用函数,C++
【发布时间】:2017-04-01 17:13:16
【问题描述】:

我在使用多个文件时遇到了一点问题。 我的任务是使用三个文件:function.hfunction.cppprog.cpp

function.h中我定义了每个函数。

function.cpp中我放了每个函数的代码。

prog.cpp 我必须调用函数。我没有任何定义。

我有这些错误:

"void __cdecl randInt(int *,int)" (?randInt@@YAXPAHH@Z) already defined in function.obj

"void __cdecl showInt(int *,int)" (?showInt@@YAXPAHH@Z) already defined in function.obj

One or more multiply defined symbols found

function.cpp:

#include <iostream>
using namespace std;

void randInt(int *arr, int size) {
    for (int *i = arr; i < arr + size; i++) {
        *i = rand() % 10;
    }

}
void showInt(int *arr, int size) {
    cout << "Int Massive" << endl;
    for (int *i = arr; i < arr + size; i++) {
        cout << *i << ", ";
    }
    cout << endl;
}

function.h:

#pragma once
void randInt(int *, int);
void showInt(int *, int);

prog.cpp:

#include <iostream>
#include <ctime>;
using namespace std;
#include "function.h"
#include "function.cpp"


int main()
{
    srand(time(0));
    int n = 10;
    int *arrInt = new int[10];
    randInt(&arrInt[0], n);
    showInt(&arrInt[0], n);
    return 0;
}

【问题讨论】:

  • 您的prog.cpp 明确包含function.cpp。那是你的问题。
  • 删除#include "function.cpp"
  • 在 function.h 中我定义了每个函数。 - 你声明了这些函数。 在function.cpp 中我把每个函数的代码。 - 你定义 函数。要从任何地方调用函数,您只需要声明。所以你应该只包含这些声明所在的头文件。
  • function.h 文件中包含#include function.cpp 并删除为上述cmets 中提到的#include function.cpp 或尝试使用#ifndef#ifndef 检查给定的标记是否已在文件或包含文件的前面#defined;如果没有,它包含它之间的代码
  • @daemon7osh 你误解了一些东西。在*.h 中通常不包含*.cpp

标签: c++ function file


【解决方案1】:

包含 .cpp 文件是不正确且不必要的,因此删除#include "function.cpp" 应该没问题。

【讨论】:

  • @chbchb55 仅删除 #include " function.cpp" 会导致对函数的未定义引用。将您的 function.cpp 文件包含到 .h 文件中
  • @daemon7osh @chbchb55 在*.h 中包含*.cpp 是零意义。 OP 只需要删除 #include "function.cpp" 即可。
  • 你原来的答案是正确的,但现在我认为它不是。
  • @daemon7osh 任何大型 C++ 通常都有多个 .cpp 文件。每个 C++ 编译器都支持从 多个 .cpp 文件构建单个可执行文件。您无需将它们包含在任何地方即可。您只需要正确配置您的项目,您的错误就会消失。
  • @daemon7osh 我将答案退回到原始版本,因为您不需要在此处将function.h 包含在function.cpp 中。它不会造成任何伤害,代码也可以在没有它的情况下运行。
【解决方案2】:

你应该用这种方式编辑你的文件

function.h

    #pragma once
    void randInt(int *, int);
    void showInt(int *, int);

function.cpp

    #include <iostream>
    #include "function.h"
    using namespace std;

    void randInt(int *arr, int size) {
        for (int *i = arr; i < arr + size; i++) {
            *i = rand() % 10;
        }

    }
    void showInt(int *arr, int size) {
        cout << "Int Massive" << endl;
        for (int *i = arr; i < arr + size; i++) {
            cout << *i << ", ";
        }
        cout << endl;
    }

prg.cpp

    #include <iostream>
    #include <ctime>;
    #include "function.h"
    using namespace std;

    int main()
    {
        srand(time(0));
        int n = 10;
        int *arrInt = new int[10];
        randInt(&arrInt[0], n);
        showInt(&arrInt[0], n);
        return 0;
     }

然后您可以通过将 prg.cpp 与您的 function.cpp 文件链接来编译您的程序。如果您使用的是 g++ 编译器,您可以按照以下方式进行。

    g++ prg.cpp fuction.cpp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多