非常简单的代码与C ++"未找到标识符"问题

时间:2014-04-23 03:15:59

标签: c++

我正在为一个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";
        }
}

4 个答案:

答案 0 :(得分:2)

在使用之前输入已使用函数的声明,因此,在main之前,放置:

int Weather(int weather);
int WaterClarity(int waterclarity);
int Season(int season);

答案 1 :(得分:1)

见这里:

int Weather(int weather)
int WaterClarity(int waterclarity);
int Season(int season);

你这样做:

Weather();
WaterClarity();
Season();

这些函数调用与您定义的函数不同: 您已经创建了需要要提供的值的函数。< / strong>因此,您的函数调用不正确,因为您使用void参数调用它们。

这就是为什么你没有找到&#34;标识符&#34;编译器尝试查找名为Weather且返回整数的函数时出错,采用void参数。由于您没有定义(即您只有一个接受整数参数),因此会引发错误。

要解决此问题,您需要做两件事:

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
 }

此外,函数名称的当前约定是它们是小写的。大写名称保留给类和结构。只是一个FYI。

答案 2 :(得分:0)

您需要的是转发声明,将它们放在main()之前:

int Weather(int weather);
int WaterClarity(int waterclarity);
int Season(int season)
int main()
...

你应该得到另一个错误:

  

错误:函数'int Weather(int)'

的参数太少

然后你需要修复函数的定义或你调用函数的代码,以使它们保持一致。

答案 3 :(得分:0)

为您正在使用的功能提供前向声明

int Weather(int weather);
int Season(int season);
int WaterClarity(int waterclarity);

或复制粘贴主要功能。