qt报错【LNK2005、LNK1169】找到一个或多个多重定义的符号

=============== mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"teacher.h"
#include"student.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void classIsOver();

    Teacher *ls;
    Student *fz;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
======================= mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include"teacher.h"
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton *mybtn = new QPushButton(0);
    mybtn->setParent(this);
    mybtn->QPushButton::setText("我的按钮");
    mybtn->resize(200,40);
    mybtn->move(100,50);

    this->ls = new Teacher(this);
    this->fz = new Student(this);

    void(Teacher::*teacherSignal)(void)= &Teacher::hungry2;
    void(Student::*studentSlot)(void) = &Student::treat;
//    void(Student::*studentSlot)() = &Student::treat;

    connect(ls,teacherSignal,fz,studentSlot);

// void(Teacher::*teacherSlot)() = &Teacher::hungry2;
    connect(mybtn,&QPushButton::clicked,ls,teacherSignal);// clicked 这个信号的参数为 bool 类型,所以hungry2()作为槽函数也不能有参数(或者只能也是bool型参数)
//信号可以连接槽函数,也可以连接信号函数
}
//void MainWindow::classIsOver(){
//    ls->hungry2("宫保鸡丁");

//}
MainWindow::~MainWindow()
{
    delete ui;
}

=============== student.h
#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>
#include<QDebug>
#pragma once

class Student : public QObject
{
    Q_OBJECT
public:
    explicit Student(QObject *parent = nullptr);
    void treat();
void treat(QString food);
signals:


};

#endif // STUDENT_H
====================== teacher.h
#ifndef TEACHER_H
#define TEACHER_H

#include <QObject>
#include<QDebug>
#pragma once

class Teacher : public QObject
{
    Q_OBJECT
public:
    explicit Teacher(QObject *parent = nullptr);

//  void hungry2(QString food);

signals:
//    void hungry2();
//   inline void hungry2(QString food);
//   inline void hungry2();

   void hungry2(QString food);
   void hungry2();

};

#endif // TEACHER_H
===================== students.cpp
#include "student.h"

Student::Student(QObject *parent) : QObject(parent)
{

}
void Student::treat(QString food){
    qDebug()<<"请老师吃:"<<food<<endl;
}
void Student::treat(){
    qDebug()<<"请老师吃饭"<<endl;
}
================ teacher.cpp
#include "teacher.h"

Teacher::Teacher(QObject *parent) : QObject(parent)
{

}
void Teacher::hungry2(QString food){
    qDebug()<<"老师饿了,要吃:"<<food;
}

void Teacher::hungry2(){
    qDebug()<<"老师饿了,要吃:";
}


想要实现信号连接信号的功能,发现一直报错 

后面看了这篇文章在 hungry2() 和 hungry2(QString food) 加上 inline 就好了!

【LNK1169】找到一个或多个多重定义的符号_lyorz的博客-优快云博客_lnk1169找到一个或多个多重定义的符号

 

还不是很明白,对内联函数不清楚,还是要加强学习啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值