私はクラスHashTaggerの2つのメソッドを持っています。同じクラスにある文字列を検出できない原因となるのは何ですか?
最初(void getTags(std :: string line);)は入力を受け取り、好きなように書式設定された文字列を生成します。
他の(void printTags();)は、その文字列をコンソールに、十分な簡単な方法で出力する必要があります。
しかし、出力が決して私は愚かな何かが欠けていない来る。ここで
メイン
メインループの最後のサイクルの後#include "HashTagger.h"
#include <string>
#include <iostream>
using namespace hw02;
using namespace std;
int main() {
// Construct an object for extracting the hashtags.
HashTagger hashTagger;
// Read the standard input and extract the hashtags.
while (true) {
// Read one line from the standard input.
string line;
getline(cin, line);
cout << line << endl;
if (!cin) {
break;
}
// Get all of the hashtags on the line.
hashTagger.getTags(line);
}
// Print the hashtags.
hashTagger.printTags();
// Return the status.
return 0;
}
ソース法と更新
#include "HashTagger.h"
#include <iostream>
using namespace std;
using namespace hw02;
void HashTagger::getTags(string line) {
int h = 0;
int i = 0;
//add a space after line, so we can check for end of line
line = line + " ";
// Loop over all characters in a line that can begin a hashtag
for (unsigned int j = 0; j < line.length(); ++j) {
char c = line.at(j);
// if "#" is found assign beginning of a hashtag to h
if (c == '#') {
h = j; //h is the beginning char of the hashtag
i = 1; //signifies that a hashtag has been begun
// checks that a hashtag has begun, then looks for a newline, ".", "?", or "!" and adds substring of the hashtag to hashtags_
} else if (i == 1 && (c == ' ' || c == '\r' || c == '.' || c == '?' || c == '!')) {
hashtags_ = hashtags_ + "\n" + line.substr(h, j - h);
h = 0;
i = 0;
}
}
//TEST// cout << hashtags_ << endl;
}
ある最後に、試験によって示されるように、これは私がしたい出力を生成しています上の行。しかし、私はその変数を同じcout < <呼び出しを使用して出力としてprintTags()メソッドに持たせたいと思います。
void HashTagger::printTags() {
// print out hashtags_ to the console
cout << hashtags_ << endl;
}
最後にヘッダ
#ifndef HASHTAGGER_H
#define HASHTAGGER_H
#include <string>
namespace hw02 {
class HashTagger {
public:
void getTags(std::string line);
void printTags();
std::string hashtags_;
};
}
#endif
ここでは、場合I出力が出なかった理由私は考え出しテスト入力
Test#of hash#tagging
#even!#when #starting? a line
or #containing a #comma,
と期待される出力
#of
#tagging
#even
#when
#starting
#containing
#comma,
で答えを見つけました。 –
[最小限の、完全で検証可能な例](http://stackoverflow.com/help/mcve)を投稿してください。 –
フルコードなしでは難しいですが、あなたのprintTagsに 'this'がありませんか? – Lagerbaer