2015-03-10 5 views
2

Мне предлагается скомпилировать эту программу, используя C в октаве. Мне удалось скомпилировать простую программу 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; 
} 

Так можно ли решить эту проблему? Большое спасибо и сожалеем о любых ошибках в сообщении, которое я сделал с тех пор, как я довольно новичок в использовании октавы и на этом форуме.

EDIT (после ответа Alter Манна):

Так что это мой 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)

ответ

1
#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.

+0

Да, я отредактировал код так же, как вы предложили, и я получил сообщение об ошибке, как показано в моем отредактированном сообщении. – IS1001

+0

'Обратите внимание, что octave/oct.h является заголовком C++ и не может быть напрямую # include'ed в исходном файле C или любом другом языке.' –

+0

yes Я удалил октаву/oct.h так же, как и я, и я все еще получают ошибку. Не могли бы вы показать мне пример того, как использовать функцию октавы в исходном файле C? – IS1001

Смежные вопросы