我正在尝试创建简单的数据库引擎。我对结构有疑问。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct LICZBY
{
int wartosc;
};
struct STUDENCI
{
int indeks;
string imie;
string nazwisko;
};
struct PRZEDMIOTY
{
int id;
string nazwa; // auto
int semestr; // clamp (1/10)
};
struct SALE
{
string nazwa;
int rozmiar; // clamp (10/600)
bool projektor;
double powierzchnia;
};
struct TABLES
{
vector<LICZBY> liczby;
vector<STUDENCI> studenci;
vector<PRZEDMIOTY> przedmioty;
vector<SALE> sale;
};
int main() {
TABLES tables;
tables.liczby.push_back({1});
cout << tables.liczby[0].wartosc;
// your code goes here
return 0;
}
我使用的是 Visual Studio 2012。此代码返回一个错误:需要一个表达式(此处为 tables.liczby.push_back({1});),但代码在 ideone.com 上有效。 http://ideone.com/fork/zc9pz8
怎么了?请给我一些建议。
最佳答案
VS2012 尚不支持 C++11 统一初始化语法/语义,这是您的 {1}
在此上下文中工作所必需的。
此时它不能重写为 VS2012 的单行代码(除非您在
LICZBY
类中声明一个转换构造函数)。对于LICZBY
的原始定义,您只能将其重写为const LICZBY liczby = { 1 }; tables.liczby.push_back(liczby);
如果添加转换构造函数
struct LICZBY { int wartosc; LICZBY(int wartosc) : wartosc(wartosc) {} };
那么你就可以这样做了
tables.liczby.push_back(1);
关于c++ - 为什么它在 Visual Studio 2012 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363539/