我创建了这个程序,但是在编译时出现以下错误信息:
<块引用>函数‘void FillDatabase()’:
main.cpp:59:4: 错误:无法将 '.... 从 '
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
struct Courses{
long Course_ID;
string name;
float Hours;
};
struct Students {
long ID;
string name;
};
struct Instructors {
long ID;
string name;
string Email;
string Course;
};
struct Schedule {
Courses Course;
Instructors instructor;
Students Student[10];
std::string Hall_NO;
int Time=0;
};
void FillDatabase()
{
struct Schedule Sch[10]={
{100,"C++ Basics",10.30f,10,"ahmed manna","gggg@gmail.com","c++",{ 10, "ali"},
{ 20, "Mohammed"},
{ 30, "Ahmad"},
{ 40, "Safaa"},
{ 50, "Marwa"},
{ 60, "Hind"},
{ 70, "Ibrahim"},
{ 80, "Ghada"},
{ 90, "Mahmud"},
{ 100, "Abdulsalam"},
"holl_5",11}
};
问题出在哪里?或者有其他解决方案吗?
答案 0 :(得分:0)
你试图用一个衬垫做太多事情,然后你陷入了错误的括号位置。如果缩进得当,对你来说会更明显。它应该是这样的。
void FillDatabase()
{
Schedule Sch[10] = {
{
{ 100, "C++ Basics", 10.30f },
{ 10,"ahmed manna", "gggg@gmail.com", "c++" },
{
{ 10, "ali" },
{ 20, "Mohammed" },
{ 30, "Ahmad" },
{ 40, "Safaa" },
{ 50, "Marwa" },
{ 60, "Hind" },
{ 70, "Ibrahim" },
{ 80, "Ghada" },
{ 90, "Mahmud" },
{ 100, "Abdulsalam" },
},
"holl_5",
11
},
};
}
此后您仍然有一个问题,即这种类型的 aggregate initialization 带有许多要求,其中之一是 no default member initializers 与此行一样:
int Time=0;
您需要将其更改为
int Time;
我建议您为各种结构提供适当的构造函数,而不是依赖聚合初始化。否则,如果您更改成员的顺序或添加或删除成员,那么您的代码将中断。拥有它就很脆弱。