Struct 不在一个程序的全局空间中工作,但在另一个程序中工作

问题描述 投票:0回答:0

我对 C++ 比较陌生。我最近在书中了解了 C++ 中的结构,它有以下代码来添加以英尺和英寸为单位的测量值。下面粘贴的代码按预期工作:

#include <iostream>
using namespace std;

struct Distance {
    int feet;
    float inches;
    };

int main(){
    Distance d1, d3; 
    Distance d2 = { 11, 6.25 }; 
    cout << "\nEnter feet: "; cin >> d1.feet;
    cout << "Enter inches: "; cin >> d1.inches;
    d3.inches = d1.inches + d2.inches; 
    d3.feet = 0; 
    if(d3.inches >= 12.0) 
        {
        d3.inches -= 12.0; 
        d3.feet++;
        }
    d3.feet += d1.feet + d2.feet;


    cout << d1.feet << "\'-" << d1.inches << "\" + ";
    cout << d2.feet << "\'-" << d2.inches << "\" = ";
    cout << d3.feet << "\'-" << d3.inches << "\"\n";
    return 0;
}

如您所见,结构变量是在主函数内部定义的,而结构是在其外部定义的。

我还决定测试我的知识并制作一个类似于此的程序,它将增加时间如下:

#include <iostream>
using namespace std;

// This function basically calculates how much is carried over to the next quantity for eg. if seconds      //is 122, it will return 2  which will be carried to minutes.



int carry (int inp) {
    int carry;
    carry = inp / 60;
    return carry;
}

struct time {
int hours;
int minutes;
int seconds;
};
    
int main(){

    char ques = 'y';
    time time1, time2, timetot;

    do {
        cout << "Want to add time (y/n) ? : "; cin >> ques;
        if (ques != 'y') break;
        cout << "Hours   : "  ; cin >> time1.hours  ; cout << endl;
        cout << "Minutes : "  ; cin >> time1.minutes; cout << endl;
        cout << "Seconds : "  ; cin >> time1.seconds; cout << endl;
        cout << "Hours   : "  ; cin >> time2.hours  ; cout << endl;
        cout << "Minutes : "  ; cin >> time2.minutes; cout << endl;
        cout << "Seconds : "  ; cin >> time2.seconds; cout << endl;
        
        if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); time1.seconds %= 60; }
        if (time1.minutes >=60) { time1.hours += carry(time1.minutes);   time1.minutes %= 60; }
        if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); time2.seconds %= 60; }
        if (time2.minutes >=60) { time2.hours += carry(time2.minutes);   time2.minutes %= 60; }

        timetot.hours = time1.hours + time2.hours ;
        timetot.minutes = time1.minutes + time2.minutes ;
        timetot.seconds = time1.seconds + time2.seconds ;

        if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); }
        if (time1.minutes >=60) { time1.hours += carry(time1.minutes); }
        if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); }
        if (time2.minutes >=60) { time2.hours += carry(time2.minutes); }

        cout << "Tot Hours"   << timetot.hours   << endl;
        cout << "Tot minutes" << timetot.minutes << endl;
        cout << "Tot seconds" << timetot.seconds << endl;
    }
    while (ques=='y') ;
    return 0;
};

上面的代码会产生time1, time2, time3 未定义的错误,但是我已经明确定义了这些结构变量。我也知道上面的代码没有语法错误,因为当我试图在 main() 函数中定义结构时,程序按预期运行。我也在下面粘贴代码:**

因为,我的编译器说他跟随:


**addTime.cpp: In function 'int main()':
addTime.cpp:20:9: error: expected ';' before 'time1'
   20 |     time time1, time2, timetot;
      |         ^~~~~~
      |         ;
addTime.cpp:27:40: error: 'time1' was not declared in this scope; did you mean 'timeb'?
   27 |         cout << "Hours   : "  ; cin >> time1.hours  ; cout << endl;
      |                                        ^~~~~
      |                                        timeb
addTime.cpp:30:40: error: 'time2' was not declared in this scope; did you mean 'timeb'?
   30 |         cout << "Hours   : "  ; cin >> time2.hours  ; cout << endl;
      |                                        ^~~~~
      |                                        timeb
addTime.cpp:39:9: error: 'timetot' was not declared in this scope; did you mean 'time_t'?
   39 |         timetot.hours = time1.hours + time2.hours ;
      |         ^~~~~~~
      |         time_t**

我决定将结构定义放在 main() 函数的范围内,我这样做了,代码就可以工作了。我粘贴了下面的代码,您也可以将其复制粘贴到您的编译器中并运行它。这是对我的代码没有语法错误的确认。那为什么书上写的代码可以运行,而我做同样的事情却不行

#include <iostream>
using namespace std;

// This function basically calculates how much is carried over to the next quantitiy for eg. if seconds //in 122, it will return 2  which will be carried to minutes

int carry (int inp) {
    int carry;
    carry = inp / 60;
    return carry;
};
    
int main(){
    struct time {
        int hours;
        int minutes;
        int seconds;
    };

    char ques = 'y';
    time time1, time2, timetot;

    do {

        cout << "Want to add time (y/n) ? : "; cin >> ques;
        if (ques != 'y') break;

        cout << "Hours   : "  ; cin >> time1.hours  ; cout << endl;
        cout << "Minutes : "  ; cin >> time1.minutes; cout << endl;
        cout << "Seconds : "  ; cin >> time1.seconds; cout << endl;
        cout << "Hours   : "  ; cin >> time2.hours  ; cout << endl;
        cout << "Minutes : "  ; cin >> time2.minutes; cout << endl;
        cout << "Seconds : "  ; cin >> time2.seconds; cout << endl;
        
        if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); time1.seconds %= 60; }
        if (time1.minutes >=60) { time1.hours += carry(time1.minutes);   time1.minutes %= 60; }
        if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); time2.seconds %= 60; }
        if (time2.minutes >=60) { time2.hours += carry(time2.minutes);   time2.minutes %= 60; }

        timetot.hours = time1.hours + time2.hours ;
        timetot.minutes = time1.minutes + time2.minutes ;
        timetot.seconds = time1.seconds + time2.seconds ;

        if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); }
        if (time1.minutes >=60) { time1.hours += carry(time1.minutes); }
        if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); }
        if (time2.minutes >=60) { time2.hours += carry(time2.minutes); }

        cout << "Tot Hours"   << timetot.hours   << endl;
        cout << "Tot minutes" << timetot.minutes << endl;
        cout << "Tot seconds" << timetot.seconds << endl;
    }
    while (ques=='y') ;
    return 0;
};

如您所见,我所做的只是在 main() 函数中定义结构,然后程序开始运行。我错过了什么?请帮助我。

c++ visual-c++ c++20 c++-standard-library
© www.soinside.com 2019 - 2024. All rights reserved.