我正在编写一个程序,我正在编写文件中的单词数。我遇到的问题是我的cpp中的函数“get_word_counts”。我不断在Visual Studio中收到一条消息,指出“错误C4716:'TextCounter :: get_word_counts':必须返回一个值”,尽管事实上,我确实在函数结束时返回一个值。
有人可以帮我理解这个问题是什么吗?我到处搜索,但我似乎无法弄清楚究竟是什么问题。也许它很简单,但我看不到它。
我会在我的cpp文件以及标题下面发布:
CPP:
#include"TextCounter.h"
#include <string>
#include <iostream>
#include <vector>
/*
Constructor takes in the filename and builds the map.
*/
TextCounter::TextCounter(std::string file):
filename(file) {
// Build the input list.
parse_file();
}
/*
Get the count of each word in the document.
If a word doesn't occur in the document, put 0.
*/
std::vector<int> TextCounter::get_word_counts(const std::vector<std::string>& words) {
// TODO: Finish this method.
std::vector<int> result;
std::unordered_map<std::string, int>::iterator iter;
for (auto const &i : words) {
iter = TextCounter::frequency.find(i);
if (iter == frequency.end()) {
result.push_back(0);
}
else {
result.push_back(iter->second);
}
}
return result;
}
// Add a word to the map.
// Check to see if the word exists, if so, increment
// otherwise create a new entry and set it to 1.
void TextCounter::add_word(std::string word) {
// COMP: finished this method.
//Look if it's already there.
if (frequency.find(word) == frequency.end()) // Then we've encountered the word for a first time.
frequency[word] = 1; // Initialize it to 1.
else // Then we've already seen it before..
frequency[word]++; // Increment it.
}
// Parse an input file.
// Return -1 if there is an error.
int TextCounter::parse_file() {
// Local variables.
std::ifstream inputfile; // ifstream for reading from input file.
// Open the filename specified for input.
inputfile.open (filename);
// Tokenize the input.
// Read one character at a time.
// If the character is not in a-z or A-Z, terminate current string.
char c;
char curr_str[MAX_STRING_LEN];
int str_i = 0; // Index into curr_str.
bool flush_it = false; // Whether we have a complete string to flush.
while (inputfile.good()) {
// Read one character, convert it to lowercase.
inputfile.get(c);
c = tolower(c);
if (c >= 'a' && c <= 'z') {
// c is a letter.
curr_str[str_i] = c;
str_i++;
// Check over-length string.
if (str_i >= MAX_STRING_LEN) {
flush_it = true;
}
} else {
// c is not a letter.
// Create a new string if curr_str is non-empty.
if (str_i>0) {
flush_it = true;
}
}
if (flush_it) {
// Create the new string from curr_str.
std::string the_str(curr_str,str_i);
// std::cout << the_str << std::endl;
// COMP: Insert code to handle new entries or increment an existing entry.
TextCounter::add_word(the_str);
// Reset state variables.
str_i = 0;
flush_it = false;
}
}
// Close input file.
inputfile.close();
return 0;
}
标题:
#include <fstream>
#include <unordered_map>
#include <string>
#include <vector>
#define MAX_STRING_LEN 256
#define DICT_SIZE 20000
class TextCounter {
public:
explicit TextCounter(std::string file = "");
// Get the counts of a vector of words.
std::vector<int> get_word_counts(const std::vector<std::string>& words);
private:
// Name of the input file.
std::string filename;
// COMP: Implement a data structure to keep track of each word and the
// number of times that word occurs in the document.
std::unordered_map<std::string, int> frequency;
// Parse an input file.
int parse_file();
// Add a word to the map.
void add_word(std::string word);
};
非常感谢任何帮助。 谢谢!