weixin_45298206 2019-07-13 11:43 采纳率: 0%
浏览 1058

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;
}

错误信息如下:
图片说明

  • 写回答

1条回答 默认 最新

  • 巇岩流云 2019-07-26 17:12
    关注

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

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

    解决方法:

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python