Passing a pointer to template class as a parameter - templates

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);

Related

pre-defining classes not working when using pointers to classes

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.

How to declare a new class in the header and define it in the source file without this errors?

I'm on the situation where I want to create a new class and then use it in another created class (C++), but without using different header or source files: both classes shall be in the same place, one way or another. The main class shall contain only a pointer to the "child" class.
Now I know that in many cases is perfectly possible to define a class in the header file. In fact, if one wants to not just set a pointer to that "child class", but actually use one of its methods already in the header file (e.g., for inline methods), one would actually have to define it in the source file:
class ChildClass
{
public:
bool myFunctions() { return true; }
}
class MainClass
{
private:
ChildClass* poChildClass;
inline bool getResult() { return poChildClass->myFunctions(); }
}
But let's suppose I want just to have that pointer there, without any call to my ChildClass' methods, so I should be able to only declare the ChildClass and later define it in the same .cpp file as MainClass is defined:
//in .hpp
class ChildClass;
class MainClass
{
private:
ChildClass* poChildClass;
}
//in .cpp
class ChildClass
{
public:
bool myFunctions() { return true; }
}
//etc.
In a first moment I don't know what could be there of a problem. But in trying to do so with one of my classes in particular (which is based on Qwt's QwtPlotPicker class), I get some compile errors (in this last version):
error: undefined reference to `vtable for Picker'
The error points out where in the following code (in the .cpp):
class Picker : public QwtPlotPicker
{
Q_OBJECT
public:
Picker( QWidget* canvas ) :
QwtPlotPicker( canvas ) //Here lies the error the compiler says
//...
So what is the problem? What do I get this "undefined reference to 'vtable'" problem?
Thanks for any help,
Momergil
This is a problem I have had forever when using QT. Any class that has the Q_OBJECT macro in it MUST be listed in the HEADERS before running qmake (as far as I can tell). This may even mean putting the .cpp file in the HEADERS section.

Starting thread in multifile project

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))

Templated partial application call issues

For the upcoming Coursework in January, I started developing a small DirectX engine. To find out if there is a performance increase I wanted to try to not use any virtuals at all (I know that virtuals aren't all bad, but I just wanted to see if it is possible without them).
When I started playing around with a simple StateManager, it got hard to avoid virtuals, but this is my current approach:
#include <boost\function.hpp>
#include <boost\bind.hpp>
template <class Derived>
struct TBaseState {
bool update(float delta) {
return static_cast<Derived *>(this)->update(delta);
};
};
struct CTestState : TBaseState<CTestState> {
bool update(float delta) {
return true;
}
};
class StateManager
{
public:
template <class StateClass> static void setState(StateClass nextState)
{
m_funcptrUpdate = boost::bind(&TBaseState<StateClass>::update, boost::ref(nextState), _1);
}
static bool update(float delta)
{
return m_funcptrUpdate(delta);
}
protected:
private:
static boost::function<bool (float)> m_funcptrUpdate;
};
Visual Studio 2010's Intellisense seems to think everything is fine, but when I want to compile the program and test the StateManager with a very basic approach:
CTestState* t = new CTestState();
StateManager::setState(*t);
StateManager::update(0.0f);
The following error is thrown during linking phase:
error LNK2001: unresolved external symbol "private: static class boost::function<bool __cdecl(float)> StateManager::m_funcptrUpdate" (?m_funcptrUpdate#StateManager##0V?$function#$$A6A_NM#Z#boost##A)
So obviously he can't find the binded function, but how can I solve this issue? I get similar errors if I use boost::bind directly to some class. Since I am a computer science student I would also be interested in some insight or approaches without boost (e.g. bind1st, ...).
EDIT:
I was also thinking about using C++11 Variadic Templates, but one of the coursework requirements is to stick to VS2012.
Static class members need to be given storage. They're like extern variables. Add a definition to one of your .cpp files, outside of the class definition:
boost::function<bool (float)> StateManager::m_funcptrUpdate;
Also, in this code:
template <class StateClass> static void setState(StateClass nextState)
{
m_funcptrUpdate = boost::bind(&TBaseState<StateClass>::update,
boost::ref(nextState), _1);
}
You're maintaining storing a reference to the local variable nextState. That reference will be invalid after setState returns.

C++ accessing a function from in a class, receiving functions as a parameter

i have two questions that are fairly small and related so i will put them both in the same question.
i have been experimenting with classes and i was attempting to access a class in another file that wasn't in a class so for example.
//class 1 .cpp
void Class1::function1()//another error
{
function()
}
//main.cpp
void function()
{
//stuff happens
}
is there a way to-do this? or would i need to add this function to a class to get it to work. also how would you go about creating a function that receives a function as it parimetre? for example function(function2())
i am simply trying to access a function from a class as it would make my code easier to use later if the function that i am using doesn't get added to a class. with regards to the seconds question i which to create a function that receives a time and a function as an argument. it will wait for the specified time then execute the program
How to access a function in another file?
Depends on the type of function, there can be to cases:
1. Accessing class member functions in another file(Translation Unit):
Obviously, you need to include the header file, which has the class declaration in your caller translation unit.
Example code:
//MyClass.h
class MyClass
{
//Note that access specifier
public:
void doSomething()
{
//Do something meaningful here
}
};
#include"MyClass.h" //Include the header here
//Your another cpp file
int main()
{
MyClass obj;
obj.doSomething();
return 0;
}
2. Accessing free functions in another file(Translation Unit):
You do not need to include the function in any class, just include the header file which declares the function and then use it in your translation unit.
Example Code:
//YourInclude.h
inline void doSomething() //See why inline in #Ben Voight's comments
{
//Something that is interesting hopefully
}
//Your another file
#include"YourInclude.h"
int main()
{
doSomething()
return 0;
}
Another case as pointed out by #Ben in comments can be:
A declaration in the header file, followed by a definition in just one translation unit
Example Code:
//Yourinclude File
void doSomething(); //declares the function
//Your another file
include"Yourinclude"
void doSomething() //Defines the function
{
//Something interesting
}
int main()
{
doSomething();
return 0;
}
Alternately, a messy way to do this can be to just mark the function as extern in your another file and use the function.Not recommended but a possibility so here is how:
Example Code:
extern void doSomething();
int main()
{
doSomething();
return 0;
}
How would you go about creating a function that receives a function as it parameter?
By using function pointers
In a nutshell Function pointers are nothing but pointers but ones which hold address of functions.
Example Code:
int someFunction(int i)
{
//some functionality
}
int (*myfunc)(int) = &someFunction;
void doSomething(myfunc *ptr)
{
(*ptr)(10); //Calls the pointed function
}
You need a prototype for the function you want to call. A class body contains prototypes for all its member functions, but standalone functions can also have prototypes. Typically you organize these in a header file, included from both the file which contains the function implementation (so the compiler can check the signature) and in any files which wish to call the function.
(1) How can the `class` function be accessible ?
You need to declare the class body in a header file and #include that wherever needed. For example,
//class.h
class Class1 {
public: void function1 (); // define this function in class.cpp
};
Now #include this into main.cpp
#include"class.h"
You can use function1 inside main.cpp.
(2) How to pass a function of class as parameter to another function ?
You can use pointer to class member functions.