\Eigen 'long long int std::abs 冲突
时间: 2023-11-03 21:04:10 浏览: 27
这个问题可能是因为您同时使用了 `using namespace std;` 和 `using namespace Eigen;`,导致了 `std::abs` 和 `Eigen::abs` 冲突。为了解决这个问题,您可以使用以下两种方法之一:
1. 明确指定要使用的 `abs` 函数的命名空间:
```cpp
long long int x = -123;
auto y1 = std::abs(x); // 使用 std 命名空间下的 abs 函数
auto y2 = Eigen::abs(x); // 使用 Eigen 命名空间下的 abs 函数
```
2. 避免使用 `using namespace`,而是使用单独的 `using` 语句引入需要使用的类或函数:
```cpp
#include <iostream>
#include <Eigen/Core>
int main()
{
long long int x = -123;
auto y1 = std::abs(x); // 使用 std 命名空间下的 abs 函数
auto y2 = Eigen::abs(x); // 使用 Eigen 命名空间下的 abs 函数
std::cout << y1 << " " << y2 << std::endl;
return 0;
}
```
相关问题
D:\ZBY\CodeBase\eigen-3.4.0\Eigen\src\LU\FullPivLU.h:715: error: conflicting declaration of C function 'long long int std::abs(long long int)' using std::abs; ^
这个错误是因为在你的代码中使用了 `std::abs` 函数,但是它与 `long long int` 类型存在冲突。解决这个问题的方法是使用 `<cmath>` 头文件中的 `llabs` 函数来代替 `std::abs` 函数,`llabs` 函数可以处理 `long long int` 类型的整数。
你只需要将代码中的 `std::abs` 改为 `llabs` 即可,例如:
```
long long int x = -12345;
long long int y = llabs(x);
```
如果你需要使用 `std::abs` 函数来处理其他类型的数据,你可以使用类型转换来解决冲突,例如:
```
int x = -12345;
long long int y = std::abs(static_cast<long long int>(x));
```
D:\ZBY\CodeBase\eigen-3.4.0\Eigen\src\SVD\SVDBase.h:150: error: conflicting declaration of C function 'long long int std::abs(long long int)' using std::abs; ^
这个错误可能是因为您在代码中使用了 `std::abs` 函数,但是有另一个名为 `abs` 的函数与其冲突了。建议您使用 `std::abs` 代替 `abs`,或者使用 `using namespace std;` 来避免命名冲突。例如:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long long int x = -123456789;
cout << abs(x) << endl; // 冲突
cout << std::abs(x) << endl; // 使用 std::abs
return 0;
}
```
如果您仍然想使用 `abs` 函数,您可以将其重命名为其他名称,如:
```cpp
#include <iostream>
#include <cmath>
namespace mymath {
using std::abs;
}
int main() {
long long int x = -123456789;
cout << abs(x) << endl; // 冲突
cout << mymath::abs(x) << endl; // 重命名为 mymath::abs
return 0;
}
```