最近在做一些数值计算,当中需要一些双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;
}
错误信息如下: