I'm having issues with multithreading and multifile projects. Works fine when testing with a single file project but as I am trying to keep my headers separated from my implimentation, is there a way to make this work?
the error I am getting is:
error C3867: 'class1::Update': function call missing argument list; use '&class1::Update' to create a pointer to member
Sadly, the suggestion there doesn't work. Any help would be greatly appreciated.
Class1.H
class class1
{
public:
class1();
~class1();
private:
thread sThread;
void Update();
};
Class1.cpp
int class1::Initialize()
{
this->sThread = std::thread(Update);
}
As you say, the error is:
'class1::Update': function call missing argument list; use '&class1::Update' to create a pointer to member
So do that. Once you do you will find that you then need to use std::bind() to attach an instance of the class to the member function. That will look like:
thread(bind(&class1::Update, this))
Related
I am trying to implement a listener. Because of many cross-references I am trying to avoid including other classes and pre-define them
My listener looks as follows
.h
class Book
{
public:
Book();
private:
std::vector<MyListener *> listeners_;
void Notify();
}
.cpp
Book::Book() {}
void Book::Notify() {
MyListener *p_listener;
for ( int i = 0; i < this->listeners_.size(); i++ ) {
p_listener = listeners_[i];
p_listener->Update(); // ERRORS THROWN HERE WHEN NOT INCLUDING LISTENER.H
}
}
This all works fine when I include the listener.h file
#include "listener.h"
But when I instead pre-declare Listener it doesnt work
class Listener;
It gives me the two errors
C:\CPP\qtTradeSim\qtTradeSim\test\book.cpp:33: error: C2027: use of undefined type 'Listener'
C:\CPP\qtTradeSim\qtTradeSim\test\book.cpp:33: error: C2227: left of '->Update' must point to class/struct/union/generic type
Is there a way to avoid including the Listener header?
In the header file of class Book, you should indeed use a forward declaration of MyListener, as the header only defines an std::vector of pointers to MyListener and does not need to know the full declaration of MyListener.
The implementation file of class Book, however, actually needs the full declaration of MyListener, as it calls its update method, so you would include listener.h in the implementation file of class Book instead of in the header file.
Let's suppose the compiler sees the following code:
class Listener;
std::vector<Listener*> pListeners;
// some code...
for(auto& pListener: pListeners) {
pListener->update();
}
Note, how does the compiler see the Listener has a member function update? The symbol update could not be determined until the compiler see the Listener full declaration. Think if you used update with an argument missing, could the compiler capture this problem without seeing the declaration of update? Thus, it cannot translate the code. If you give a full declaration of the Listener, e.g.
class Listener {
public:
Listener() { // some construction
}
void update() {
// dosth
}
};
The compiler could know the update method, its parameters, the return value, etc., and compile it happily.
I am using OOLUA 2.0.0 and am receiving the error undefined reference to OOLUA::Proxy_class<TestClass>::class_name.
The code is:
class TestClass
{
int test_member;
public:
void setTestMember(int x) { test_member = x; }
int getTestMember() { return test_member; }
};
OOLUA_PROXY(TestClass)
OOLUA_MEM_FUNC(void, setTestMember, int)
OOLUA_MEM_FUNC(int, getTestMember)
OOLUA_PROXY_END
int main()
{
OOLUA::Script script;
script.register_class<TestClass>();
OOLUA::run_chunk(script, "local n = TestClass.new() \n n:setTestMember(42) \n print(\"test_member is: \" .. n:getTestMember()");
return 0;
}
The documentation here does not appear to say anything about this error. I'm not sure what class_name even is. Any help is appreciated.
By the way, I'm using GCC 4.9.2 to compile it.
So this is late but hopefully it will help if anyone else runs across a similar issue. Basically your example is missing an important but subtle part that will help explain why you got the link errors you got.
TestClass.hpp
class TestClass
{
int test_member;
public:
void setTestMember(int x) { test_member = x; }
int getTestMember() const { return test_member; }
static void aStaticMember() { }
};
OOLUA_PROXY(TestClass)
OOLUA_MEM_FUNC(void, setTestMember, int)
OOLUA_MEM_FUNC_CONST(int, getTestMember)
OOLUA_SFUNC(aStaticMember)
OOLUA_PROXY_END
TestClass.cpp
OOLUA_EXPORT_FUNCTIONS(TestClass
,setTestMember
)
OOLUA_EXPORT_FUNCTIONS_CONST(TestClass
,getTestMember
)
You must always put a OOLUA_EXPORT_FUNCTIONS block in the associated .cpp file so that the declarations from the OOLUA_PROXY block are defined. Even if you only have const member functions, you must still place an empty block e.g. OOLUA_EXPORT_FUNCTIONS(TestClass) in the .cpp file. In the event that your class only has static functions, you would be required to use a slightly different setup.
In summary:
OOLUA_PROXY declares a proxy class
OOLUA_EXPORT_FUNCTIONS blocks define the members of that class
Also, if your class has only static member functions exposed, you will need to include a OOLUA_EXPORT_NO_FUNCTIONS(TestClass) in the .cpp file. For every static member you must then use special syntax for registering the static functions with the Script.
using namespace OOLUA; //NOLINT(build/namespaces)
Script vm;
vm.register_class_static<TestClass>("aStaticMember",
&OOLUA::Proxy_class<TestClass>::aStaticMember);
The documentation is not very helpful in this matter, but if you reveiw the associated test source files, they in combination with the documentation are enough to get past most issues.
It would be better posting questions about the library to the mailing list oolua.org/mailinglist
Have a look at the documentation for the library, specifically exporting class functions[1]. Personally I would use the minimalist DSL for your functions instead of the expressive, this would then make your proxied functions like the following (however the get should really be a constant function):
OOLUA_MFUNC(setTestMember)
OOLUA_MFUNC(getTestMember)
[1] https://docs.oolua.org/_o_o_lua_proxying.html
I need to send reference a QPlainTextEdit to my C++ class Analizador for add lines to QPlainTextEdit from my class. I add the include <QPlainTextEdit> to the class, create the QPlainTextEdit from the graphic interface and call the constructor function just like that
Analizador *anal=new Analizador(ui->textProgres);
the constructor function is:
Analizador(QPlainTextEdit* text);
the compiler throw the error :
mainwindow.cpp:23: error: undefined reference to
`Analizador::Analizador(QPlainTextEdit*)'
so I guess the error is because I'm not sending a pointer to the constructor function but I don't know how to access the pointers of QPlainTextEdit
PS. I'm new in Qt and C++
In this case, compiler complains that it cannot find the definition of Analizador constructor when it tries to link your application.
Make sure you have written the definition of Analizador::Analizador(QPlainTextEdit*) constructor.
If you have written the constructor but still you gets this issue, The cpp file where your constructor exists may not have got compiled. If you are using QtCreator, try Build -> Run QMake and then Build -> Rebuild All
You can try this workaround.
#ifndef ANALIZADOR_H
#define ANALIZADOR_H
#include <QPlainTextEdit>
class Analizador
{
public:
Analizador(QPlainTextEdit *text)
{
plainTextEdit = text;
}
void addLines(QString line)
{
plainTextEdit->appendPlainText(line);
}
private:
QPlainTextEdit *plainTextEdit;
};
#endif // ANALIZADOR_H
And use this class like this.
analizador = new Analizador(ui->plainTextEdit);
analizador->addLines("Hello");
analizador->addLines("World");
I am having trouble passing a template class as a parameter to a another function.
I am using VS2012 c++/cli on a Windows 8.1 machine compiling for x64.
The compiler keeps telling me:
void Channel::TestFunc(SynchQueue<T> *)' : overloaded member function not found in 'Channel'
SynchQueue is a template class for a multi-threaded queue. I created it with another class I will call Images.
In my main.cpp, I have:
QPtr = new SynchQueue<Images>;
Also in main.cpp, I created a class called WorkerThread to which I passed QPtr.
No problems with that.
Now I want WorkerThread to pass QPtr to another class that is instantiated in WorkerThread.
So I defined the function as:
Channel.h
public ref class Channel
{
public:
// other definition stuff
void TestFunc(SynchQueue<Images> *tQPtr);
}
Channel.cpp
void Channel::TestFunc(SynchQueue<Images> *tQPtr)
{
int x;
x++;
}
I keep getting the error above. What am I doing wrong?
Any help appreciated.
You need:
template<typename T>
void TestFunc(SynchQueue<T> *tQPtr);
I'm currently getting started in C++. For the homework I'm currently doing, I have to define a number of classes in one header file. I'm not sure If I'm doing this right. Here is sample of what I'm trying to do.
//classOne.h
class classOne{
public:
classOne();
~classOne();
class classInsideClass{
public:
classInsideClass
void hello();
void print();
};
}
(I have skipped some code in this sample, like constructor for classOne)
//classOne.cpp
classOne::classInsideClass::classInsideClass(){}
classOne::classInsideClass::hello(){
cout << ""Hello <<endl;
}
//main.cpp
classOne callingClass;
callingClass.classInsideClass.hello;
I have defined a class inside classOne's header file. And I have created the functions for the this classInsideClass, inside the classOne's cpp. Is this the right way of saying, classInsideClass belongs to classOne, or am I not allowed to do this?
Am I calling the functions of classInsideClass correctly in main.cpp? When I try to run this, I get following error;
error:invalid use of 'class classOne::classInsideClass
If I don't try and call a function of classInsideClass in main.cpp, it complies fine.
Thanks in advance.
classInsideClass is a type inside of classOne, not an object. If you want to call classInsideClass::hello(), you need an actual instance of classInsideClass:
classOne::classInsideClass callingClass;
callingClass.hello();
//classOne.h
...
class classInsideClass{
public:
classInsideClass();
void hello();
};
Here you've missed the parentehsis at the end of the constructor definition.
void classOne::classInsideClass::hello(){
cout << "Hello" <<endl;
}
Here the function return type is missing and the quote marks are supposed to be around the string.
Good luck!
Try:
//main.cpp
classOne::classInsideClass internalClassObject;
internalClassObject.hello();