出于某种原因,这段非常基本的代码在 Visual C++ 中编译时不会出现错误,但在 XCode 中会出现错误。我需要知道原因,以便继续在 Xcode 中为我的计算机科学类(class)工作。
#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;
}
它给出了两个错误,一个说它期望';'在帐户之前(声明 main 之后),第二个帐户未声明为 cin >> account.num;
最佳答案
问题实际上不在您的代码中:虽然在这种情况下 C 确实要求您在变量前加上 struct
前缀,但 C++ 不需要。问题实际上是在 Unix 上有一个名为 acct
的全局函数 - 正是 this 混淆了编译器。如果您将结构重命名为其他内容,例如 bank_account
,它将按您的预期运行。
关于c++ - 为什么结构在 Xcode 中不起作用,而在 Visual C++ 中起作用?需要帮助!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1608909/