C++ 错误 C2109:下标需要数组或指针类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2829966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:12:32  来源:igfitidea点击:

error C2109: subscript requires array or pointer type

c++visual-studio-2008

提问by numerical25

I am trying to debug some homework but I am having trouble with these lines of code

我正在尝试调试一些作业,但我在处理这些代码行时遇到了问题

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string>
using namespace std;

int main()
{
   char word;
   cout << "Enter a word and I will tell you whether it is" << endl <<
 "in the first or last half of the alphabet." << endl << 
   "Please begin the word with a lowercase letter. --> ";
   cin >> word;
   if(word[0] >= 'm')
     cout << word << " is in the first half of the alphabet" << endl;
   else
     cout << word << " is in the last half of the alphabet" << endl;
   return 0;
}  

I get the following error and I have no clue what its sayings

我收到以下错误,我不知道它的说法

error C2109: subscript requires array or pointer type

回答by AnT

The term subscriptrefers to the application of []operator. In your word[0], the [0]part is a subscript.

术语下标指的是[]运算符的应用。在您的 中word[0],该[0]部分是一个下标。

The built-in []operator can only be used with arrays or pointers. You are trying to use it with an object of type char(your wordis declared as char), which is neither an array nor a pointer. This is what the compiler is telling you.

内置[]运算符只能与数组或指针一起使用。您正在尝试将它与类型对象char(您word的声明为char)一起使用,该对象既不是数组也不是指针。这就是编译器告诉你的。

回答by Lucas

Instead of

代替

char word;

declare

宣布

string word;

You already included the string-class header. Then you can access the elements with the []-operator.

您已经包含了 string-class 标头。然后您可以使用 [] 运算符访问元素。

Additional remark: Why do you use conio.h? It is obsolete and is not part of the C++ standard.

补充说明:为什么要使用conio.h?它已经过时并且不是 C++ 标准的一部分。

回答by Thomas Matthews

Another suggestion: declare output text as one entity, then block write. This may make your programs easier to debug, read and understand.

另一个建议:将输出文本声明为一个实体,然后阻止写入。这可能会使您的程序更易于调试、阅读和理解。

int main(void)
{
    static const char prompt[] =
    "Enter a word and I will tell you whether it is\n"
    "in the first or last half of the alphabet.\n"
    "Please begin the word with a lowercase letter. --> ";

   string word;
   cout.write(prompt, sizeof(prompt) - sizeof('##代码##'));

   getline(cin, word);

   cout << word;
   cout << "is in the ";
   if(word[0] >= 'm')
     cout "first";
   else
     cout << "last";

   cout << " half of the alphabet\n";
   return 0;
}

For Your Information (FYI):

供您参考(仅供参考):

  1. stdafx.his not a standard header and not required for small projects.
  2. conio.his not a standard header and not required for simple console I/O.
  3. Prefer stringfor text rather than char *.
  1. stdafx.h不是标准标题,小型项目不需要。
  2. conio.h不是标准头文件,也不是简单控制台 I/O 所必需的。
  3. 更喜欢string文本而不是 char *.

回答by dcp

word is declared as a char, not an array. But you're using word[0].

word 被声明为 a char,而不是数组。但是您正在使用word[0].