首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >` `Counter::metaObject() const‘的多重定义

` `Counter::metaObject() const‘的多重定义
EN

Stack Overflow用户
提问于 2011-11-17 11:54:24
回答 2查看 3.5K关注 0票数 3
代码语言:javascript
复制
moc_mm.o: In function `Counter::metaObject() const':
moc_mm.cpp:(.text+0x0): multiple definition of `Counter::metaObject() const'
moc_joystick.o:moc_joystick.cpp:(.text+0x0): first defined here
moc_mm.o:(.rodata+0x0): multiple definition of `Counter::staticMetaObject'

我应该在这里显示代码的哪一部分,w.r.r.这个错误?

这个错误的意思是什么?

我已经做了make干净的,但这没有帮助。函数名metaObject似乎是Qt的内部函数。

编辑1:

添加头文件:

代码语言:javascript
复制
#include <stdio.h>
#include <QObject>
#ifndef __JOYSTICK_H__
#define __JOYSTICK_H__

#define JOYSTICK_DEVNAME "/dev/input/js0"

#define JS_EVENT_BUTTON         0x01    /* button pressed/released */
#define JS_EVENT_AXIS           0x02    /* joystick moved */
#define JS_EVENT_INIT           0x80    /* initial state of device */


struct js_event {
    unsigned int time;  /* event timestamp in milliseconds */
    short value;   /* value */
    unsigned char type;     /* event type */
    unsigned char number;   /* axis/button number */
};

struct wwvi_js_event {
    int button[11];
    int stick_x;
    int stick_y;
};

class Counter : public QObject
 {
    Q_OBJECT
    private:
        int m_value;

    public:
        Counter() { m_value = 0; }

        int value() const { return m_value; }

    //public slots:
        int open_joystick();
        int read_joystick_event(struct js_event *jse);
        //void set_joystick_y_axis(int axis);
        //void set_joystick_x_axis(int axis);
        void close_joystick();
        int get_joystick_status(struct wwvi_js_event *wjse);

    //signals:
        //void valueChanged(int newValue);
 };
#endif

输出:

代码语言:javascript
复制
anisha@linux-dopx:~/Desktop/anishaJoystick> qmake -project
anisha@linux-dopx:~/Desktop/anishaJoystick> qmake
anisha@linux-dopx:~/Desktop/anishaJoystick> make
g++ -m64 -Wl,-O1 -Wl,-rpath,/home/anisha/qtsdk-2010.05/qt/lib -o anishaJoystick joy.o mm.o moc_joystick.o moc_mm.o    -L/home/anisha/qtsdk-2010.05/qt/lib -lQtGui -L/home/anisha/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread 
moc_mm.o: In function `Counter::metaObject() const':
moc_mm.cpp:(.text+0x0): multiple definition of `Counter::metaObject() const'
moc_joystick.o:moc_joystick.cpp:(.text+0x0): first defined here
moc_mm.o:(.rodata+0x0): multiple definition of `Counter::staticMetaObject'
moc_joystick.o:(.rodata+0x0): first defined here
moc_mm.o: In function `Counter::qt_metacast(char const*)':
moc_mm.cpp:(.text+0x20): multiple definition of `Counter::qt_metacast(char const*)'
moc_joystick.o:moc_joystick.cpp:(.text+0x30): first defined here
moc_mm.o: In function `Counter::valueChanged(int)':
moc_mm.cpp:(.text+0x70): multiple definition of `Counter::valueChanged(int)'
mm.o:mm.cpp:(.text+0x10): first defined here
moc_mm.o: In function `Counter::qt_metacall(QMetaObject::Call, int, void**)':
moc_mm.cpp:(.text+0xb0): multiple definition of `Counter::qt_metacall(QMetaObject::Call, int, void**)'
moc_joystick.o:moc_joystick.cpp:(.text+0x20): first defined here
collect2: ld returned 1 exit status
make: *** [anishaJoystick] Error 1

源文件:

代码语言:javascript
复制
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "joystick.h"

static int joystick_fd = -1;

int Counter::open_joystick()
{
    joystick_fd = open(JOYSTICK_DEVNAME, O_RDONLY | O_NONBLOCK); // read write for force feedback?
    if (joystick_fd < 0)
        return joystick_fd;

    // maybe ioctls to interrogate features here? 

    return joystick_fd;
}

int Counter::read_joystick_event(struct js_event *jse)
{
    int bytes;

    bytes = read(joystick_fd, jse, sizeof(*jse)); 

    if (bytes == -1)
        return 0;

    if (bytes == sizeof(*jse))
        return 1;

    printf("Unexpected bytes from joystick:%d\n", bytes);

    return -1;
}

void Counter:: close_joystick()
{
    close(joystick_fd);
}

int Counter::get_joystick_status(struct wwvi_js_event *wjse)
{
    int rc;
    struct js_event jse;
    if (joystick_fd < 0)
        return -1;

    // memset(wjse, 0, sizeof(*wjse));
    while ((rc = read_joystick_event(&jse) == 1)) {
        jse.type &= ~JS_EVENT_INIT; /* ignore synthetic events */
        if (jse.type == JS_EVENT_AXIS) {
            switch (jse.number) {
            case 0: wjse->stick_x = jse.value;
                break;
            case 1: wjse->stick_y = jse.value;
                break;
            default:
                break;
            }
        } else if (jse.type == JS_EVENT_BUTTON) {
            if (jse.number < 10) {
                switch (jse.value) {
                case 0:
                case 1: wjse->button[jse.number] = jse.value;
                    break;
                default:
                    break;
                }
            }
        }
    }
    // printf("%d\n", wjse->stick1_y);
    return 0;
}

//#if 0
/* a little test program */
int main(int argc, char *argv[])
{
    int fd, rc;
    int done = 0;

    struct js_event jse;

    Counter c;
    fd = c.open_joystick();
    if (fd < 0) {
        printf("open failed.\n");
        exit(1);
    }

    while (!done) {
        rc = c.read_joystick_event(&jse);
        usleep(1000);
        if (rc == 1) {
            printf("Event: time %8u, value %8hd, type: %3u, axis/button: %u\n", jse.time, jse.value, jse.type, jse.number);
        }
    }
}
//#endif
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-18 09:35:42

您的目录中可能有多个moc_*文件,可能是一些重命名后的旧文件,运行qmake -project已经将旧的文件包含在您的构建中。它为相同的名称(moc_mm.cpp和moc_joystick.cpp)和冲突找到多个moc_mm.cpp声明。

手动删除所有moc_*文件,用-project重新创建.pro文件.

票数 2
EN

Stack Overflow用户

发布于 2020-06-16 01:54:20

当您意外地为同一个类使用宏两次时,也会发生此错误。

例如,在从.cpp文件中的声明和实现的源代码布局(如描述的here)到将它们分成.h.cpp的典型布局的混乱转换过程中,我就遇到了这种情况。映射到示例代码时,我的代码如下:

  • joystick.h:与问题完全相同,包括这个带宏调用的关键部分:

类计数器: public QObject { Q_OBJECT // .方法声明在这里. };

  • joystick.cpp,包括意外的第二次宏调用,从原始实现的剩余部分完全在这里:

类计数器:公共QObject { //宏的错误第二次调用。Q_OBJECT // .这里的方法定义. };//在Q_OBJECT文件中使用.cpp宏时必须包含。#包括"joystick.moc"

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8166669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档