c++ - Microsoft Visual C++ 2010 Express - 正确代码中的错误

标签 c++ visual-studio-2010 class compilation

我从 The Programming Language 4th Edition 2013 复制并粘贴了代码:

The Programming Language 4th Edition 2013, page 49

到 Microsoft Visual C++ 2010 Express 并编译。我有很多错误,为什么?编译器版本老?我没有运行它的所有代码? Microsoft Visual C++ 2010 Express

复制代码:

#include <iostream>
#include <cstdio>
using namespace std;


class vector{
public:
    vector(int s) :elem{new double[s]}, sz{s} {}
    double& operator[](int i) {return elem[i];}
    int size() {return sz;}
private:
    double* elem;
    int sz;
};


void main()
{
    vector v(6);
}

[编辑]当我使用 Visual Studio 2013 时:

Visual Studio 2013

最佳答案

如上所述,您的语法使用 C++11,这在 Visual Studio 2010 中不可用。

但是,代码中的小改动将使其通过编译。 只需在构造函数行中将 {} 替换为 () + 使 main() 返回一些东西(不知道这是否特定于 C++11,但我的编译器不想使用 void< 进行编译主要)。

#include <iostream>
#include <cstdio>
//using namespace std; commented, it's useless and confusing


class vector{
public:
    vector(int s) : elem(new double[s]), sz(s) {}
    double& operator[](int i) {return elem[i];}
    int size() {return sz;}
private:
    double* elem;
    int sz;
};


int main()
{
    vector v(6);
    return 0;
}

关于c++ - Microsoft Visual C++ 2010 Express - 正确代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285849/

相关文章:

c++ - 无法编译简单的C++17程序

.net - 在 Visual Studio 2010 中组织或颜色编码项目和文件夹

swift - 是否应该将 Swift 类的实例分配给 `nil` 以查看反初始化器的运行情况?

c# - 如何将我的代码重组为 C# 中的类?

c++ - 接口(interface)的名称是否可以与其类中的名称不同? [C++]

c++ - Linux ioctl -> 如何判断当前 IP 是否由 dhcp 获取

c++ - 使用 boost::mpl::lambda 从基于静态常量成员变量的 boost::mpl::list 中删除类型

javascript - VS2010 ASP.NET : How can I set a breakpoint in my Javascript?

c# - 在 WP7 应用程序中使用 .GIF 动画作为启动画面

c++ - 为什么这段代码不能运行? [初学者]