gpt4 book ai didi

c++ - `Counter::metaObject() const'的多重定义

转载 作者:行者123 更新时间:2023-11-30 04:31:27 26 4
gpt4 key购买 nike

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.t 这个错误?
这个错误是什么意思?

我已经完成了ma​​ke clean,但这没有帮助。函数名称 metaObject 似乎是 Qt 内部的。

编辑 1:
添加头文件:

#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

输出:

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

源文件:

#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

最佳答案

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

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

关于c++ - `Counter::metaObject() const'的多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8166669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com