c++ - 在 C 中使用 octave 内置函数

标签 c++ c octave built-in

我被告知要在 Octave 音程中使用 C 来编译这个程序。在使用 Octave 之前,我已经成功地编译了一个简单的 C 程序,但是对于这个,我已经尝试过,但我不知道如何用 C 编译它,尽管我已经尝试将一些 C++ 语言更改为 C 语言。

 #include <iostream>

 #include <octave/oct.h>

 int main (void)

 {
 std::cout << "Hello Octave world!\n";

 int n = 2;

 Matrix a_matrix = Matrix (n, n);

 for (octave_idx_type i = 0; i < n; i++)

 for (octave_idx_type j = 0; j < n; j++)

 a_matrix(i,j) = (i + 1) * 10 + (j + 1);

 std::cout << a_matrix;

 return 0;
 }

那么这个问题有什么解决办法吗? 非常感谢您,对于我在帖子中所犯的任何错误,我深表歉意,因为我在使用 Octave 和在这个论坛上还是个新手。

编辑(在 Alter Mann 的回答之后):

这是我的C程序代码

#include <stdio.h>

int main(void)
{
        printf("Hello Octave World!");

    int n=2;
    Matrix a_matrix = Matrix (n, n);

    for (octave_idx_type i=0; i<n; i++)
    {
            for (octave_idx_type j=0; j<n; j++)
            {
                    a_matrix(i,j) = (i+1) * 10 + (j+1);
            }

            printf ("%d", &a_matrix);
    }
    return 0;
}

但是我得到了这个错误

standalone.c: In function ‘main’:
standalone.c:8: error: ‘Matrix’ undeclared (first use in this function)
standalone.c:8: error: (Each undeclared identifier is reported only once
standalone.c:8: error: for each function it appears in.)
standalone.c:8: error: expected ‘;’ before ‘a_matrix’
standalone.c:10: error: ‘octave_idx_type’ undeclared (first use in this function)
standalone.c:10: error: expected ‘;’ before ‘i’
standalone.c:10: error: ‘i’ undeclared (first use in this function)
standalone.c:12: error: expected ‘;’ before ‘j’
standalone.c:12: error: ‘j’ undeclared (first use in this function)
standalone.c:14: warning: implicit declaration of function ‘a_matrix’
standalone.c:17: error: ‘a_matrix’ undeclared (first use in this function)

最佳答案

#include <iostream>

应该是

#include <stdio.h>

std::cout << a_matrix;

应该是

printf("%d", a_matrix);

但是<octave/oct.h>不能用于 C:

A.1.1 Getting Started with Oct-Files

The first critical line is #include which makes available most of the definitions necessary for a C++ oct-file. Note that octave/oct.h is a C++ header and cannot be directly #include’ed in a C source file, nor any other language.

Alternative :

The interface is centered around supporting the languages C++, C, and Fortran. Octave itself is written in C++ and can call external C++/C code through its native oct-file interface. The C language is also supported through the mex-file interface for compatibility with MATLAB. Fortran code is easiest to reach through the oct-file interface.

关于c++ - 在 C 中使用 octave 内置函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28959344/

相关文章:

c++ - 优化找到特定斐波那契数的算法

c - C 中的输入字符串数组内存管理

c - 从 C 二叉树中删除节点而不弄乱它

ffmpeg - 每次我尝试运行代码时,AVI INFO 都会使 Octave GUI 崩溃

matlab - 从 Octave 中的其他脚本文件运行脚本文件

C++ 重载方法与虚拟继承的绑定(bind)

c++ - QDial 将范围从 0.00 到 10.00 设置为小部件显示

c++ - 已连接串口C列表Qt

c - 用链表筛选素数

CentOS 7 安装带有 GUI 的 Octave 4.0.2(OpenGL 支持)