• ベストアンサー

ifstreamについて

ifstream でファイルを読み込もうとしているのですが、上手くいきません。 ソース A.cpp #include"A.h" void Broker::read_file(ifstream in) { in >> shikin; } int main(void) { ifstream in; in.open("hoge.txt"); Broker b; b.read_file(in); b.show(); } A.h #include<fstream.h> #include<iostream.h> class Broker{ int shikin; int nensu; double nenri; int tesuryo; public: Broker() { shikin = 0; nensu = 0; nenri = 0.0; tesuryo = 0; } void read_file(ifstream in); void show() {cout << shikin << endl;} }; 以上のようなプログラムなのですが、 コンパイルすると エラー E2247 'ios::basic_ios(const ios &)' はアクセスできない(関数 ifstream::basic_ifstream(ifstream &) ) エラー E2125  コンパイラは 'copy constructor' クラスの istream を作成できない(関数 ifstream::basic_ifstream(ifstream &) ) とのエラーが出てきて実行できません。 コンパイラ C++Builder OS XP なにが原因なのかが分かりません。 本当に初心者な質問ですが、よろしくお願いします。 (何か足りない情報などがありましたら、お手数ですがその掲示板にお願いします)

質問者が選んだベストアンサー

  • ベストアンサー
  • KoHal
  • ベストアンサー率60% (110/181)
回答No.2

とりあえずはBroker::read_fileがifstreamの参照を受けるようにすればよいはずです。  void Broker::read_file( ifstream& in ) ただ、kirin3_199さんは勉強にあたり古い本を使われてるようですね。 C++は数年前に大きな改定が行われています。 現行のC++は標準ライブラリの使い方がかなり変更されてるので気をつけてください。 一例をあげると、  旧:#include <fstream.h>  新:#include <fsstream>  旧:void read_file(ifstream& in);  新:void read_file(std::ifstream& in);  この辺は基本を一通り勉強した後で、新しい内容の本で確認してください。  あと、コンストラクタでメンバを初期化するときは普通こうします(これは古いC++でも同じ)。 class Broker { //省略 public:  Broker() : sikin(0), nensu(0), nenri(0.0), tesuryo(0) {} };

kirin3_199
質問者

お礼

丁寧なご解説ありがとうございました。

その他の回答 (1)

  • MASATO3
  • ベストアンサー率60% (27/45)
回答No.1

エラー行が書いていないので推測ですが、 void Broker::read_file(ifstream& in) ではないでしょうか。

