前提・実現したいこと
一週間で身につくC++言語の基本を参考に現在C++を勉強しています。
prob3-1.の問題でstringで宣言したcommentを取得したいのですが、char型はそのままreturnで返却できずほかの方法が思いつきません。
http://cpp-lang.sevendays-study.com/problem3.html
ソースコード3つ掲載します。
発生している問題・エラーメッセージ
data.cpp: メンバ関数 ‘char CData::getComment()’ 内: data.cpp:23:9: エラー: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return return comment;
該当のソースコード
C++
1// data.h 2#ifndef _CDATA_H_ 3#define _CDATA_H_ 4 5#include <iostream> 6#include <string> 7 8using namespace std; 9 10class CData 11{ 12private: 13 //メンバ変数number 14 int number; 15 //メンバ変数comment 16 string comment; 17public: 18 //初期化 19 void init(); 20 void setNumber(int num); 21 int getNumber(); 22 void setComment(string com); 23 char getComment(); 24}; 25 26#endif // _CDATA_H_ 27 28 29 30 31//data.cpp 32#include "data.h" 33#include <string> 34using namespace std; 35void CData::init() 36{ 37 number = 0; 38 comment = ""; 39} 40void CData::setNumber(int num) 41{ 42 number = num; 43} 44void CData::setComment(string com) 45{ 46 comment = com; 47} 48int CData::getNumber() 49{ 50 return number; 51} 52char CData::getComment() 53{ 54 return comment; 55} 56 57 58 59//main.cpp 60#include <iostream> 61#include "data.h" 62 63using namespace std; 64 65int main() 66{ 67 CData d; 68 d.init(); //パラメーターを初期化 69 d.setNumber(100); 70 d.setComment("Programming C++"); 71 cout << "number = " << d.getNumber() << " comment = " << d.getComment() << endl; 72 73 return 0; 74}
試したこと
C言語のポインタを使って解決しようと考えましたがうまくいかず手詰まりの状態です。
補足情報(FW/ツールのバージョンなど)
コンパイラはcygwinを使っています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/31 13:15
2018/01/31 13:20
2018/01/31 13:32