提问者:小点点

虽然定义和声明了,如何定义和声明?


我知道这个问题看起来令人困惑,但事实并非如此。 我正在构建一个关于drone的程序(抱歉,如果它没有意义)这里从这段代码中我得到错误“(第116行)标识符”CamRecord“是未定义的”和“编译器错误C2065”(第116行)标识符“:未声明的标识符”,尽管我已经识别并声明了它们? 对此如何解决? 谢谢

 #include<iostream>
    #include <fstream>
    #include <cstdlib>
    #include <iomanip>
    #include <string.h>
    
    using namespace std;
    
    void manualMode();
    void defaultMode();
    void Getdata(double & camrecord, double & Projectframerate);
    void calculate(double & camrecord, double & Projectframerate, double & actualframerate);
    
    
    
    int speed, timeTofly, operation;
    int angle = 90;
    
    int main() {
        int mode = 0, part = 3;
        double flyLevel;
        int obstacleSensor = 0, timer = 3, AccelerometerSensor = 0, tempSensor = 25;
        char startPause;
        cout << "Drone Flying Technology" << endl;
        do {
            cout << "Choose Mode: (1)Default, (2)Manual \n";
            cin >> mode;
            if (mode == 1)
                defaultMode();
            if (mode == 2)
                manualMode();
        } while (mode == 0);
    
        cout << "Choose how to fly: (1)Upper , (2)Lower , (3)Upper and Lower \n";
        cin >> part;
        switch (part) {
        case 1:
        case 2:
            flyLevel = 0.5;
            break;
        case 3:
            flyLevel = 1;
            break;
        }
        if (obstacleSensor == 0) {
            do {
                cout << "Press (S) to Start,and swing the propeller." << endl;
                cin >> startPause;
                cout << "LED is On\n";
                while (AccelerometerSensor != flyLevel) {
                    AccelerometerSensor++;
                }
                cout << "Flying session Started! Time Left:" << timeTofly << endl;
                cout << "Eagle eye Operation Started!\n";
                timeTofly = timeTofly / 2;
                cout << "Crusing mode initiate Time Left:" << timeTofly << endl;
                timeTofly = timeTofly / 2;
                cout << "Free fly commenced Time Left:" << timeTofly << endl;
                timeTofly = 0;
                startPause = 'P';
            } while ((startPause == 's') || (startPause == 'S'));
        }
        cout << "End!\nLED is Off";
        return 0;
    }
    
    
    void manualMode() {
        cout << "Enter drone speed (knot) \n";
        cin >> speed;
        cout << "Enter Time to fly: \n";
        cin >> timeTofly;
        cout << "Choose Operation: (1)Fully manual, (2)Normal Orientation, (3)Free orientation, (4)FPV racing, (5)All\n";
        cin >> operation;
    }
    void defaultMode() {
        int howtofly;
        cout << "how to fly: (1)Circle, (2)Altitude Hold, (3)Free orientation";
        cin >> howtofly;
        switch (howtofly) {
        case 1:
            speed = 30;
            timeTofly = 3;
            break;
        case 2:
            speed = 40;
            timeTofly = 3;
            break;
        case 3:
            speed = 60;
            timeTofly = 3;
            break;
        }
    
    
        char name[25];
        char id[5];
        float m1, m2, m3, m4, m5;
    
        ofstream outputFile("droneinfo.txt", ios::out);
    
        cout << "Please enter object you want to record,drone id imei number and your 5 preferred video fps:\n";
        cout << "\nPress <ctrl> + z to stop. \n";
    
        while (cin >> id >> name >> m1 >> m2 >> m3 >> m4 >> m5)
        {
            outputFile << id << " " << name << " " << m1 << " " << m2 << " " << m3 << " " << m4 << " " << m5 << endl;
        }
    
        double fps = 0.0;
        double Projectframerate = 0.0;
    
        double actualframerate, out = 0;
    
    
        Getdata(camrecord, Projectframerate);
        calculate(camrecord, Projectframerate, actualframerate);
        cout << "The actual frame rate is =" << actualframerate << " f/s\n";
    
    
    }
    void Getdata(double& camrecord, double& Projectframerate) {
        cin >> camrecord;
        cin >> Projectframerate;
        
        cout << "Please enter\n Recording frame rate =" << camrecord << "f/s ,\ndesired frame rate="
            << Projectframerate << "f/s\n";
    }
    
    //-------------------//
    void calculate(double& camrecord, double& Projectframerate, double& actualframerate) {
        actualframerate = camrecord / Projectframerate;
    }
    
    //-------------------//

共1个答案

匿名用户

因此,当您想要输入变量并修改它时,就会使用作为参数的引用。 查看void Getdata(Double&;camrecord,Double&;Projectframerate),您的意思是Getdata获取camrecord和projectframe速率,读取这些值并修改它们,以便调用方可以使用新值。

查看代码,似乎getdata实际上并不是在读取输入值。 所以有一个不一致的地方。

为了与方法的实现保持一致,方法getData不应该接受任何参数-而只是返回它们。 现在,您不能返回两个参数,但是可以将它们打包到一个结构中并返回:

struct Data
{
    double camrecord;
    double Projectframerate;
};

Data Getdata();

然后,GetData的定义可以是:

Data getData()
{
    Data data;
    cin >> data.camrecord;
    cin >> data.Projectframerate;
    
    cout << "Please enter\n Recording frame rate =" << data.camrecord << "f/s ,\ndesired frame rate="
        << data.Projectframerate << "f/s\n";

    return data;
}

对于calculate也可以进行类似的操作。