gpt4 book ai didi

c++ - 数组下标的无效类型 'int [5][float]'

转载 作者:行者123 更新时间:2023-11-30 01:57:35 29 4
gpt4 key购买 nike

这是我的全部代码(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;
}

这是我在 main 中编写的代码中存在问题的部分。更具体地说,result += 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:10PM 我忘了将它切换回 int。我最初有 int 但由于错误我想我会尝试 float 看看它是否有所作为。

最佳答案

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

关于c++ - 数组下标的无效类型 'int [5][float]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18543911/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com