C++ bug解决经验(一)
1 error: expected initializer before ‘namespace’
仔细检查下,头文件定义函数时未添加分号,或者函数未写完就编译了。
2 ‘typedef’ was ignored in this declaration
原因:结构体没有定义别名
解决:曲调typedef或者在结构体括号外添加别名
typedef struct Vehicle
{
std::string name;
}Veh; // veh就是别名
3 坐标转换之后的角度,为什么只能按照固定轴序旋转
因为其实按照XYZ或者ZYX理论上都是可行的,只不过不同顺序所旋转的角度不一样,所以在计算的时候,我们会设定一种旋转方式,然后列出方程,最后解算的角度,当然要按照设定的顺序旋转了。
4 error: expected unqualified-id before ‘if’
error: expected unqualified-id before ‘if’
原始代码:
bool Swap(std::vector<double>& left, std::vector<double>& right, int dimension, double distance /* = 0.1 */)
{
if (dimension >= left.size() || dimension >= right.size())
return false;
}
// if下面划线,报错
if (fabs(left[dimension] - right[dimension]) > distance) {
/* code */
}
if (fabs(left[dimension] - right[dimension]) > distance) {
Swap(left, right);
return true;
}
}
解决:
第一个if花括号不完整,补齐即可。
5 ubuntu c++使用eigen提示"fatal error: Eigen/Dense: No such file or directory"的解决办法
源代码里面使用了
#include <Eigen/Dense>
查看自己电脑,eigen的安装路径为:
/usr/include/eigen3/Eigen
解决办法:
(1)修改头文件为
#include <eigen3/Eigen/Dense>
(2) 复制/usr/include/eigen3/Eigen
中的文件到/usr/include/Eigen
,可使用命令:
cp -rf /usr/include/eigen3/Eigen /usr/include/Eigen -R
6 Eigen报错:Eigen::CommaInitializer::finished()
nlopt: /usr/include/eigen3/Eigen/src/Core/CommaInitializer.h:120: XprType& Eigen::CommaInitializer<MatrixType>::finished() [with XprType = Eigen::Matrix<double, 4, 1>]: Assertion `((m_row+m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0) && m_col == m_xpr.cols() && "Too few coefficients passed to comma initializer (operator<<)"' failed.
Aborted (core dumped)
我的输入:
Eigen::Vector4d point1, point2, point3, point4, point5, point6, point7;
point1 << 5.45708, 5.1764, -0.5597;
data_cp.point_array_1.push_back(point1); //右 right
point2 << 5.950488090515, 4.565999984741, -0.558037996292;
point3 << 5.617546558380, 2.353021621704, -0.558990061283;
point4 << 4.072897911072, 1.896781682968, -0.564294576645;
point5 << 3.155550241470, 1.227772474289, -0.567389249802;
point6 << 6.464124202728, -2.563307285309, -0.575628519058;
point7 << 9.175043106079, -2.056616067886, -0.566318452358;
解决办法
重点在报错最后一句 "Too few coefficients passed to comma initializer (operator<<)"' failed
输入参数过少。仔细检查发现定义的是Vector4d,应该有4个参数,但是实际我输入的是一个三维的点,最后一个维度输入1即可。
7 Eigen 使用std::vector <Eigen::Vector4d >编译报错
解决方法:
std::vector<Eigen::Vector4d, Eigen::aligned_allocator<Eigen::Vector4d>> array;
Shell
1. [: missing ]
原语句如下:
if [ ! -d "$foldpath"];then
mkdir $foldpath
else
cd $foldpath
fi
if的[]中括号内判断语句变量前后都要有空格,说以 “filepath”前后得要空格才行。