简体   繁体   English

我不明白这个 c++ 错误,但我需要帮助修复它

[英]I do not understand this c++ error but I need help fixing it

I am trying to write a function that counts the number of words in a given string but I keep getting an error message every time I try to compile the code.我正在尝试编写一个 function 来计算给定字符串中的单词数,但每次尝试编译代码时我都会收到一条错误消息。

**The error message**: lab4.cpp: In function ‘int NumWords(const string&)’:
lab4.cpp:98:17: error: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’
  while (inSS >> str) {
                 ^
In file included from /usr/include/c++/4.8.2/iostream:40:0,
                 from lab4.cpp:9:
/usr/include/c++/4.8.2/istream:872:5: error:   initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = const std::basic_string<char>]’
     operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
     ^
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

//Function prototypes //函数原型

int NumWords(const string&);

int NumNonWSCharacters(const string&);

void CharReplace(string&, char, char);

char PrintMenu();

//Main function //主function

int main () {

//Variables
string text;

//Input & Output original
cout << "Enter a line of text: ";
getline(cin, text);
cout << "\n";
cout << "You entered: " << text << "\n";

//How many words
cout << NumWords(text) << "\n";

}

//Counts the number of words in a string //统计字符串中单词的个数

int NumWords(const string& str) {

int count = 0;
istringstream inSS(str);
while (inSS >> str) {

    count++;

}
return count;

}

//Count the number of characters (not including whitespace) in a string //统计字符串中的字符数(不包括空格)

int NumNonWSCharacters(const string&) {

cout << "f";

}

//Replaces one character with another in a given string //将给定字符串中的一个字符替换为另一个字符

void CharReplace(string&, char, char) {
cout << "FINISH\n";

}

//Prints the menu //打印菜单

char PrintMenu() {
cout << "FINISH\n";

}

Your NumWords function has an issue你的 NumWords function 有问题

int NumWords(const string& str) {

int count = 0;
istringstream inSS(str);
while (inSS >> str) {    // RIGHT HERE

    count++;

}
return count;

}

You're trying to use the const parameter, str , as the variable that receives the stream output of inSS >> str .您正在尝试使用 const 参数str作为接收inSS >> str的 stream output 的变量。 Since it's const, there's no way that inSS can write into it.因为它是常量,所以inSS无法写入它。 That's what the compiler is complaining about.这就是编译器所抱怨的。 Just use a temp variable to get around this.只需使用一个临时变量来解决这个问题。

int NumWords(const string& str) {

    int count = 0;
    istringstream inSS(str);
    std::string tmp;         // dummy string

    while (inSS >> tmp) {    // string into tmp
        count++;
    }
    return count;
}

Also, your NumNonWSCharacters and PrintMenu functions are missing returne values.此外,您的NumNonWSCharactersPrintMenu函数缺少返回值。 That should be an easy fix.那应该很容易解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM