LNK2005和LNK1169同时出现,但找不到重复变量。

最近在做一些数值计算,当中需要一些双log插值,于是自行杜撰了一些双log插值代码,但是在调试的时候出现了LNK2005和LNK1169 错误,按照论坛上说明的排解方法并不奏效,不知错在何处?文件ION.cpp是主文件,文件udf_functions.cpp是外部文件。
代码如下:

#include <iostream>
#include "udf_functions.cpp"

using namespace std;
double *udf_interp1d(double x[], double y[], double u[], double v[], int method);

int main()
{
    double a[2] = { 1, 10 };
    double b[2] = { 5, 8 };
    double c[2] = { 2, 5 };
    double d[2];
    double* result;
    result = udf_interp1d(a, b, c, d, 22);
    std::cout << result;

}

udf_functions.cpp的代码如下:

#include<iostream>
#include<cmath>
#include<string>

using namespace std;

double *udf_interp1d(double x[], double y[], double u[], double v[], int method)
{
    // method value:
    // value    x      y
    //  11    linear-linear
    //  22       log-log
    int len_x = sizeof(x);
    int len_y = sizeof(y);
    int len_u = sizeof(u);

    if (len_x != len_y)
    {
        cout << "Error: input vector must have the same lenth.";
        exit(1);
    }
    if (len_x != len_u)
    {
        cout << "Error: arguement vector must have the same lenth of input vector.";
        exit(21);
    }
    if (len_y != len_u)
    {
        cout << "Error: arguement vector must have the same lenth of input vector.";
        exit(22);
    }
    switch (method)
    {
    case 22:
        for (int i = 0; i < len_u; i++)
        {
            for (int j = 0; j < len_u; j++)
            {
                if (u[i] == x[j])
                {
                    v[i] = y[j];
                }
                else if ((u[i] > x[j]) & (j < len_u -1))
                {
                    v[i] = log10(u[i] / x[j]) * log10(y[j + 1] / y[j]) / log10(x[j + 1] / x[j]);
                }
                else
                {
                    cout << "Error: argument vector beyond input domain.";
                }
            }
        }

    case 11:
        for (int i = 0; i < len_u; i++)
        {
            for (int j = 0; j < len_u; j++)
            {
                if (u[i] == x[j])
                {
                    v[i] = y[j];
                }
                else if ((u[i] > x[j]) & (j < len_u - 1))
                {
                    v[i] = -(u[i] - x[j]) * (y[j + 1] - y[j]) / (x[j + 1] - x[j]);
                }
                else
                {
                    cout << "Error: argument vector beyond input domain.";
                }
            }
        }

    default:
        cout << "Error: Wrong method.";
        break;
    }

    return v;
}

错误信息如下:
图片说明

include的本质是文本替换,所以你ION的头部就定义了一次udef_interp1d()这样编译ION.cpp时就有了这个函数。
然后在编译udf_functions.cpp时就又编译了一遍。

那么在链接时,就会发生重定义错误。

解决方法:

//ION.cpp
#include <iostream>
#include "udf_functions.cpp"//删掉这行

或者:
把声明丢进头文件,然后在ION.cpp中include头文件而非源文件。