如何在数据结构中插入函数
How to insert function into data structure?
在数据结构中,如何插入函数?
struct Student_info {
std::string name;
double midterm, final;
unsigned int& counter;
std::vector<double> homework;
double overall = grade(students[counter]);
};
总是得到这种类型的错误:-
。此代码中没有声明"variable"
b。"Student_info::counter"不能出现在常量表达式中。
c。数组引用不能出现在常量表达式中。
d。函数调用不能出现在常量表达式
中。编辑:-哦,我的意思是student_info包含在一个向量中,等等,为什么需要这个信息…Dx
哦,顺便说一句,这是来自加速c++,显然是一本书,我想回答它的一个练习,然后我需要知道这部分,在书中没有找到Dx
问题是4-6。重写Student_info结构以立即计算成绩,并仅存储最终成绩。
您可以NOT动态地将函数插入到结构体中。
你可以声明一个具有方法()的结构
struct Student_info
{
void doDomethingToStudent()
{
// Manipulate the object here.
}
// STUFF
};
也不能像上面那样初始化成员。
double overall = grade(students[counter]);
这里需要创建初始化成员的构造函数。
struct Student_info
{
Student_info(std::string& studentName, unsigned int& externalCounter)
: name(studentName)
, midterm(0)
, final(0)
, counter(externalCounter)
, homework()
// It is not clear if overall is a normal memeber
// Or a static member of the class
, overall(grade(students[counter]))
{}
// STUFF
};
int main()
{
unsigned int counter = 0;
Student_info bob("Bob", counter);
}
相关文章:
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 为 std::vector 编写自定义插入函数
- 类数组插入函数
- 需要帮助将字符串插入函数
- 在 C++ 中为特定哈希表创建插入函数
- 为sLinkedList编写插入函数时引发异常
- 无法转换字符串插入函数的参数
- 错误表达式必须是插入函数(二叉树 C++)中的可修改 Ivalue
- 具有 4 个指针的节点的递归插入函数返回 null
- 在字符串中的字符前添加空格C++插入函数
- "<某些系统标头>:错误:"<在此处插入函数>"在移动源文件后不是"std"的成员
- 自己的C++列表类实现(插入函数)出现问题
- 是否有任何快捷键可以插入函数的定义
- 使用算法中的插入函数将元素插入空容器中,未给出预期的结果
- 如何改进单向链表插入函数 - C++
- 在 QString 插入函数中使用参数
- 如何在LLVM传递中的模块末尾插入函数调用?
- 使用模板的插入函数会丢失数字
- Bool排序的插入函数,检查列表中是否已经存在int