我从 The Programming Language 4th Edition 2013 复制并粘贴了代码:
到 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 时:
最佳答案
如上所述,您的语法使用 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/