C++ 必须返回一个值;整数向量;字数
我写的一个程序有一些问题,你可以在这个程序中找到文件中的字数。我遇到的问题是我的cpp中的函数“get_word_counts”。我在VisualStudio中不断收到一条消息,指出“错误C4716:'TextCounter::get_word_counts':必须返回一个值”,尽管事实上我确实在函数末尾返回了一个值 有人能帮我理解这是什么问题吗?我到处都找遍了,但我似乎不知道到底是什么问题。也许这很简单,但我就是看不出来 我将在下面发布我的cpp文件以及标题: cpp:C++ 必须返回一个值;整数向量;字数,c++,visual-studio,vector,return,C++,Visual Studio,Vector,Return,我写的一个程序有一些问题,你可以在这个程序中找到文件中的字数。我遇到的问题是我的cpp中的函数“get_word_counts”。我在VisualStudio中不断收到一条消息,指出“错误C4716:'TextCounter::get_word_counts':必须返回一个值”,尽管事实上我确实在函数末尾返回了一个值 有人能帮我理解这是什么问题吗?我到处都找遍了,但我似乎不知道到底是什么问题。也许这很简单,但我就是看不出来 我将在下面发布我的cpp文件以及标题: cpp: #include"Te
#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;
}
#包括“TextCounter.h”
#包括
#包括
#包括
/*
构造函数接受文件名并构建映射。
*/
TextCounter::TextCounter(std::字符串文件):
文件名(文件){
//构建输入列表。
解析_文件();
}
/*
获取文档中每个单词的计数。
如果文档中没有出现单词,请放入0。
*/
标准::向量文本计数器::获取单词计数(常量标准::向量和单词){
//TODO:完成此方法。
std::向量结果;
std::无序_映射::迭代器iter;
for(自动常数和输入:字){
iter=TextCounter::frequency.find(i);
if(iter==frequency.end()){
结果:推回(0);
}
否则{
结果:推回(iter->秒);
}
}
返回结果;
}
//在地图上添加一个单词。
//检查单词是否存在,如果存在,则递增
//否则,创建一个新条目并将其设置为1。
void TextCounter::add_word(std::string word){
//完成了这个方法。
//看看它是否已经在那里了。
如果(frequency.find(word)=frequency.end())//那么我们第一次遇到了这个词。
频率[字]=1;//将其初始化为1。
否则,我们以前已经看过了。。
频率[字]++;//递增。
}
//解析输入文件。
//如果出现错误,则返回-1。
int TextCounter::parse_文件(){
//局部变量。
std::ifstream inputfile;//从输入文件读取的ifstream。
//打开为输入指定的文件名。
inputfile.open(文件名);
//将输入标记化。
//一次读一个字符。
//如果字符不在a-z或a-z中,请终止当前字符串。
字符c;
字符当前长度[最大字符串长度];
int str_i=0;//索引到curr_str。
bool flush_it=false;//是否有完整的字符串要刷新。
while(inputfile.good()){
//读取一个字符,将其转换为小写。
get(c);
c=耐受力(c);
如果(c>='a'&&c=MAX\u STRING\u LEN){
刷新它=真;
}
}否则{
//c不是一个字母。
//如果curr_str为非空,则创建一个新字符串。
如果(str_i>0){
刷新它=真;
}
}
如果(冲洗它){
//从curr_str创建新字符串。
std::字符串的_str(curr_str,str_i);
//std::cout除了将向量
作为值返回是一个昂贵的操作(返回引用会更快)之外,我也看不出有什么错。昂贵肯定没有语法错误。@Aganju:不会很昂贵,因为它会被删除。不可复制。VS 2015可以毫无问题地编译代码。
#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);
};