c++ - 为什么结构在 Xcode 中不起作用,而在 Visual C++ 中起作用?需要帮助!

标签 c++ xcode

出于某种原因,这段非常基本的代码在 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/

相关文章:

c++ - 为最简单的项目生成的 Eclipse CDT 构建未正确设置包含路径

c++ - 如何将 std::vector 插入具有自定义排序功能的 std::set

java - 尝试用java代码编译并运行c++代码

xcode - 创建包 .pkg 文件时构建失败

swift - 在 SwiftUI 中的 ActionSheet 上设置辅助功能标识符

cocoa-touch - 用 cocoa 存储对象数组的最佳方法是什么?

c++ - 读取 header ,CURL

cocoa - 如何将 View 连接到图形上下文?

xcode - 如何在不使用自动布局的情况下使我的应用程序适合所有 iPhone 尺寸?

c++ - 尝试与 typedef 交 friend 时出现 "elaborated type refers to typedef"错误