如何在数据结构中插入函数

How to insert function into data structure?

本文关键字:插入 函数 数据结构      更新时间:2023-10-16

在数据结构中,如何插入函数?

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);
}