为什么结构在Xcode中不起作用,当它们在Visual C++中时呢?需要帮助!

Jos*_*osh 3 c++ xcode

出于某种原因,这个非常基本的代码将在Visual C++中编译时没有错误,但在XCode中会出错.我需要知道为什么,为了继续在Xcode中为我的计算机科学课程工作.

#include <iostream>
#include <string>

using namespace std;

struct acct {        // bank account data
    int     num;      // account number
    string name;      // owner of account
    float   balance; // balance in account
};

int main() {

    acct account;

    cout << "Enter new account data: " << endl;
    cout << "Account number: ";
    cin  >> account.num;
    cout << "Account name: ";
    cin  >> account.name;
    cout << "Account balance: ";
    cin  >> account.balance;

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

它给出了两个错误,一个说它预期';' 在帐户之前(在声明主要之后),并且第二个帐户未被声明为cin >> account.num;

Jac*_*oyd 13

问题实际上并不在你的代码中:虽然C确实要求你struct在这种情况下为变量添加前缀,但C++却没有.这个问题实际上是有在Unix全局函数命名acct-这是是混淆编译器.如果您将结构重命名为其他内容,比如说bank_account,它将按预期运行.