代码之家  ›  专栏  ›  技术社区  ›  IS1001

使用C中的八度内置函数

  •  3
  • IS1001  · 技术社区  · 10 年前

    我被告知要用八度音阶的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;
     }
    

    那么这个问题有什么解决办法吗? 非常感谢你,并为我在帖子中犯的任何错误感到抱歉,因为我在使用八度音阶和在这个论坛上都很新。

    编辑(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:在函数main中:
    standalone.c:8:error:矩阵未声明(此函数中首次使用)
    standalone.c:8:error:(每个未声明的标识符只报告一次
    standalone.c:8:error:对于其中出现的每个函数。)
    standalone.c:8:错误:应为;在a_矩阵之前
    standalone.c:10:error:未声明octave_idx_type(此函数中首次使用)
    standalone.c:10:错误:应为;在我之前
    standalone.c:10:error:i未声明(此函数中首次使用)
    standalone.c:12:错误:应为;在j之前
    standalone.c:12:error:j未声明(此函数中首次使用)
    standalone.c:14:警告:函数amatrix的隐式声明
    standalone.c:17:错误:未声明的矩阵(此函数中首次使用)

    1 回复  |  直到 10 年前
        1
  •  2
  •   David Ranieri    10 年前
    #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

    第一条关键线是#include 提供C++oct文件所需的大多数定义。笔记 八度/八度h是C++标头,不能直接#included 在C源文件或任何其他语言中。

    Alternative :

    该接口以支持C++、C和 Fortran语言。倍频程本身是用C++编写的,可以调用外部C++/C 通过其原生oct文件接口编写代码。C语言也是 通过mex文件接口支持,以与 MATLAB。Fortran代码最容易通过oct文件访问 界面