Error while trying to create simple thread in c++ [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Im trying to create a simple thread and have it execute.
My function definition is:
void MyClass::myFunction()
{
//Do Work
}
I'm creating the thread and executing it:
std::thread t1(myFunction);
Upon compiling my code, i get the following error:
error C3867: function call missing argument list; use '&MyClass::myfunction' to create a pointer to member.
Since my function does not take any parameters, i'm assuming i'm declaring it wrongly where i'm creating my thread? Any help will be appreciated, Thanks!!

If your method is a non-static member : You need an instance of your object to call the member function on.
If your method is static member, do what the compiler suggest : simply pass the address of your function.
Example:
class A
{
public:
void foo() { cout << "foo"; }
static void bar() { cout << "bar"; }
};
int main() {
std::thread t1(&A::foo, A()); // non static member
t1.join();
std::thread t2(&A::bar); // static member (the synthax suggested by the compiler)
t2.join();
return 0;
}

Related

C++ struct with class as member, struct not declared in this scope [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I declared a struct called ThreadParams and class Server and ThreadParams have member Server, inside class server there's a member function called dispatchThread (ThreadParams *params) that takes in a thread param. I'm not sure where to declare and use this struct, my server.cc and server.hh is below
class Server {
public:
struct ThreadParams {
const Server * server;
Socket_t sock;
};
void dispatchThread( ThreadParams * params);
};
below is where i used the method
#include "server.hh"
void dispatchThread( ThreadParams * params) {
// do some stuff
}
however I got
error: ‘ThreadParams’ was not declared in this scope
error: ‘params’ was not declared in this scope
Any help is greatly appreciated !
Try the function signature "dispatchThread" like this
void Server::ThreadParams::dispatchThread( ThreadParams * params) {
// do some stuff
}
Function dispatchThread should be visible from Server class.

out-of-line definition of 'tedt' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Sorry if my English won't be clear for you, I'm a Russian student.
I have a code of a class with function declarations:
Account.h
class Account {
public:
Account();
virtual ~Account() = 0;
void tedt(const std::string &);
Account(std::string);
}
Account.cpp:
#include "Account.h"
void Account::tedt(const std::string& a) <===== error here
{
return;
}
Account::Account()
{
Account(""); <==== some other error is here...
}
Account::Account(std::string input) <===== and here!!!
{
SetLogin(input);
SetProxy("");
}
I see this message:
error: out-of-line definition of 'tedt' does not match any declaration in 'Account'
end this
error: out-of-line definition of 'Account' does not match any declaration in 'Account' (about Account::Account(std::string input))
And I don't know to do. I'm using qt creator for coding if it is important
As pointed out, you need to put a semicolon after the closing bracket of your class definition.
Please also note that your constructor without arguments will not work as expected here. Instead, you create a new temporary object with the argument. This will not change your current object. Better use:
Account::Account() : Account("") {}
, which works as expected.

how can I bind a class member function with param as rvalue to boost::function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I try to bind a class member function with param as rval to boost::function.
But it doesn't work.
my sample false code :
class Class1
{
int Foo1(int&& b)
{
return b;
}
void foo2()
{
boost::function<int(int&&)> fc(boost::bind(&Class1::Foo1, this, _1)
}
};
Use a lambda expression:
boost::function<int(int&&)> fc = [this](int&& x)
{
return Foo1(x);
};

My program is stopping [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is my class,and my problem is when I call the constructor
Eveniment e1(1,m1);
with parameters in main method, my program is stopping and I don't know why. M1 is an object of IntrareCAlendar.
class Eveniment{
private:
const int id;
IntrareCalendar data;
char* detalii;
int static nrIntrari;
public:
Eveniment(int nr,IntrareCalendar ic) :id(nr){
this->data = ic;
nrIntrari++;
}
~Eveniment(){
if (this->detalii != NULL)
delete[]this->detalii;
}
};
What should I do? thanks a lot!
You never set detalii to anything valid. It remains uninitialised - it is not initialised to a particular value automatically. You could set it to nullptr in your constructor. (Don't use NULL in C++.)
Your destructor calls delete[] on that member, but no new[] has been called prior to that. As such the behaviour of your program is undefined.
Also, consider using static std::atomic<int> as the type for nrIntarari in case multiple threads instantiate an Eveniment.

C++, calling a non-member function from a member function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It is possible somehow to a call a non-member function from a member function?
Something like this:
class A {
int a;
public:
bool (int b) {
smt;
lala = define_smt(smt);
if (lala < 5) {
return true;
}
else {return false}
}
}
int define_smt(smt){ ...}
Thnaks in advance
The function must be known at call time. That means you must declare it before you use it. You have basically two choices here: Either put the whole function before the class, or use a function prototype before the class. So either:
int define_smt(smt) { ... }
class A {
// ...
}
or:
int define_smt(smt);
class A {
// ...
}
int define_smt(smt) { ... }
Of course It is Possible. As long as your function is available in the current context. i.e. either a prototype or a defination should be available, before the point of calling.