本文介绍了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;
}

推荐答案

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;
}


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.




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

10-21 10:29