【问题标题】:Invalid types 'int [5][float]' for array subscript数组下标的无效类型“int [5] [float]”
【发布时间】:2013-08-31 02:34:30
【问题描述】:

这是我的全部代码(在 C++ 中),以备不时之需。我正在使用 Eclipse Kepler。

//Tutorials from www.cplusplus.com/doc/tutorial combined
#include <iostream>
using namespace std;

//tutorial #1.1 Function with type -- Addition
int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

//tutorial #1.2 Function with type -- Subtraction
int subtraction (int a, int b){
    int r;
    r=a-b;
    return (r);
}

//tutorial #1.3 Function with NO type
void printmessage (void)
{
  cout << "It's good to be the king." << '\n';
}

//tutorial #2.1 Arguments passed by reference
void duplicate (int& a, int& b, int& c)
{
  a*=2;
  b*=2;
  c*=2;
}

//tutorial #2.2 and 2.5 Arguments passed by value and Declaration of preferred inline function
inline void prevnext (int x, int& prev, int& next)
{
  prev = x-1;
  next = x+1;
}

//tutorial #2.3 Default values in parameters (functions)
int divide (int a, int b=2)
{
  int r;
  r=a/b;
  return (r);
}

//tutorial #2.4.1 Overloaded functions -- int cannot handle decimals
int operate (int a, int b)
{
  return (a*b);
}

//tutorial #2.4.2 Overloaded functions -- float is used with decimals
float operate (float a, float b)
{
  return (a/b);
}

//tutorial #2.6 Recursivity -- factorial calculator
long factorial (long a)
{
  if (a > 1)
   return (a * factorial (a-1));
  else
   return (1);
}

//tutorial #2.7 Declaring function prototypes
void odd (int a); //declared
void even (int a); //declared

void odd (int a) //defined
{
  if ((a%2)!=0) cout << "Number is odd.\n";
  else even (a);
}

void even (int a) //defined
{
  if ((a%2)==0) cout << "Number is even.\n";
  else odd (a);
}

//tutorial #3.1 Initializing and accessing the values of an array
float billy [] = {16, 2, 77, 40, 12071};
float k, result=0;

int main ()
{
    cout << "Name: My Name \n";
    cout << "Homework: 1";
    cout << endl;
    cout << endl;

  int x=8, y=1, z, aa;
  float n=3.0,m=9.0;
  long number;

  z = addition (4,2);
  aa = subtraction (6,3);
    cout << "The sum is " << z << '\n';
    cout << "The subtraction is " << aa << '\n';
    cout << "The second subtraction is " << subtraction(x,y) << '\n';

  z = addition (x,y) - 3;
    cout << "The new sum is " << z << '\n';

  printmessage();

  duplicate (x,y,z);
    cout << "x=" << x << ", y=" << y << ", z=" << z << '\n';

  prevnext (x, y, z);
    cout << "Previous=" << y << ", Next=" << z;

    cout << endl;
    cout << "Division = " << divide (14);
    cout << endl;
    cout << "Second Division = " << divide (10,8);
    cout << endl;

    cout << "Operate Multiply: " << operate(x,y);
    cout << '\n';
    cout << "Operate Divide: " << operate(n,m);
    cout << endl;

    cout << "Please type a number: ";
      cin >> number;
      cout << number << "! = " << factorial (number);        
      cout << endl;

    int i;
      do {
        cout << "Type a number (type 0 to exit): ";
        cin >> i;
        odd (i);
      } while (i!=0);

      for ( k=0 ; k<5 ; k++ )
       {
          result += billy[k];
       }
       cout << result;

  return 0;
}

这是我编写的主要代码中存在问题的部分。更具体地说,结果 += billy[k] 行抛出错误:

      for ( k=0 ; k<5 ; k++ )
       {
          result += billy[k];
       }
       cout << result;

这对应于:

//tutorial #3.1 Initializing and accessing the values of an array
float billy [] = {16, 2, 77, 40, 12071};
float k, result=0;

我需要帮助修复此代码,但最重要的是我想知道为什么我的代码向我抛出了这个错误,以及如何使它变得更好(在初级水平上——显然,我不是在此处实施高级概念)。

编辑晚上 11:10 我忘记将其切换回 int。我最初有 int 但由于错误我想我会尝试 float 看看它是否有所作为。

【问题讨论】:

  • 为什么要使用浮点数据类型从0迭代到5?
  • 尝试将变量声明得更接近它们的使用位置。特别是,在 for 循环中声明计数变量。例如,for (int k = 0; k &lt; 5; k++) 这将更容易发现错误,例如使用了错误的类型。
  • 克里斯,我在原始帖子中添加了一条关于我为什么使用浮动的注释。尼尔柯克,我按照你的建议尝试了,但我仍然遇到同样的错误。不过感谢您的建议,因为从现在开始我肯定会继续这样做。
  • 在仍然出现错误的情况下尽量从代码中删减,然后发布一个新问题。

标签: c++ arrays eclipse compiler-errors


【解决方案1】:

您不能使用浮点类型作为数组的索引。 k 应该是一个整数。

【讨论】:

    【解决方案2】:

    您应该使用整数作为数组的索引。也就是说,将k 声明为整数。

    一种思考方式是,如果您尝试billy[1.5] 会怎样?这是否有意义,这应该返回第一个元素还是第二个元素,或者介于两者之间,或者什么都不返回?访问 billy[1.5] 没有意义,您应该将数组视为一组离散的值,并且索引应该只指向其中一个值。

    float billy[] = {1.0, 2.0, 3.0, 4.0, 5.0};
    float result = 0.0;
    int k;
    
    for (k = 0; k < 5; k++)
    {
        result += billy[k];
    }
    
    cout << result << endl;
    

    此外,除非您确实需要在程序中的任何位置访问resultbillyk,否则您应该考虑将它们移到您的main 函数中。正如 Neil Kirk 在其中一个 cmets 中所说,如果您的循环中只需要 k,则可以编写以下代码:

    for (int k = 0; k < 5; k++)
    {
        // Do Something
    }
    

    【讨论】:

    • 谢谢!我喜欢你解释它的方式。数组中的元素小于或大于整体是没有意义的。我仍然在努力解决我遇到的错误消息。我认为通过我所做的一些更正,我的程序应该可以编译。我开始怀疑我的 main 是否太大,以至于函数上方的行中的某些内容可能导致它无法工作。编辑:我刚刚移动了它,看起来同样的错误出现在它所在的行上,但它与我的代码无关。再次感谢您的帮助!
    【解决方案3】:

    根据 C++ 标准,数组索引必须是整数类型,您需要将变量 k 更改为整数类型。

    §8.3.4 数组

    在声明 T D 中,其中 D 具有以下形式

    D1 [ constant-expressionopt] attribute-specifier-seqopt
    

    且声明T D1中的标识符类型为“derived-declarator-type-list T”,则D的标识符类型为数组类型;

    如果常量表达式(5.19)存在,它应该是一个整数常量表达式,并且它的值应该大于零。常量表达式指定数组(元素数量)的边界。如果常量表达式的值为N,则该数组有N个元素,编号从0到N-1,D的标识符类型为“N T的派生声明器类型列表数组”。

    现在看看 § 5.19 常量表达式

    § 5.19.4 由于本国际标准对 浮点运算的准确性,未指定在翻译过程中对浮点表达式的求值是否与对同一表达式的求值产生相同的结果

    【讨论】:

      猜你喜欢
      • 2016-08-23
      • 2010-09-26
      • 2012-09-07
      • 1970-01-01
      • 2015-09-26
      • 2013-02-12
      • 1970-01-01
      相关资源
      最近更新 更多