ダイアログ上のボタンをクリックすると指定されたテキストファイルをオープンし、
ファイルの内容をスペースで区切って格納するプログラムを
http://oshiete1.goo.ne.jp/kotaeru.php3?q=474452
こちらを少し変えて作ったのですが、
コンパイル・デバッグを行なうと
'std' : 識別子がクラス名でも名前空間名でもありません。
'vector' : 定義されていない識別子です。
'ofstream' : 定義されていない識別子です。
と言ったようなエラーになってしまいます。
必要なヘッダーファイルはインクルードされているはずなのですが・・・。
この原因は何故でしょうか?
/**********プログラムソース**********/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
#include "stdafx.h"
#include "SCHEDULE.h"
#include "SCHEDULEDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// ~中略~
void CSCHEDULEDlg::OnReq()
{
typedef std::pair<std::string,std::string> item;
std::vector<item> participation;
std::ofstream file(dataFile.txt);
std::string line;
while ( std::getline(file,line) ) {
std::string::size_type pos = line.find(' ');
participation.push_back(item(line.substr(0,pos), line.substr(pos+1)));
}
for ( int i = 0; i < participation.size(); ++ i) {
std::cout << "result = " << participation[j] << "]\n";
}
}
No.4ベストアンサー
- 回答日時:
> error C2679: 二項演算子 '<<' : 型 'struct std::pair<class
> ...
> の右オペランドを扱う演算子は定義されていません。(または変換できません)
streamに対して operator<< が定義されていないからです。
型 XXX をstreamに出力するには:
std::ostream& operator(std::ostream& stream, const XXX& data)
を定義しなければなりません。
ご回答ありがとうございます。
申し訳ありません。
良く考えたのですが、理解には至りませんでした。
型 XXX とは何を示すのでしょうか。
エラー箇所のparticipation[i]の型かと考えたのですが、
std::vector<item> participation;
にて定義されているはずですし。(勘違いでしたら恥ずかしいですが)
No.9
- 回答日時:
> 標準出力の定義についても教えて頂けないでしょうか。
厳密に答えるのはご勘弁を。
コンソール・アプリケーションにおけるスクリーン(画面)が標準出力(cout), キーボードが標準入力(cin)に割り当てられており、パイプによって割り当てを変更することができます。
# 「パイプって何?」と訊かないこと。きりがないから。
# そうでなくてもとっくに質問の表題とはかけ離れていますし。
ご回答ありがとうございました。
定義については、理解には至りませんでしたが頭に置いておきたいと思います。
また、質問が表題から離れてしまった事は申し訳ありませんでした。
まとめてとなってしまいますが、
ご回答下さった皆様に改めてお礼申し上げます。
ありがとうございました。
No.7
- 回答日時:
> が、ダイアログのボタンをクリックしても結果が表示されません。
どこに表示するつもりですか?
標準出力(cout)はどこにもないんじゃありませんか?
ご回答ありがとうございます。(前回は抜けてしまい申し訳ありません)
あくまで確認なので表示はどこでも良いのですが、
コマンドプロンプトに表示しようと考えています。
出力は
for ( int i = 0; i < participation.size(); ++ i) {
std::cout << "result = " << participation[i] << "]\n";
}
てっきりこの部分で行なわれるものと思っていました。
しかし、coutを使用しても定義されていないと出ますし・・・。
coutは
#include <iostream>
で定義されているのではないでしょうか。
No.6
- 回答日時:
> また、
> void CSCHEDULEDlg::OnReq() {
> …
> std::ostream& operator<<(std::ostream& stream, const item& data);
> …
それは"定義"ではありません。 単に"宣言"してるだけ。
#include <iostream>
#include <vector>
#include <utility>
#include <string>
// ↓この"定義"を追加。
std::ostream& operator<<(std::ostream& stream, const std::pair<std::string,std::string>& data) {
return stream << data.first << ' ' << data.second;
}
int main() {
typedef std::pair<std::string,std::string> item;
std::vector<item> participation;
participation.push_back(item("apple","りんご"));
participation.push_back(item("orange","みかん"));
participation.push_back(item("peach","もも"));
for ( int i = 0; i < participation.size(); ++i ) {
std::cout << "result = " << participation[i] << "]\n";
}
return 0;
}
この回答への補足
補足です。
dataFile.txtの内容は
*****************************
あ い
う え
お か
き く
け こ
*****************************
となっています。
なるほど・・・
おっしゃる通りに定義をしてみました。
警告は未だに出るのですが、エラーは無くなって
コンパイル・デバッグも通り実行ができました。
が、ダイアログのボタンをクリックしても結果が表示されません。
(というより、何も動作が起こらない)
現在プログラムは
#include "stdafx.h"
#include "SCHEDULE.h"
#include "SCHEDULEDlg.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
std::ostream& operator<<(std::ostream& stream, const std::pair<std::string,std::string>& data) {
return stream << data.first << ' ' << data.second;
}
/****中略****/
void CSCHEDULEDlg::OnReq()
{
typedef std::pair<std::string,std::string> item;
std::vector<item> participation;
std::ifstream file("dataFile.txt");
std::string line;
while ( std::getline(file,line) ) {
std::string::size_type pos = line.find(' ');
participation.push_back(item(line.substr(0,pos), line.substr(pos+1)));
}
for ( int i = 0; i < participation.size(); ++ i) {
std::cout << "result = " << participation[i] << "]\n";
}
}
となっています。
まだおかしいところがあるのでしょうか・・・。
No.5
- 回答日時:
> エラー箇所のparticipation[i]の型かと考えたのですが、
> std::vector<item> participation;
> にて定義されているはずですし。(勘違いでしたら恥ずかしいですが)
違います。今のままでは:
item message("hello","world");
std::cout << message;
のコンパイルに失敗するはずです。
std::ostream& operator<<(std::ostream& stream, const item& data)
が定義されていませんから。
ご回答ありがとうございます。
C++の本と合わせて理解しようとしてみました。
入力するために定義したitem型の動的配列participationを
出力に利用するために
std::ostream& operator<<(std::ostream& stream, const item& data)
を定義する。という事で良いのでしょうか。
また、
void CSCHEDULEDlg::OnReq()
{
typedef std::pair<std::string,std::string> item;
std::vector<item> participation;
std::ostream& operator<<(std::ostream& stream, const item& data);
~略~
という具合に付け加えてコンパイル・デバックしてみたところ、
warning C4786: 'std::vector<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > >' :
識別子が '255' 文字に切り捨てられました (debug 情報内)。
というような警告が関数の最後の}に22個。
また、エラー箇所が不明なのですが、
CHEDULEDlg.obj : error LNK2001: 外部シンボル
""class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,structstd::char_traits<char> > &,struct
std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > const &)"
(??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@
AAV01@ABU?$pair@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@V12@@1@@Z)" は未解決です
Debug/SCHEDULE.exe : fatal error LNK1120: 外部参照 1 が未解決です。
link.exe の実行エラー
以上のような2個のエラーが出てしまいました。
何度も質問をして本当に申し訳ないのですが、
教えていただけないでしょうか。
よろしくお願い致します。
No.3
- 回答日時:
#include "stdafx.h"
#include "SCHDULE.h"
#include "SCHDULEDlg.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 中略
void CSCHDULEDlg::OnReq()
{
typedef std::pair<std::string,std::string> item;
std::vector<item> participation;
std::ifstream file("dataFile.txt");
std::string line;
while ( std::getline(file,line) ) {
std::string::size_type pos = line.find(' ');
participation.push_back(item(line.substr(0,pos), line.substr(pos+1)));
}
for ( int i = 0; i < participation.size(); ++ i) {
std::cout << "result = " << participation[i].first << participation[i].second << "]\n";
}
}
と修正するとコンパイルは通るようになるみたいです。
後は何がしたいのかわからないのでちょっと。
ご回答ありがとうございました。
/****dataFile.txt****/
あ い あ あ い
い い い い あ
/****dataFile.txt****/
というファイルからファイルの内容を読み込み、
行ごとに文字の出現数をカウントしたいのです。
文字がきちんと格納されているかを確認するために
一先ず出力して確認しようと考えていました。
No.2
- 回答日時:
std は、ネームスペース識別子(正確な表現ではないかもしれません)です。
プログラムの始まる前に、
using namespace std;
の一行を書いておいたらどうでしょう。
ご回答ありがとうございました。
初めstdが定義されないと出たので、
必要かと思い付け加えてみたのですが、
それでもエラーが無くなりませんでした。
私が調べた限りでは
using namespace std;
は必要無いようなのです。
http://www.ksky.ne.jp/~seahorse/cpp/#portable
こちらにその事が書いてありました。
No.1
- 回答日時:
stdafx.hがあるなら、これを最初にインクルードする必要があるんじゃないかな。
ご回答ありがとうございました。
おっしゃる通り、ヘッダーファイル定義の順番を変え、
#include "stdafx.h"
#include "SCHEDULE.h"
#include "SCHEDULEDlg.h"
を先に持ってきたところ、エラーが減りました。
インクルードする順番も考慮が必要なのですね。
ただ、新たなエラーも出てしまいました。
エラー箇所は質問にあるfor文内のstd::coutの部分なのですが、エラーが
SCHEDULE\SCHEDULEDlg.cpp(198):
error C2679: 二項演算子 '<<' : 型 'struct std::pair<class std::basic_string
<char,struct std::char_traits<char>,class std::allocator
<char>>,class std::basic_string
<char,struct std::char_traits<char>,class std::allocator<char> > >'
の右オペランドを扱う演算子は定義されていません。(または変換できません)
(新しい動作; ヘルプを参照)
となっており、理解が難しいです。
続けての質問となってしまいますが、これは何のエラーでしょうか?
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# C++初心者です stirng 2 2022/09/20 20:43
- C言語・C++・C# このプログラミング誰か教えてください 9 2022/04/22 18:50
- C言語・C++・C# C++プログラミングコードにポリモーフィズムを取り入れ方を教えてください。 2 2023/06/09 11:17
- C言語・C++・C# メインプログラムに#include <algorithm>を書いて、 そのメインプログラムが // 3 2023/05/02 11:24
- C言語・C++・C# このプログラミング誰か教えてください。 2 2022/04/22 18:48
- C言語・C++・C# このプログラミング誰か教えてくれませんか 2 2022/05/14 09:45
- C言語・C++・C# このプログラミング誰か教えてくれませんか 3 2022/05/13 17:27
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- C言語・C++・C# プログラミングの授業の課題です 1 2023/01/17 22:15
- C言語・C++・C# C++のcinの動作 5 2023/02/26 00:13
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
switch文のエラーについて
-
VC++で文字列から任意の文字を...
-
#include "fstream.h"
-
VHDLのvector
-
wstringの主力
-
セグメンテーションフォルトが...
-
JPEGやPNGが読めるLoadImage関数
-
c++のvirtual関数および継承に...
-
構文エラーが出ているのですが...
-
リモートデスクトップの接続元I...
-
C言語のエラーを修正したい
-
C言語プログラミングにて、関数...
-
C++文字配列ソート関数を教えて...
-
string型からint型への型変換
-
構造体配列のvectorへの変換と...
-
std::wstringのメモリリークに...
-
なぜ、C++の標準ヘッダをインク...
-
C言語からgnuplot呼び出し
-
TeXで文書作成しているのですが、
-
【C++】ヘッダ内でstringを格納...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
VC++で文字列から任意の文字を...
-
構文エラーが出ているのですが...
-
enumの値から定義名を文字列化...
-
switch文のエラーについて
-
空ENTERの判別
-
なぜ、C++の標準ヘッダをインク...
-
【C++】ヘッダ内でstringを格納...
-
C++での <iostream.h>と<iostre...
-
#include "fstream.h"
-
#define中の#のエスケープ
-
JPEGやPNGが読めるLoadImage関数
-
リモートデスクトップの接続元I...
-
string型のフォーマット書式指...
-
CStringとString
-
std::map の const 修飾について
-
VHDLのsignedとunsignedの違いは?
-
このプログラミング誰か教えて...
-
指定した文字を削除したい
-
構造体配列のvectorへの変換と...
-
C言語のポインターで詰まっている
おすすめ情報