Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;表达式必须具有“类类型”;但是当检查类型时,它确实有一个char类? 我只是在为C++做一个简单的编码挑战,因为我对语言比较陌生,我想把两个字符串按词典的方式进行比较,以便如果输入的第一个字符串比第二个大,打印出来1,如果相反的话,打印出来1。当我循环时,我只想用小写字母来比较它们,所以如果第一个字符串中的一个字符是大写字母“a”,我想把它转换成小写字母“a”,并且相同的规则适用于正在比较的两个字符串的每个字符。当我试图在我的if语句中使用头实现这个想法时,就像这样first_str[i].tolower()对于第二个字符串,我得到了错误“表达式必须具有类类型”。这是我的密码: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { int first{}; int second{}; string first_str{}; string second_str{}; cin >> first_str; cin >> second_str; for (size_t i{}; i < first_str.length(); ++i) { // The if statement below has one accessing the character by indexing with the square brackets // and the other one has it through the .at() method provided by the <string> header if (first_str[i].tolower() > second_str.at(i).tolower()) { ++first; } else if (first_str.at(i).tolower() < second_str.at(i).tolower()) { ++second; } else { // If they are equal then just add the count to both ++first; ++second; } } if (first > second) cout << 1 << endl; else if (first > second) cout << -1 << endl; else cout << 0 << endl; return 0; } #包括 #包括 #包括 使用名称空间std; int main(){ int第一{}; int第二{}; 字符串第一个_str{}; 字符串第二个字符{}; cin>>第一街; cin>>第二条; 对于(size_t i{};i第二条[i].tolower()){ ++第一,; }else if(第一个在(i.tolower)上的str.at(i.tolower()第二次) 这是因为first_str[i]是一个char。char没有.tolower()方法_C++ - Fatal编程技术网

&引用;表达式必须具有“类类型”;但是当检查类型时,它确实有一个char类? 我只是在为C++做一个简单的编码挑战,因为我对语言比较陌生,我想把两个字符串按词典的方式进行比较,以便如果输入的第一个字符串比第二个大,打印出来1,如果相反的话,打印出来1。当我循环时,我只想用小写字母来比较它们,所以如果第一个字符串中的一个字符是大写字母“a”,我想把它转换成小写字母“a”,并且相同的规则适用于正在比较的两个字符串的每个字符。当我试图在我的if语句中使用头实现这个想法时,就像这样first_str[i].tolower()对于第二个字符串,我得到了错误“表达式必须具有类类型”。这是我的密码: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { int first{}; int second{}; string first_str{}; string second_str{}; cin >> first_str; cin >> second_str; for (size_t i{}; i < first_str.length(); ++i) { // The if statement below has one accessing the character by indexing with the square brackets // and the other one has it through the .at() method provided by the <string> header if (first_str[i].tolower() > second_str.at(i).tolower()) { ++first; } else if (first_str.at(i).tolower() < second_str.at(i).tolower()) { ++second; } else { // If they are equal then just add the count to both ++first; ++second; } } if (first > second) cout << 1 << endl; else if (first > second) cout << -1 << endl; else cout << 0 << endl; return 0; } #包括 #包括 #包括 使用名称空间std; int main(){ int第一{}; int第二{}; 字符串第一个_str{}; 字符串第二个字符{}; cin>>第一街; cin>>第二条; 对于(size_t i{};i第二条[i].tolower()){ ++第一,; }else if(第一个在(i.tolower)上的str.at(i.tolower()第二次) 这是因为first_str[i]是一个char。char没有.tolower()方法

&引用;表达式必须具有“类类型”;但是当检查类型时,它确实有一个char类? 我只是在为C++做一个简单的编码挑战,因为我对语言比较陌生,我想把两个字符串按词典的方式进行比较,以便如果输入的第一个字符串比第二个大,打印出来1,如果相反的话,打印出来1。当我循环时,我只想用小写字母来比较它们,所以如果第一个字符串中的一个字符是大写字母“a”,我想把它转换成小写字母“a”,并且相同的规则适用于正在比较的两个字符串的每个字符。当我试图在我的if语句中使用头实现这个想法时,就像这样first_str[i].tolower()对于第二个字符串,我得到了错误“表达式必须具有类类型”。这是我的密码: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { int first{}; int second{}; string first_str{}; string second_str{}; cin >> first_str; cin >> second_str; for (size_t i{}; i < first_str.length(); ++i) { // The if statement below has one accessing the character by indexing with the square brackets // and the other one has it through the .at() method provided by the <string> header if (first_str[i].tolower() > second_str.at(i).tolower()) { ++first; } else if (first_str.at(i).tolower() < second_str.at(i).tolower()) { ++second; } else { // If they are equal then just add the count to both ++first; ++second; } } if (first > second) cout << 1 << endl; else if (first > second) cout << -1 << endl; else cout << 0 << endl; return 0; } #包括 #包括 #包括 使用名称空间std; int main(){ int第一{}; int第二{}; 字符串第一个_str{}; 字符串第二个字符{}; cin>>第一街; cin>>第二条; 对于(size_t i{};i第二条[i].tolower()){ ++第一,; }else if(第一个在(i.tolower)上的str.at(i.tolower()第二次) 这是因为first_str[i]是一个char。char没有.tolower()方法,c++,C++,因此,不是: first\u str[i].tolower() 你应使用: tolower(第一个str[i])对于char没有tolower方法,也没有方法,因为它不是错误所说的类类型。@Quimby我仍然有点困惑,我来自python这样的语言,那里有一个类,用于处理字符串之类的东西,它也包含独立字符,因此假设char有自己的class type,对不起。这是否意味着:int、char和string literals等类型实际上没有类?不,它们没有,char只有一个字节长。但是string l

因此,不是:

first\u str[i].tolower()

你应使用:


tolower(第一个str[i])

对于
char
没有
tolower
方法,也没有方法,因为它不是错误所说的类类型。@Quimby我仍然有点困惑,我来自python这样的语言,那里有一个类,用于处理字符串之类的东西,它也包含独立字符,因此假设char有自己的class type,对不起。这是否意味着:int、char和string literals等类型实际上没有类?不,它们没有,
char
只有一个字节长。但是string literal和
std::string
之间有区别。如果你对这种语言很认真,你可能想看看。为了增加混淆:
char
int
不是类,但是
std::string
是类。但是字符串文本没有类型
std::string
,而是
const char[]
@dave我想我现在明白了,实际上,对于c类型字符串文本,您必须这样创建它们:''char my_string[5]{'H','e','l','l','o',\0}´最后一个空格包含空字符,对吗?