If I adapt some working Rcpp code to put it into a class, it stops working.
Here is the working, non-class-based code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List test1_() {
Rcpp::NumericVector x = Rcpp::NumericVector::create(1.0);
return Rcpp::List::create(Rcpp::Named("x") = x);
}
But when I separate it into .cpp ...
// testlist.cpp
#include <Rcpp.h>
#include "testlist.h"
using namespace Rcpp;
// [[Rcpp::export]]
SEXP test2_() {
testlist a_testlist;
return a_testlist.test;
}
... and .h ...
// testlist.h
#ifndef TESTLIST_
#define TESTLIST_
#include <Rcpp.h>
class testlist {
public:
testlist() {}
Rcpp::List test() {
Rcpp::NumericVector x = Rcpp::NumericVector::create(1.0);
return Rcpp::List::create(Rcpp::Named("x") = x);
}
};
#endif
... then I get the following compilation error.
g++ -I/usr/include/R/ -DNDEBUG -D_FORTIFY_SOURCE=2 -I"/home/nacnudus/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include" -fpic -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -c RcppExports.cpp -o RcppExports.o
g++ -I/usr/include/R/ -DNDEBUG -D_FORTIFY_SOURCE=2 -I"/home/nacnudus/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include" -fpic -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -c testlist.cpp -o testlist.o
testlist.cpp: In function ‘Rcpp::List test2_()’:
testlist.cpp:8:21: error: cannot convert ‘testlist::test’ from type ‘Rcpp::List (testlist::)() {aka Rcpp::Vector<19> (testlist::)()}’ to type ‘Rcpp::List {aka Rcpp::Vector<19>}’
return a_testlist.test;
^~~~
make: *** [/usr/lib64/R/etc/Makeconf:141: testlist.o] Error 1
ERROR: compilation failed for package ‘testlist’
* removing ‘/tmp/RtmppNxPq5/devtools_install_f654fb60a32/testlist’
What am I doing wrong?
Simple typo:
return a_testlist.test;
Needs to be:
return a_testlist.test();
Though, you should consider reading the Rcpp-modules vignette to properly expose classes.
Related
nvcc has supported recursive __global__ functions. What about clang? I failed to compile the following code (using clang++ -o helloWorld helloWorld.cu --cuda-gpu-arch=sm_75 -ldl -lrt -lcudart_static -pthread -L/usr/local/cuda/lib64 -std=c++14):
#include <stdio.h>
__global__ void cuda_hello(int i){
printf("Hello World from GPU!\n");
if (i < 3) {
cuda_hello<<<1,1>>>(i+1);
}
}
int main() {
cuda_hello<<<1,1>>>(0);
return 0;
}
. The error I got is
helloWorld.cu:6:3: error: reference to __global__ function 'cuda_hello' in __global__ function
cuda_hello<<<1,1>>>(i+1);
^
helloWorld.cu:3:17: note: 'cuda_hello' declared here
__global__ void cuda_hello(int i){
Anyone knows if/how clang supports such recursion?
At the time of writing (July 2020), it would appear that while separate compilation support has been added to clang, dynamic parallelism ("nested kernels") is still not supported.
Am beginner in move semantics and I'm confused by compilation result. When I use qt 5.5.1 all are okey, but when I use qt 4.8.7 I have build errors. I'm trying to build the following code:
struct TResource
{
int _resource_id = 18;
};
typedef std::vector<std::unique_ptr<TResource>> TResources;
struct TChannel
{
QString _title;
TResources _ress;
};
typedef std::vector<TChannel> TChanneles;
int main(int , char **)
{
TChannel channel;
TChanneles chnls;
//Creating some channel resorces and place it to the vector
// .......
chnls.push_back(std::move(channel));
return 0;
}
The error messages are following:
g++ -c -m64 -pipe -std=c++0x -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I. -o main.o main.cpp
In file included from /usr/include/c++/5/bits/stl_tempbuf.h:60:0,
from /usr/include/c++/5/bits/stl_algo.h:62,
from /usr/include/c++/5/algorithm:62,
from /usr/include/qt4/QtCore/qglobal.h:68,
from /usr/include/qt4/QtCore/qnamespace.h:45,
from /usr/include/qt4/QtCore/qobjectdefs.h:45,
from /usr/include/qt4/QtCore/qobject.h:47,
from /usr/include/qt4/QtCore/qcoreapplication.h:45,
from /usr/include/qt4/QtCore/QCoreApplication:1,
from main.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<TResource>; _Args = {const std::unique_ptr<TResource, std::default_delete<TResource> >&}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*; _Tp = std::unique_ptr<TResource>]’
/usr/include/c++/5/bits/stl_vector.h:322:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<TResource>; _Alloc = std::allocator<std::unique_ptr<TResource> >]’
main.cpp:11:8: required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = TChannel; _Args = {TChannel&}]’
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = TChannel*; _ForwardIterator = TChannel*; _Tp = TChannel]’
/usr/include/c++/5/bits/stl_uninitialized.h:303:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = TChannel*; _ForwardIterator = TChannel*; _Allocator = std::allocator<TChannel>]’
/usr/include/c++/5/bits/vector.tcc:422:8: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {TChannel}; _Tp = TChannel; _Alloc = std::allocator<TChannel>]’
/usr/include/c++/5/bits/vector.tcc:101:23: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {TChannel}; _Tp = TChannel; _Alloc = std::allocator<TChannel>]’
/usr/include/c++/5/bits/stl_vector.h:932:21: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = TChannel; _Alloc = std::allocator<TChannel>; std::vector<_Tp, _Alloc>::value_type = TChannel]’
main.cpp:26:39: required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = TResource; _Dp = std::default_delete<TResource>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from /usr/include/c++/5/memory:81:0,
from main.cpp:2:
/usr/include/c++/5/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^
Makefile:203: recipe for target 'main.o' failed
make: *** [main.o] Error 1
It seems that something not supported in qt 4.8.7 and I should take this into account and change my code. But I don't know what change.
Thank you in advance.
I have fixed my issue.
A have added move constructor TChannel struct:
TChannel(TChannel&& that)
{
_title = std::move(that._title);
}
It helped because move constructor was introduced in Qt 5.2.
I am new in Qt. I am trying to build my project in Qt 5.5.1 but getting the following error. What could be reason for this?
Error Snapshot:
---------------------------------------
18:56:20: Starting: "C:\Qt\Qt5.5.1\Tools\mingw492_32\bin\mingw32-make.exe"
C:/Qt/Qt5.5.1/Tools/mingw492_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'D:/Perforce_Client/project/src'
g++ -c -pipe -fno-keep-inline-dllexport -Wp,-isystem,D:/Perforce_Client/target/win32/usr/include -fno-strict-aliasing -Werror -O2 -std=c++0x -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CONCURRENT_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I..\..\target\win32\usr\include -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtQuick -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtGui -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtANGLE -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtQml -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtNetwork -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtConcurrent -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore -Iobj -IC:\Qt\Qt5.5.1\5.5\mingw492_32\mkspecs\win32-g++ -o obj\myGen3.o main\myGen3.cc
In file included from C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobject.h:40:0,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qiodevice.h:39,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qfiledevice.h:37,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore/qfile.h:37,
from C:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore/QFile:1,
from main\myGen3.cc:6:
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:65: error: variable or field 'qt_static_metacall' declared void
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:65: error: 'QObject' was not declared in this scope
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:74: error: expected primary-expression before ',' token
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:76: error: incomplete type 'QMetaObject' used in nested name specifier
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:95: error: expected primary-expression before 'int'
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
^
C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qobjectdefs.h:164:100: error: expected primary-expression before 'void'
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
---------------------------------------
I am using Qt Creator 3.5.1 (enterprise) based on Qt 5.5.1 (MSVC 2013, 32 bit).
Build&Run -> Kits configuration is: Desktop QT5.5.1 MinGW 32 bit
I am compiling a project using MSVC2010 compiler i am compiling it against following library (Qt\4.8.1\msvc2010) In that case compilation is successful with no error.
Now I am trying to compilng a QT app using minGW using following library (Qt\4.7.4\mingw).Then i am getting following error.
devicethreadobject.h include following file event.h.
Event.h file contains few template class.
Does MinGW have problem while working with template ?
Why compilation is successfull with MSVC compiler & fails with MINGW compiler ?
Event.h file :----
#ifndef EVENT_H
#define EVENT_H
#include <QtGui>
#include <QEvent>
//String event derived class
template <typename T> class StringEvent : public QEvent
{
QString m_str;
public:
explicit StringEvent(const QString val) : QEvent(staticType()), m_str(val)
{
}
void setvalue(QString val)
{
m_str = val;
}
QString value() const
{
return m_str;
}
static QEvent::Type staticType()
{
static int type = QEvent::registerEventType();
return static_cast<QEvent::Type>(type);
/*
static int type;
if(type == 0)
{
type = QEvent::registerEventType();
}
return static_cast<QEvent::Type>(type);*/
}
static bool is(const QEvent * ev)
{
return ev->type() == staticType();
}
};
class UpdateEvent : public StringEvent<UpdateEvent>
{
public:
explicit UpdateEvent(QString val): StringEvent(val)
{
//qDebug() << "hello";
}
};
class ClearEvent : public StringEvent<ClearEvent>
{
public:
explicit ClearEvent(QString val): StringEvent(val)
{
}
};
#endif // EVENT_H
Error :---
18:23:08: Running build steps for project CanSewLyzer_vs...
18:23:08: Starting: "c:\qtsdk\desktop\qt\4.7.4\mingw\bin\qmake.exe" D:\Stryker\Stryker\Qt\AutoS\CanSewLyzer_err_mingw\CanSewLyzer_vs\CanSewLyzer_vs.pro -r -spec win32-g++ "CONFIG+=release"
18:23:09: The process "c:\qtsdk\desktop\qt\4.7.4\mingw\bin\qmake.exe" exited normally.
18:23:09: Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe"
C:/QtSDK/mingw/bin/mingw32-make.exe -f Makefile.Release
mingw32-make.exe[1]: Entering directory `D:/Stryker/Stryker/Qt/AutoS/CanSewLyzer_err_mingw/CanSewLyzer_vs-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release'
c:/QtSDK/Desktop/Qt/4.7.4/mingw/bin/uic.exe ../CanSewLyzer_vs/mainwindow.ui -o ui_mainwindow.h
g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/ActiveQt' -I'release' -I'.' -I'../CanSewLyzer_vs' -I'.' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/mkspecs/win32-g++' -o release/main.o ../CanSewLyzer_vs/main.cpp
In file included from ../CanSewLyzer_vs/../../geny/common/mythread/devicethreadobject.h:8,
from ../CanSewLyzer_vs/mainwindow.h:9,
from ../CanSewLyzer_vs/main.cpp:2:
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h: In constructor 'UpdateEvent::UpdateEvent(QString)':
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:56: error: class 'UpdateEvent' does not have any field named 'StringEvent'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:56: error: no matching function for call to 'StringEvent<UpdateEvent>::StringEvent()'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:18: note: candidates are: StringEvent<T>::StringEvent(QString) [with T = UpdateEvent]
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:15: note: StringEvent<UpdateEvent>::StringEvent(const StringEvent<UpdateEvent>&)
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h: In constructor 'ClearEvent::ClearEvent(QString)':
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:68: error: class 'ClearEvent' does not have any field named 'StringEvent'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:68: error: no matching function for call to 'StringEvent<ClearEvent>::StringEvent()'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:18: note: candidates are: StringEvent<T>::StringEvent(QString) [with T = ClearEvent]
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:15: note: StringEvent<ClearEvent>::StringEvent(const StringEvent<ClearEvent>&)
mingw32-make.exe[1]: Leaving directory `D:/Stryker/Stryker/Qt/AutoS/CanSewLyzer_err_mingw/CanSewLyzer_vs-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release'
mingw32-make.exe[1]: *** [release/main.o] Error 1
mingw32-make.exe: *** [release] Error 2
18:23:14: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project CanSewLyzer_vs (target: Desktop)
When executing build step 'Make'*
It's not MinGW/GCC having issues with templates, it's MSVC being permissive about the syntax.
Change
explicit UpdateEvent(QString val): StringEvent(val)
...
explicit ClearEvent(QString val): StringEvent(val)
With
explicit UpdateEvent(QString val): StringEvent<UpdateEvent>(val)
...
explicit ClearEvent(QString val): StringEvent<ClearEvent>(val)
Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html
#include <QObject>
#include <QPushButton>
#include <iostream>
using namespace std;
class MyWindow : public QWidget
{
Q_OBJECT // Enable slots and signals
public:
MyWindow();
private slots:
void slotButton1();
void slotButton2();
void slotButtons();
private:
QPushButton *button1;
QPushButton *button2;
};
MyWindow :: MyWindow() : QWidget()
{
// Create button1 and connect button1->clicked() to this->slotButton1()
button1 = new QPushButton("Button1", this);
button1->setGeometry(10,10,100,40);
button1->show();
connect(button1, SIGNAL(clicked()), this, SLOT(slotButton1()));
// Create button2 and connect button2->clicked() to this->slotButton2()
button2 = new QPushButton("Button2", this);
button2->setGeometry(110,10,100,40);
button2->show();
connect(button2, SIGNAL(clicked()), this, SLOT(slotButton2()));
// When any button is clicked, call this->slotButtons()
connect(button1, SIGNAL(clicked()), this, SLOT(slotButtons()));
connect(button2, SIGNAL(clicked()), this, SLOT(slotButtons()));
}
// This slot is called when button1 is clicked.
void MyWindow::slotButton1()
{
cout << "Button1 was clicked" << endl;
}
// This slot is called when button2 is clicked
void MyWindow::slotButton2()
{
cout << "Button2 was clicked" << endl;
}
// This slot is called when any of the buttons were clicked
void MyWindow::slotButtons()
{
cout << "A button was clicked" << endl;
}
int main ()
{
MyWindow a;
}
results in:
[13:14:34 Mon May 02] ~/junkPrograms/src/nonsense $make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I/opt/qtsdk-2010.05/qt/include/QtCore -I/opt/qtsdk-2010.05/qt/include/QtGui -I/opt/qtsdk-2010.05/qt/include -I. -I. -o signalsSlots.o signalsSlots.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/opt/qtsdk-2010.05/qt/lib -o nonsense signalsSlots.o -L/opt/qtsdk-2010.05/qt/lib -lQtGui -L/opt/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread
signalsSlots.o: In function `MyWindow::MyWindow()':
signalsSlots.cpp:(.text+0x1a2): undefined reference to `vtable for MyWindow'
signalsSlots.cpp:(.text+0x1aa): undefined reference to `vtable for MyWindow'
signalsSlots.o: In function `MyWindow::MyWindow()':
signalsSlots.cpp:(.text+0x3e2): undefined reference to `vtable for MyWindow'
signalsSlots.cpp:(.text+0x3ea): undefined reference to `vtable for MyWindow'
signalsSlots.o: In function `main':
signalsSlots.cpp:(.text+0x614): undefined reference to `vtable for MyWindow'
signalsSlots.o:signalsSlots.cpp:(.text+0x61d): more undefined references to `vtable for MyWindow' follow
collect2: ld returned 1 exit status
make: *** [nonsense] Error 1
vtable is for virtual functions, AFAIK, what's the reason of error here?
It looks like moc doesn't generate code for your QObject because you declare it in the .cpp file. The easiest way to fix that is to move the declaration of MyWindow to a header, and add the header to the HEADERS list, in the .pro file:
HEADERS += yourheader.h
Then rerun qmake.
(Please note that the KDE 2.0 book you look at is vastly outdated)
Maybe too late but... Had the same issue and fighted for a while to find where it comes from.
Right click on your project and select “Run qmake” to for a new build of MOC classes. It usually does run automatically...
The moc compiler generates the stubs and calls in moc_xxxx.cpp, and generates the vtable stuff
Just Change your Main() as follows:
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWindow w;
w.show();
return a.exec();
}
Based on andref comment just above, when everything is in one cpp file like stuff.cpp, you need to add at the end of the file:
#include "moc_stuff.cpp"
(this is with Qt 4.7.0)
This issue may be caused by some of the following reasons. I have stumbled on all of them from time to time.
Your class header may be missing the Q_OBJECT macro. Add the macro to the header as already noted by other answers.
class MyWidg : public QWidget
{
Q_OBJECT
Your class header may not be parsed by the moc. Add the header file in the HEADERS definitions of your .pro (or .pri) project file.
HEADERS += file.h
If none of the above is true, then you probably need to run qmake again to make sure moc parses the header again, just in case the Q_OBJECT macro was added in the header after the qmake was run. Just run qmake again.