我正在为一个C++类做一个项目,我不明白为什么它总是说“找不到标识符”,如果有任何帮助就太棒了!我在它说找不到标识符的地方加了粗体。Intellisense还在完全相同的空格中告诉我“函数调用中的参数太少”。我很确定这可能只是一件事,但我弄不明白。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int selection=0, lure=0, season=0;
cout << "Would you like to fish by lure or by season?";
cin >> selection;
if (selection==lure){
**Weather();**
**WaterClarity();**
}
else if (selection == season){
**Season();**
}
system("pause");
return 0;
}
int Weather(int weather)
{
cout << "What clarity is the weather today in: " << endl;
cout << "Clear" << endl << "Cloudy" << endl << "Dark";
}
int WaterClarity(int waterclarity)
{
cout << "What condition is the water in: "<<endl;
cout << "Clear" << endl << "Cloudy" << endl << "Murky";
cin >> waterclarity;
}
int Season(int season)
{
int month = 0;
cout << "Using numbers 1-12 what month is it?";
cin >> month;
if (month == 1){
cout << "The fish in season are: " << endl;
cout << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
cout << "Redfish" << endl << "Spanish Mackrel";
}
else if (month == 2){
cout << "The fish in season are: " << endl;
cout << "Cobia"<< endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
cout << "Redfish" << endl << "Spanish Mackrel";
}
else if (month == 3){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Cobia" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
cout << "Redfish" << endl << "Spanish Mackrel";
}
else if (month == 4){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Kingfish" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
cout << "Redfish" << endl << "Spanish Mackrel"<<endl<<"Tarpon";
}
else if (month == 5){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Kingfish" << endl << "Mahi Mahi" << endl << "Jack Crevalle"<<endl;
cout << "Tarpon";
}
else if (month == 6){
cout << "The fish in season are: " << endl;
cout << "Cobia" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
cout << "Tarpon";
}
else if (month == 7){
cout << "The fish in season are: " << endl;
cout << "Cobia" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
cout << "Tarpon";
}
else if (month == 8){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Redfish" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
cout << "Tarpon";
}
else if (month == 9){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Flounder" << endl;
cout << "Tarpon"<<endl<<"Kingfish";
}
else if (month == 10){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Flounder" << endl;
cout << "Tarpon" << endl << "Kingfish"<< endl<<"Redfish";
}
else if (month == 11){
cout << "The fish in season are: " << endl;
cout << "Blackfin Tuna" << endl << "Spanish Mackrel" << endl << "Mahi Mahi" << endl << "Flounder" << endl;
cout << "Tarpon" << endl << "Kingfish"<<endl<<"Pompano";
}
else if (month == 12){
cout << "The fish in season are: " << endl;
cout << "Jack Crevalle" << endl << "Mahi Mahi" << endl<<"Spanish Mackrel"<<endl;
cout << "Tarpon" << endl << "Kingfish"<<"Pompano";
}
}
发布于 2014-04-23 11:18:02
在使用函数之前放入所用函数的声明,因此,在main之前放入:
int Weather(int weather);
int WaterClarity(int waterclarity);
int Season(int season);
发布于 2014-04-23 11:20:15
请看这里:
int Weather(int weather)
int WaterClarity(int waterclarity);
int Season(int season);
您正在执行以下操作:
Weather();
WaterClarity();
Season();
这些函数调用与您定义的函数不同:您创建的函数需要要提供的值。因此,您的函数调用是不正确的,因为您使用空参数调用它们。
这就是当编译器试图查找一个返回整数Weather
的名称的函数,而使用空参数时,出现“未找到标识符”错误的原因。由于您没有定义任何参数(即您只定义了一个整数参数,a参数),因此会抛出一个错误。
要解决此问题,您需要做两件事:
1)在使用函数之前(在main之前)向前声明函数:
int Weather(int weather)
int WaterClarity(int waterclarity);
int Season(int season);
在您的示例中,您需要像这样调用函数:
Weather(0);
WaterClarity(0);
Season(0);
但是,尽管这是合乎逻辑的,但它可能看起来有点奇怪。因此,更好的方法是使用默认参数(例如):
int Weather(int weather=0);//to be used as a forward declaration
int Weather(int weather) {
//check to see if weather is 0, if it is 0 then it has been called like this:
//`Weather()`
int returnvalue = 0;
if (weather == 0) {
//do something if weather is 0
}
else {
//do something else
}
return returnvalue;//you set this value based on your calculation
}
另外,函数名称的当前约定是它们是小写的。大写的名字是为类和结构保留的。只是一个参考。
发布于 2014-04-23 11:18:56
您需要的是转发声明,将它们放在main()
之前
int Weather(int weather);
int WaterClarity(int waterclarity);
int Season(int season)
int main()
...
你应该会得到另一个错误:
错误:函数'int Weather(int)‘的参数太少
然后,您需要修复函数的定义,或您调用的函数的代码,以使它们保持一致。
https://stackoverflow.com/questions/23234135
复制相似问题