Code khởi tạo đối tượng bị lỗi "no matching function for call to"

Mình viết 1 ứng dụng QT nhưng khi khởi tạo đối tượng Student thì gặp lỗi no matching function for call to 'Student::Student() Student *st=new Student();

#include "widget.h"
#include "ui_widget.h"
#include"student.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    Student *st=new Student();
           st->setname("tran manh hung");
           st->setdob("5/9/2996");
           st->setclass("d14dt1");
           ui->listWidget->addItem((QListWidgetItem *)st);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    Student *st1=(Student *)item;
     ui->name->setText(st1->getname());
     ui->dob->setText(st1->getdob());
     ui->class_2->setText(st1->getclass());
}

Hi trần mạnh hùng.
Xem lại lớp student dcủa bạn .

3 Likes

mình kiểm tra rồi nhưng ko biết sai ở đâu

#ifndef STUDENT_H
#define STUDENT_H
#include<QListWidgetItem>
#include<QString>
class Student:public QListWidgetItem
{
public:
    Student(QString name);
    ~Student();
    Student(QString dob,QString name,QString mclass);
    void setname(QString name);
    void setdob(QString dob);
    void setclass(QString mclass);
    QString getname();
    QString getdob();
    QString getclass();
private:
    QString m_dob;
    QString m_class;


};
inline void Student::setname(QString name)
{
    QListWidgetItem::setText(name);
}
inline void Student::setdob(QString dob)
{
    m_dob=dob;
}

inline void Student::setclass(QString mclass)
{
    m_class=mclass;
}

inline QString Student::getname()
{
    return QListWidgetItem::text();
}

inline QString Student::getdob()
{
    return m_dob;
}
inline QString Student::getclass()
{
    return m_class;
}
#endif // STUDENT_H

**file stdent.cpp**
#include "student.h"

Student::Student(QString name)
    :QListWidgetItem(name)
{
}
Student::~Student()
{

}

Klq, bạn post code nên để trong cặp 3 dấu nháy, như thế này:
[php]```[/php]
Các post đầu có thể cho qua, mod sửa giùm bạn, nhưng nếu post code không có ‘’’ thì dễ bị bỏ qua không reply hoặc nặng hơn là flag.

4 Likes

vâng cảm ơn bạn nhắc nhở

Hi trần mạnh hùng.
Bạn đọc về chú ý của C++ về hàm khởi tạo (Không nhầm khi bạn khai báo hàm khởi tạo thì hàm khởi tạo mặc định bị loại bỏ.)

2 Likes

ok đúng r bạn ạ mình nhầm chỗ đó

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?