アプリ版:「スタンプのみでお礼する」機能のリリースについて

ダイアログ上のボタンをクリックすると指定されたテキストファイルをオープンし、
ファイルの内容をスペースで区切って格納するプログラムを
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";
   }
}

A 回答 (9件)

> error C2679: 二項演算子 '<<' : 型 'struct std::pair<class


> ...
> の右オペランドを扱う演算子は定義されていません。(または変換できません)

streamに対して operator<< が定義されていないからです。
型 XXX をstreamに出力するには:

std::ostream& operator(std::ostream& stream, const XXX& data)
を定義しなければなりません。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

申し訳ありません。
良く考えたのですが、理解には至りませんでした。

型 XXX とは何を示すのでしょうか。
エラー箇所のparticipation[i]の型かと考えたのですが、
std::vector<item> participation;
にて定義されているはずですし。(勘違いでしたら恥ずかしいですが)

お礼日時:2006/01/07 00:23

> 標準出力の定義についても教えて頂けないでしょうか。



厳密に答えるのはご勘弁を。
コンソール・アプリケーションにおけるスクリーン(画面)が標準出力(cout), キーボードが標準入力(cin)に割り当てられており、パイプによって割り当てを変更することができます。
# 「パイプって何?」と訊かないこと。きりがないから。
# そうでなくてもとっくに質問の表題とはかけ離れていますし。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。

定義については、理解には至りませんでしたが頭に置いておきたいと思います。
また、質問が表題から離れてしまった事は申し訳ありませんでした。

まとめてとなってしまいますが、
ご回答下さった皆様に改めてお礼申し上げます。
ありがとうございました。

お礼日時:2006/01/09 22:18

> coutは


> #include <iostream>
> で定義されているのではないでしょうか。

ヘッダにあるのは宣言です。定義ではありません。

> あくまで確認なので表示はどこでも良いのですが

ならファイルに出力しては?
    • good
    • 0
この回答へのお礼

なるほど、ファイルに出力なら本を読んでできそうなのでそうします。

既に何度も質問に答えて頂いており、申し訳ないですが、
標準出力の定義についても教えて頂けないでしょうか。
今後の為に知っておきたいのです。
よろしくお願い致します。

お礼日時:2006/01/08 22:52

> が、ダイアログのボタンをクリックしても結果が表示されません。



どこに表示するつもりですか?
標準出力(cout)はどこにもないんじゃありませんか?
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。(前回は抜けてしまい申し訳ありません)

あくまで確認なので表示はどこでも良いのですが、
コマンドプロンプトに表示しようと考えています。

出力は

 for ( int i = 0; i < participation.size(); ++ i) {
  std::cout << "result = " << participation[i] << "]\n";
 }

てっきりこの部分で行なわれるものと思っていました。

しかし、coutを使用しても定義されていないと出ますし・・・。
coutは
#include <iostream>
で定義されているのではないでしょうか。

お礼日時:2006/01/08 19:39

> また、


> 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の内容は
*****************************
あ い
う え
お か
き く
け こ
*****************************
となっています。

補足日時:2006/01/08 16:31
    • good
    • 0
この回答へのお礼

なるほど・・・
おっしゃる通りに定義をしてみました。

警告は未だに出るのですが、エラーは無くなって
コンパイル・デバッグも通り実行ができました。
が、ダイアログのボタンをクリックしても結果が表示されません。
(というより、何も動作が起こらない)

現在プログラムは

#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";
}
}

となっています。
まだおかしいところがあるのでしょうか・・・。

お礼日時:2006/01/08 16:29

> エラー箇所のparticipation[i]の型かと考えたのですが、


> std::vector<item> participation;
> にて定義されているはずですし。(勘違いでしたら恥ずかしいですが)

違います。今のままでは:
item message("hello","world");
std::cout << message;
のコンパイルに失敗するはずです。

std::ostream& operator<<(std::ostream& stream, const item& data)
が定義されていませんから。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

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個のエラーが出てしまいました。
何度も質問をして本当に申し訳ないのですが、
教えていただけないでしょうか。
よろしくお願い致します。

お礼日時:2006/01/07 23:45

#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";
}
}
と修正するとコンパイルは通るようになるみたいです。
後は何がしたいのかわからないのでちょっと。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。

/****dataFile.txt****/
あ い あ あ い
い い い い あ
/****dataFile.txt****/
というファイルからファイルの内容を読み込み、
行ごとに文字の出現数をカウントしたいのです。

文字がきちんと格納されているかを確認するために
一先ず出力して確認しようと考えていました。

お礼日時:2006/01/07 00:11

std は、ネームスペース識別子(正確な表現ではないかもしれません)です。


プログラムの始まる前に、

using namespace std;

の一行を書いておいたらどうでしょう。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。

初めstdが定義されないと出たので、
必要かと思い付け加えてみたのですが、
それでもエラーが無くなりませんでした。

私が調べた限りでは
using namespace std;
は必要無いようなのです。
http://www.ksky.ne.jp/~seahorse/cpp/#portable
こちらにその事が書いてありました。

お礼日時:2006/01/06 17:46

stdafx.hがあるなら、これを最初にインクルードする必要があるんじゃないかな。

    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。

おっしゃる通り、ヘッダーファイル定義の順番を変え、
#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> > >'
の右オペランドを扱う演算子は定義されていません。(または変換できません)
(新しい動作; ヘルプを参照)

となっており、理解が難しいです。
続けての質問となってしまいますが、これは何のエラーでしょうか?

お礼日時:2006/01/06 17:37

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!