我正在尝试在Mac OS上运行犰狳,但不断收到相同的错误
I'm trying to run Armadillo on Mac OS but i keep getting the same error
我正在尝试用文本文件中的数据填充矩阵。
这是我的代码
int main() {
ifstream in;
int n=150;
int m=5;
mat coordinates (n,m);
coordinate.zeros();
in.open("test.txt");
for (int i=0; i<n ; i++) {
for (int j=0; i<m ; j++)
in >> coordinates(i,j);
return 0;
}
我用comand编译了它
g++ -I/usr/local/include coordinates.cpp -O2 -larmadillo -llapack -lblas
到目前为止一切似乎都很好,但当我尝试运行程序时,我得到了以下错误
error: Mat::operator(): index out of bounds
libc++abi.dylib: terminating with uncaught exception of type std::logic_error: Mat::operator(): index out of bounds
Abort trap: 6
我想尽一切办法,但都无济于事。你有什么建议吗?
谢谢你抽出时间。
您的代码中有一个错误:第二个循环有i<m
而不是j<m
。
此外,不要将>>
运算符直接用于矩阵,因为您无法检查元素是否被实际读取。相反,您可以使用.load()
成员函数简单地加载整个文件。
在发布基本问题之前,最好先彻底阅读Armadillo文档。
// simple way
mat coordinates;
coordinates.load("test.txt", raw_ascii); // .load() will set the size
coordinates.print();
或
// complicated way
ifstream in;
in.open("test.txt");
unsigned int n=150;
unsigned int m=5;
mat coordinates(n, m, fill::zeros);
for(unsigned int i=0; i<n; ++i)
{
for(unsigned int j=0; j<m; ++j)
{
double val;
in >> val;
if(in.good()) { coordinates(i,j) = val; }
}
}
coordinates.print();
相关文章:
- 项目错误:QT中的未知模块:Mac上的图表
- C++ Mac Xcode 错误:控制可能达到非无效功能的末尾
- Qt Debugger在Mac上使用错误的python版本
- 在 OpenGL 中在 Mac 上编译着色器时出现"version not supported"错误
- 在 Mac OS x 中制造期间出现错误未知类型名称"constexpr"
- 在 mac 上使用 MPFR 包编译错误
- MAC OpenGL 着色器错误:"version '150' is not supported "
- "cin"错误地解析了Mac上的"double"输入
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- OpenGL符号突出显示visual studio Mac代码中的错误
- 从Mac上的终端编译时C 错误
- CMAKE错误:C 11不支持Mac High Sierra
- Mac 上的 node-gyp 构建错误"calling a protected constructor of class 'v8::HandleScope'"
- 如何解决错误:在Mac上令牌"("之前缺少二进制运算符?
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- OpenCV3.2 编译错误 MAC OS X
- QT Quick (QML) assemble mac-.bundle -> 不是使用 macdeployqt 时的动态库错误
- QT 5.12 Mac OS X版本号码错误
- XCode -Mac App -Bootstrap启动时检查错误
- 提升编译错误 Mac OS X 10.7.4