iomanipのincludeでエラーが出る

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
あるむ

iomanipのincludeでエラーが出る

#1

投稿記事 by あるむ » 9年前

以下のプログラムを書いたのですがエラーがでてしまうので質問します。

コード:

#include <iostream>
#include <iomanip.h>
#include <math.h>

const int  N=3; /* 変数の数 */
const double tol=1e-10; //収束を判定する小さい数
const int itr=100; //最大の反復回数

/* ユーザー定義の型Matrixの定義 */
typedef double Matrix[N+1][N+1];

/* ユーザー定義の型Vectorの定義 */
typedef double Vector[N+1];

//反復法で収束する十文条件を用いた判定
int hantei(Matrix a, int n){
    Vector m;

    for ( int i=1; i<=n ; i++ ){
        m[i]=0.0;
        for ( int j=1; j<=n; j++ )
            if ( i != j )
                m[i] += fabs(a[i][j]);
        m[i] /= fabs(a[i][i]);
    }

    double max=m[1];

    for ( int i=2;i<=n; i++ )
        if ( max < m[i] )
            max = m[i];
    if ( max < 1 )
        return 1;
    else
        return 0;
}

int syusoku(Vector x, Vector xo, int n){
    int i=1;
    while ( i<=n && fabs(x[i] - xo[i] ) < tol){
        i++;
    }
    return ((i>n ) ? 1 : 0 );
}

int main( void ){
    Matrix a;
    Vector b,x,xo;
    int i,j;

    cerr << "変数の数は" << N << "です\n";

    for (i=1;i<=N;i++)  //係数行列の読み込み
        for (j=1;j<=N;j++) cin >> a[i][j];
    
    for (i=1;i<=N;i++) cin >> b[i]; //定数ベクトルの読み込み   

    for (i=1;i<=N;i++) x[i]=0.0;  //初期値を0に設定

    if ( hantei(a,N)==0 ) cerr << "収束は否定的です\n"; //計算可能かどうかの十文条件を判定

    int k=0;
    do{
        for (i=1; i<=N; i++) xo[i] = x[i]; // xoにxをコピー
        for (i=1; i<=N; i++) {
            x[i]=b[i];
            for( j=1; j<=N; j++)
                if ( j != i ) x[i] -= a[i][j]*x[j];

            x[i] /= a[i][i];
        }
        k++;
    }while( k< itr && syusoku(x,xo,N)==0 );

    if ( k < itr ){
        cout << "loop:" << k << "回\n";
        /* 解の表示 */
        for (i=1;i<=N;i++)
            cout << "x_" << i << "=" << setw(8) << x[i] << "\n";
    }
    else{
        cout << "収束しませんでした。\n";
    }

    return 0;
}
コンパイルしようとすると、以下の様なエラー文が出てきてしまうのですが何故でしょうか。。。orz

コード:

Gauss_Seidel.cpp:4:10: fatal error: 'iomanip.h' file not found
#include <iomanip.h>
         ^
1 error generated.

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: iomanipのincludeでエラーが出る

#2

投稿記事 by h2so5 » 9年前

<iomanip.h>ではなくて<iomanip>です。

あるむ

Re: iomanipのincludeでエラーが出る

#3

投稿記事 by あるむ » 9年前

そうすると以下の様なエラーが出るのですが、これは何故なのでしょうか…?

コード:

Gauss_Seidel.cpp:53:9: error: use of undeclared identifier 'cerr'; did you mean 'std::cerr'?
        cerr << "変数の数は" << N << "です\n";
        ^~~~
        std::cerr
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:51:33: note: 
      'std::cerr' declared here
extern _LIBCPP_FUNC_VIS ostream cerr;
                                ^
Gauss_Seidel.cpp:57:25: error: use of undeclared identifier 'cin'; did you mean 'sin'?
                        cin >> a[i][j];
                        ^~~
                        sin
/usr/include/math.h:343:15: note: 'sin' declared here
extern double sin(double);
              ^
Gauss_Seidel.cpp:57:29: error: invalid operands to binary expression ('double (*)(double)' and
      'double')
                        cin >> a[i][j];
                        ~~~ ^  ~~~~~~~
Gauss_Seidel.cpp:59:17: error: use of undeclared identifier 'cin'; did you mean 'sin'?
                cin >> b[i];
                ^~~
                sin
/usr/include/math.h:343:15: note: 'sin' declared here
extern double sin(double);
              ^
Gauss_Seidel.cpp:59:21: error: invalid operands to binary expression ('double (*)(double)' and
      'double')
                cin >> b[i];
                ~~~ ^  ~~~~
Gauss_Seidel.cpp:65:17: error: use of undeclared identifier 'cerr'; did you mean 'std::cerr'?
                cerr << "収束は否定的です\n";
                ^~~~
                std::cerr
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:51:33: note: 
      'std::cerr' declared here
extern _LIBCPP_FUNC_VIS ostream cerr;
                                ^
Gauss_Seidel.cpp:84:17: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
                cout << "loop:" << k << "回\n";
                ^~~~
                std::cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:50:33: note: 
      'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
                                ^
Gauss_Seidel.cpp:87:25: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
                        cout << "x_" << i << "=" << setw(8) << x[i] << "\n";
                        ^~~~
                        std::cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:50:33: note: 
      'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
                                ^
Gauss_Seidel.cpp:87:53: error: use of undeclared identifier 'setw'; did you mean 'getw'?
                        cout << "x_" << i << "=" << setw(8) << x[i] << "\n";
                                                    ^~~~
                                                    getw
/usr/include/stdio.h:382:6: note: 'getw' declared here
int      getw(FILE *);
         ^
Gauss_Seidel.cpp:87:58: error: cannot initialize a parameter of type 'FILE *' (aka '__sFILE *') with
      an rvalue of type 'int'
                        cout << "x_" << i << "=" << setw(8) << x[i] << "\n";
                                                         ^
/usr/include/stdio.h:382:17: note: passing argument to parameter here
int      getw(FILE *);
                    ^
Gauss_Seidel.cpp:91:17: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
                cout << "収束しませんでした。\n";
                ^~~~
                std::cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:50:33: note: 
      'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
                                ^
11 errors generated.

sasu
記事: 7
登録日時: 9年前
住所: 愛媛

Re: iomanipのincludeでエラーが出る

#4

投稿記事 by sasu » 9年前

using namespace std;
を上のほうに書いて直りませんか?

あるむ

Re: iomanipのincludeでエラーが出る

#5

投稿記事 by あるむ » 9年前

なおりました!ありがとうございます><!

閉鎖

“C言語何でも質問掲示板” へ戻る