2012-04-07 9 views
0

私はCを学ぼうとしていましたが、私はライブラリを含めて立ち往生しています。私はstrcpy()を使用する必要がありますが、そのメソッドはiostreamライブラリに含まれていますが、ライブラリをインクルードしようとするとプログラムによってエラーが発生します。私は "iostream"、 "iostream.h"、、を使ってみましたが、 "iostream.hを見つけることができません"というエラーが表示されるか、プログラムが100エラーを超えてクラッシュします。私のコードが空であっても、私はまだ同じことをします。コードは次のとおりです。MS Visual C++を使用してCにiostreamを含めることはできませんか?

#include "iostream" 

int main(void) 
{ 
} 

うん、それだけですでにクラッシュしています。そして、ここで(すべてここに貼り付けることができませんでした)私は取得していますエラーの一部です:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2061: syntax error : identifier 'abs' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2059: syntax error : ';' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'acos' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'asin' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan2' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'ceil' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): error C2061: syntax error : identifier 'cos' 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): fatal error C1003: error count exceeds 100; stopping compilation 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

はそうそうそれも100のエラーを超え、プログラムはただカウントを停止します。私はなぜ、私は定期的な図書館だけを含んでいるのか分かりません。 strcpy()に相当するものはありますか?私は主に(練習のために)このようにそれを使用したい:

#include "stdafx.h" 
#include "stdlib.h" 
#include "stdio.h" 
#include "conio.h" 
#include "iostream" 

int main(void) 
{ 
    struct person 
    { 
     int id; 
     char name[50]; 
     int age; 
    }; 

    struct person p1; 

    p1.id = 5595; 
    strcpy(p1.name, "Myname"); 
    p1.age = 18; 

    printf("%d%s%d", p1.id, p1.name, p1.age); 
} 
+0

メモクラッシュとレポートのエラーには違いがあります:]一般的にコンパイラはエラーを報告し、あまりクラッシュすることはありません。 – stijn

答えて

4

<iostream>はC++ヘッダーです(名前が示すように、入力ストリーム/出力ストリームを扱います)。 strcpyが必要な場合は、<string.h>が必要です。

+0

すぐに解決しました。どうもありがとうございました! – ZimZim

+0

@ user1007059 - Oli Charlesworthの投稿に「解決済み」と記入してください;) – paulsm4

+0

@ paulsm4、しようとしていましたが、10分待つように言っていました^^しかし、 – ZimZim

0

iostream SはC++です - のみの機能。 iostreamのヘッダファイルはC言語ではなくC言語で書かれています(C言語ではありません!)おそらく、C言語モードでコンパイラを呼び出すので、コンパイラがヘッダファイルを見ると、 iostreamで使用されている構文の多くはC++モードでのみ意味があるためです。

iostreamを使用する場合は、C++モードでコンパイルし(適切な現代のC++を適切にコーディングする)、Cのみの別のライブラリを使用するか、必要に応じて独自のコードを実装して回避する必要があります。

この場合、明らかにしたいのはstrcpy()です。それはstring.hで宣言され、iostreamではありません。 (string.hはCヘッダーファイルです)ちょうど#include <string.h>とコンパイルする必要があります。

1

ソースファイルが ".c"の場合は、名前を ".cpp"に変更するだけです。

これはC++としてコンパイルされ、C++ヘッダーが作成され、C++ストリームを使用できるようになります。

しかし、iostreamは必要ありません。

Strcpyとフレンドは "<string.h>"です。それを含めるだけで、 "stdio.h"(あなたがやっているように)。 "iostreams" #includeを削除してください。人生はいいはずです。

関連する問題