C ++错误C2082:重新定义形式参数'whatItem'第15行 [英] C++ Error C2082: redefinition of formal parameter 'whatItem' line 15

查看:273
本文介绍了C ++错误C2082:重新定义形式参数'whatItem'第15行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了什么?

What Have I Done Wrong?

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;


void chance(int whatItem){

	srand(static_cast<unsigned int>(time(0)));
	int itemChance = rand();

	int Item = (itemChance % 10) +1;
	int whatItem = Item;	//This Is the Line With The ERROR!

}



void tutorial(){
	cout<<"Welcome to the tutorial";
	cout<<"\nHere we will teach you how to play this text adventure! (OMFG, Right?!)";


}
 

int main(){
	char action [6];
	int playTutorial;
	int age;
	string name;


	

	cout<<"################################################################################";
	cout<<"#                                                                              #";
	cout<<"#                           Beast's Text Adventure                             #";
	cout<<"#                                                                              #";
	cout<<"################################################################################";

	cout<<chance;
	cout<<"\n\n\n\n";
	cout<<"Do you want to play Beast's Text Adventure tutorial?\n";
	cout<<"1) Yes\n";
    cout<<"2) No\n\n";


	cin>>playTutorial;


	if (playTutorial == 1){


		tutorial();
	}else{system("CLS");



	cout<<"################################################################################";
	cout<<"#                                                                              #";
	cout<<"#                           Beast's Text Adventure                             #";
	cout<<"#                                                                              #";
	cout<<"################################################################################";
	/*Story One
	Will Be Deleted (Quick Note To Myself */

	cout<<"\n\nYou are in a dark room, you cant see anything except some light shining from \nsome doors.\nThere is two doors. One to the north. One to the south.";
    cout<<"\n\nWhich way do you want to go ";
		 cin>>action;


	//First room north
	  if ( std::strcmp( action, "north" ) == 0 ){
		  cout<<"\nIt leads to another room. This room is lighter then the last. You can make out alightswitch to the left of you.";
		  cout<<"\n\nWhat do you want to do ";
		  cin>>action;
		

		//First room south
	  }else if ( std::strcmp( action, "south" ) == 0 ){
		  cout<<"\nThis room looks the same as the last except there some light breaking through\nsome red velvet curtains.\n";
	  }
	}
		system("pause");
	return 0;
}

推荐答案

在你的代码中,你有:

In your code, you have:
cout << chance;



您应该将该行更改为:


You should change that line to:

cout << chance();



并重新编写没有参数的函数,并返回随机值:


and re-write your function without an argument, and return the random value:

int chance()
{
    srand(static_cast<unsigned int>(time(0)));
    int itemChance = rand();
    
    return (itemChance % 10) + 1;
}


在C和C ++中,参数按值传递,除非您将它们声明为参考参数,这些参数仅适用于C ++。因此

In C and C++ parameters are passed by value, except you declare them as reference parameters, which only works in C++. Hence
void chance(int whatItem){
 
	srand(static_cast<unsigned int="">(time(0)));
	int itemChance = rand();
 
	int Item = (itemChance % 10) +1;
	int whatItem = Item;	//This Is the Line With The ERROR!

}</unsigned>



whatItem是一个传递给函数但不能传回值的值参数。除了这个致命的错误之外,你在你的函数中重新声明了whatItem,这是不允许的。



将你的函数更改为:


whatItem is a value parameter that is passed into the function, but which cannot transfer a value back. On top of this fatal mistake you are re-declaring whatItem inside your function, which is not allowed.

Change your function to:

void chance (int& whatItem)
{
	srand(static_cast<unsigned int="">(time(0)));
	int itemChance = rand();
 
	int Item = (itemChance % 10) +1;
	whatItem = Item;
}
</unsigned>



,事情将按照您的预期运行。


and things will run the way you intended.


什么是

cout<<的输出神秘(6,2,5)<< endl;

假设以下函数定义为mystery?

int mystery(int x,int y,int z)

{

int value = x;

if(y> value)

value = y;

if(z>价值)

value = z;

返回值;

} //结束函数之谜



What is the output of
cout << mystery ( 6, 2, 5 ) << endl;
assuming the following function definition of mystery?
int mystery( int x, int y, int z )
{
int value = x;
if ( y > value )
value = y;
if ( z > value )
value = z;
return value;
} // end function mystery



这篇关于C ++错误C2082:重新定义形式参数'whatItem'第15行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