関連するQ&A

  • 下記、プログラム内の「char *」の役割

    C++初心者です。 縦長になってしまいますが、 『 #include <iostream.h> void show(int); void show(double); void show(char *);   ←左記の記述の使い方 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char *z) { cout << z << endl; } 』 のプログラムにおいて、「char *」の使い方がいまいち理解できません。 上記プログラムですとエラーが表示されないのですが、下記のプログラムだとエラーが発生します。 『 #include <iostream.h> void show(int); void show(double); void show(char);   //←---------上記と違う行 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char z) {  //←---------上記と違う行 cout << z << endl; } 』 なぜ、ポインタ(*)を付けないといけないのか分かりやすく教えていただけましょうか。

  • 分割コンパイルについて

    現在分割コンパイルが分からずに苦戦しています。 下記のリストは構造体を使わなければコンパイラを通すことができましたが、 使うとなぜか通りません。 あれこれ試しましたがどうしても分かりません。 何がおかしいのでしょうか? *define.hで全てのファイルへの定義や宣言を行わせています。 ////////////// //Main.cpp ////////////// #include <stdio.h> #include <conio.h> #include "define.h" int main( void ){ Tmp[0].c = 15; printf("a: %d\n", a); printf("b: %d\n", b); printf("c: %d\n", Tmp[0].c); printf("NUM:%d\n", NUM); aaa(); bbb(); getch(); return 0; } ////////////////// // A.cpp ///////////////// #include <stdio.h> #include "define.h" void aaa( void ){ printf("a: %d\n", a); printf("b: %d\n", b); printf("c: %d\n", Tmp[0].c); printf("NUM:%d\n", NUM); } ////////////////// // B.cpp ///////////////// #include <stdio.h> #include "define.h" void bbb( void ){ printf("a: %d\n", a); printf("b: %d\n", b); printf("c: %d\n", Tmp[0].c); printf("NUM:%d\n", NUM); } ////////////////// // define.cpp ///////////////// #include "define.h" int a = 10; int b = 20; struct Parameter { int c; }; struct Parameter Tmp[NUM]; ////////////////// // define.h ///////////////// #define NUM 100 extern int a; extern int b; extern struct Parameter Tmp[NUM]; void aaa( void ); void bbb( void );

  • error LNK2001について

    C++ の勉強中です。 error LNK2001問題がありました。問題点も分からないから、 分かった方に教えていただけないでしょうか? ソースも添付します。環境はVS2005 express edition まず、ヘッダファイルです。ファイル名は、myheader.h #ifndef __MYHEADER_H__ #define __MYHEADER_H__ #define includeshow(x) \ { \ s.show(x); \ } class Sample{ public: void show(char * parameter); }; extern Sample s; #endif //------------------------------------------------ 次に、Sample.cppです。 #include "myheader.h" #include <stdio.h> void Sample::show(char *parameter){ printf("in show func"); } //----------------------------------------------- 最後にTest.cppです。 #include "myheader.h" #include <stdio.h> int main(){ includeshow("ppppp"); } これで error LNK2001: 外部シンボル ""class Sample s" (?s@@3VSample@@A)" は未解決です。というエラーが出ています。 よろしくおねがいします。

  • 関数ポインタにvirtual関数を与えたいです

    //関数ポインタについて質問させてください。 //error C2440: '=' : 'void (__thiscall A::* )(int,int)' から 'void (__cdecl *)(int,int)' に変換できません。 //というエラーが出ます。 //どうすればいいのか教えてください。 //よろしくお願いします。 #include<stdio.h> class A { public: void (*aaa)(int a,int b); virtual void test(int a,int b)=0; virtual void test2(int a,int b)=0; void execute() { aaa=test;//error C2440 } }; class B : public A { public: void test(int a,int b) { printf("test"); } void test2(int a,int b) { printf("test2"); } }; int main() { B b; b.execute(); b.aaa(2,3); return 0; }

  • このプログラムを見てください(C言語) m( _ _ )m

    現在乱数を使ったプログラムを作っています。 下のプログラムでコンパイルではエラーがでませんが、実行するとデバッグエラーが出てきます。コンパイラにはbccを使っています。恐らく原因は別の関数でbに値を代入してそれをaに代入しているからだろうと思いますが、なぜこれがいけないのかが分かりません。 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> int ran, a=1, b, c; void test(void){ b=8; } void main(){ srand(time(NULL)); a=b;//ここを無くすとなぜかエラーが出ない。 while(1){ ran=rand()%(a*2)+1; switch(ran){ case 1: c=1; break; case 2: c=1; break; case 3: c=1; break; case 4: c=2; break; case 5: c=2; break; case 6: c=3; break; case 7: c=3; break; case 8: c=4; break; } printf("%d",c);printf("何か押してください\n");getch(); } }

  • 条件演算子論理ORについて

    すみません、独学でC言語を学んでいるものです。 どうかお助けいただけませんか? テキストに載っているものをそのままコンパイラしているのに エラーがでました。どこがいけないのか確認していただけませんか? *Memopad.cppで作ったプログラム* #include<stdio.h> void main(void) { int a,b,c; a=0; b=0; c=0; if((a=1)||(b=2)||(c=3)) { printf("%d\n",a); printf("%d\n",b); printf("%d\n",c); } } *エラーメッセージ* 添付を参照下さい。 うまくいくと 1 0 0 と表示されるようです。ずっと考えてもわからなかったのでお力を貸して頂ければ助かります。 よろしくお願いします。

  • C言語で乱数がうまく効きません。下記のソースでコンパイルして実行すると

    C言語で乱数がうまく効きません。下記のソースでコンパイルして実行すると結果が130固定となってしまいます。 #include <stdio.h> #include <stdlib.h> int main(void){ int x; x = rand(); printf("%d",x); return 0; } 使っているコンパイラはBorland C++ 5.5.1です。何か間違いでもあるのでしょうか?よろしくお願いします。

  • iostreamの挙動について

    //save.h class c_save{  ofstream ostrm;  ifstream istrm; public:  void read(int *point);  void write(int *point); }; //save.cpp void c_save::read(int *point){  istrm.open("savedata.sav",ios::in | ios::binary);  if(!istrm){   MessageBox(NULL,L"セーブデータが読み込めませんでした。\n新規セーブデータを作成します。",L"セーブデータの作成",MB_OK);   ostrm.open("savedata.sav",ios::out | ios::trunc | ios::binary);   if(!ostrm){    MessageBox(NULL,L"新規セーブデータの作成に失敗しました。",L"セーブデータの作成",MB_OK);   }   else{    *point = 0;    ostrm.write((char *)point,sizeof(int));   }   ostrm.close();  }  else{  istrm.read((char *)point,sizeof(int));  }  istrm.close(); } void c_save::write(int *point){  ostrm.open("savedata.sav",ios::out | ios::trunc | ios::binary);  if(!ostrm){   MessageBox(NULL,L"セーブデータの書き込みに失敗しました。",L"セーブデータの書き込み",MB_OK);  }  else{   ostrm.write((char *)point,sizeof(int));  }  ostrm.close(); } ゲーム用に、ポイントを記録するために上記のようなクラスを作成したのですが、read関数を呼び出し、その後write関数を呼び出し、その次にまたread関数を呼び出すと、read関数で2回ともセーブデータが読み込めませんというメッセージがでてしまいます。 一度目のread関数でsavedata.savは作成されているのにもかかわらず、二度目でも読み込めないのはなぜでしょうか? ちなみに、アプリケーションを再起動させるとsavedata.savは認識されます。

  • borlandC++で膨大データの入出力ができない

    borlandC++を試してますが、ifstream等を利用してファイル入出力を試してますが、プロンプトを用いて、入力データが多すぎるとコンパイルは通りますが実行exeが拒否反応を起こし処理が止まってしまいます。 データが7万行以上ぐらいに達すると拒否反応を起こします。 自分がやろうとしてるのはそれを遥かに凌ぐ何百万行のデータの入出力処理を行いたいのですがこれでは不可能です。 どうして膨大データの入出力ができないのでしょうか? 自分の試したファイル入出力のプログラムソースと問題の読み込んだデータの内容も下に貼り付けます。 またソース中の処理で上記とは別のエラーを起こしたものがあり、入力データを一行毎に格納する配列 dag[b]  をprintfで表示させようと以下の様に記述しましたが printf("[%d] %s\n", b,dag[b]); もデータが7行以上になるだけでエラーを起こして止まります。 どうか膨大データの入出力とprintfのエラーの解決方法について教えて下さい。 宜しくお願いします。 #include <iostream.h> #include <fstream> main() { string dag[100000]; string LineData; int b=0; ifstream in("rrs.txt", ios::in); if(!in){ printf("エラー\n"); exit(1); } while(!in.eof()){ printf("データ\n"); in >> dag[b]; printf("[%d] %s\n", b,dag[b]); b++; } in.close(); ofstream out; out.open ("WRT.txt"); for(int i=0;i < b;i++)out << dag[i] << endl; out.close(); return 0; } #読み込んだデータ内容 2011/12/09,16:00:00,77.48,77.50,77.48,77.50 2011/12/11,17:00:00,77.66,77.66,77.54,77.61 2011/12/11,18:00:00,77.61,77.61,77.58,77.59 2011/12/11,19:00:00,77.59,77.64,77.59,77.63 2011/12/11,20:00:00,77.63,77.65,77.61,77.63 2011/12/11,21:00:00,77.63,77.63,77.58,77.60 2011/12/11,22:00:00,77.60,77.62,77.60,77.60 2011/12/11,23:00:00,77.61,77.64,77.60,77.64 2011/12/12,00:00:00,77.64,77.64,77.61,77.63 2011/12/12,01:00:00,77.64,77.64,77.56,77.58 2011/12/12,02:00:00,77.58,77.63,77.57,77.59 2011/12/12,03:00:00,77.59,77.69,77.58,77.66 2011/12/12,04:00:00,77.67,77.84,77.67,77.83 2011/12/12,05:00:00,77.82,77.90,77.74,77.80 2011/12/12,06:00:00,77.80,77.84,77.75,77.81 2011/12/12,07:00:00,77.81,77.82,77.75,77.78 2011/12/12,08:00:00,77.78,77.97,77.76,77.97 2011/12/12,09:00:00,77.97,77.97,77.83,77.91 2011/12/12,10:00:00,77.90,77.93,77.76,77.80 2011/12/12,11:00:00,77.79,77.86,77.72,77.86 2011/12/12,12:00:00,77.86,77.88,77.83,77.84 2011/12/12,13:00:00,77.84,77.89,77.83,77.89

  • cではよくてc++ではダメな理由

    #include <stdio.h> #include <stdlib.h> int main(void) { int *x; x = calloc(1, sizeof(int)); if (x == NULL) puts("記憶域の確保に失敗しました。"); else{ *x = 57; printf("*x = %d\n", *x); free(x); } return 0; } 上のソースなのですがcallocの戻り値がvoidなのでintにはキャストしてくれみたいなことを コンパイラに言われます。 cではコンパイルできてc++ではコンパイルできないのはなぜでしょう。

専門家に質問してみよう