I am using vs2012 and having the same problem. below is my sample source code.
#include "stdafx.h"
#include "iostream"
#include "list"
#include "algorithm"
#include "xutility"
using namespace std;
typedef struct Student{
string name;
int age;
int operator==(list<Student>::iterator itr)
{
return (!strcmp(itr->name.c_str(),this->name.c_str()));
}
}SStudent;
int _tmain(int argc, _TCHAR* argv[])
{
list<SStudent> myList;
SStudent temp1;
temp1.age = 10;
temp1.name = "aaaa";
myList.push_back(temp1);
list<SStudent>::iterator itr;
itr = std::find(myList.begin(), myList.end(), "aaaa");
return 0;
}
here is the error:
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Student' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3186
I have a C++11 program that gives me this error:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Code:
const int popSize=100;
void initializePop(mt3dSet mt3dPop[], int index1, int index2, std::string ssmName, std::string s1, std::string s2, std::string s3, std::string s4, std::string mt3dnam, std::string obf1, int iNSP, int iNRM, int iNCM, int iNLY, int iOPT, int iNPS, int iNWL, int iNRO, int ssmPosition, int obsPosition ){
if((index1 >= index2)||index1<0||index2>popSize){
std::cout<<"\nInitializing population...\nIndex not valid..\nQuitting...\n";
exit(1);
}
for(int i=index1; i<index2; i++){
mt3dPop[i].setSSM(ssmName, iNSP, iNRM, iNCM, iNLY);
mt3dPop[i].setNam(toString(s1,s3,i));
mt3dPop[i].setObsName(toString(s1,s4,i));
mt3dPop[i].setSsmName(toString(s1,s2,i));
mt3dPop[i].getSSM().generateFl(toString(s1,s2,i),iOPT,iNPS);
mt3dPop[i].generateNam(mt3dnam, ssmPosition, obsPosition);
mt3dPop[i].setFitness(obf1, iNWL, iNRO);
}}
void runPackage(ifstream& inFile){
//all variables/function parameters for function call are read from inFile
unsigned int numThreads = std::thread::hardware_concurrency();// =4 in my computer
std::vector<std::thread> vt(numThreads-1);//three threads
for(int j=0; j<numThreads-1; j++){
vt[j]= std::thread(initializePop,mt3dPop,j*popSize/numThreads, (j+1)*popSize/numThreads, ssmName, s1,s2, s3, s4, mt3dnam,obf1,iNSP, iNRM, iNCM, iNLY, iOPT, iNPS, iNWL, iNRO, ssmPosition, obsPosition ); //0-24 in thread 1, 25-49 in thread 2, 50-74 in thread 3
}
//remaining 75 to 99 in main thread
initializePop(mt3dPop,(numThreads-1)*popSize/numThreads, popSize, ssmName, s1,s2, s3, s4, mt3dnam,obf1,iNSP, iNRM, iNCM, iNLY, iOPT, iNPS, iNWL, iNRO, ssmPosition, obsPosition);
for(int j=0; j<numThreads-1; j++){
vt[j].join();
}}
What does the error mean and how do I fix it?
You need to link correctly, and compile with -std=c++11 - see this example.
I'm guessing you had the same problem as me! (I compiled with -pthread and -std=c++11 rather than linking with those two. (But you will need to compile with std=c++11 as well as linking with it.))
Probably you want to do something like this:
g++ -c <input_files> -std=c++11
then
g++ -o a.out <input_files> -std=c++11 -pthread
... at least I think that's right. (Someone to confirm?)
How to reproduce these errors:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
void task1(std::string msg){
cout << "task1 says: " << msg;
}
int main() {
std::thread t1(task1, "hello");
t1.join();
return 0;
}
Compile and run:
el#defiant ~/foo4/39_threading $ g++ -o s s.cpp
s.cpp: In function ‘int main()’:
s.cpp:9:3: error: ‘thread’ is not a member of ‘std’
s.cpp:9:15: error: expected ‘;’ before ‘t1’
You forgot to #include <thread>, include it and try again:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <thread>
using namespace std;
void task1(std::string msg){
cout << "task1 says: " << msg;
}
int main() {
std::thread t1(task1, "hello");
t1.join();
return 0;
}
Compile and run:
el#defiant ~/foo4/39_threading $ g++ -o s s.cpp -std=c++11
el#defiant ~/foo4/39_threading $ ./s
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
More errors, as you defined above, because you didn't specify -pthread in the compile:
el#defiant ~/foo4/39_threading $ g++ -o s s.cpp -pthread -std=c++11
el#defiant ~/foo4/39_threading $ ./s
task1 says: hello
Now it works.
Consider my test code:
#include <thread>
class Foo {
public:
void threadFunc() {}
void startThread() {
_th = std::thread(&Foo::threadFunc, *this);
}
private:
std::thread _th;
};
int main(int argc, char *argv[])
{
Foo f;
f.startThread();
return 0;
}
This is an error it produces:
../untitled/main.cpp:13:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char *argv[])
^
../untitled/main.cpp:13:26: warning: unused parameter 'argv' [-Wunused-parameter]
int main(int argc, char *argv[])
^
In file included from ../untitled/main.cpp:1:
In file included from /usr/bin/../lib/c++/v1/thread:90:
In file included from /usr/bin/../lib/c++/v1/__functional_base:15:
/usr/bin/../lib/c++/v1/type_traits:1372:12: error: call to implicitly-deleted copy constructor of 'typename decay<Foo &>::type' (aka 'Foo')
return _VSTD::forward<_Tp>(__t);
^~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/__config:273:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/usr/bin/../lib/c++/v1/thread:351:33: note: in instantiation of function template specialization 'std::__1::__decay_copy<Foo &>' requested here
__decay_copy(_VSTD::forward<_Args>(__args))...));
^
../untitled/main.cpp:7:15: note: in instantiation of function template specialization 'std::__1::thread::thread<void (Foo::*)(), Foo &, void>' requested here
_th = std::thread(&Foo::threadFunc, *this);
^
../untitled/main.cpp:10:17: note: copy constructor of 'Foo' is implicitly deleted because field '_th' has an inaccessible copy constructor
std::thread _th;
^
And if I create a thread like this: _th = std::thread(&Foo::threadFunc, std::ref(*this));
I get:
../untitled/main.cpp:13:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char *argv[])
^
../untitled/main.cpp:13:26: warning: unused parameter 'argv' [-Wunused-parameter]
int main(int argc, char *argv[])
^
In file included from ../untitled/main.cpp:1:
/usr/bin/../lib/c++/v1/thread:330:5: error: attempt to use a deleted function
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
^
/usr/bin/../lib/c++/v1/thread:340:5: note: in instantiation of function template specialization 'std::__1::__threaad_execute<void (Foo::*)(), std::__1::reference_wrapper<Foo> , 1>' requested here
__threaad_execute(*__p, _Index());
^
/usr/bin/../lib/c++/v1/thread:352:41: note: in instantiation of function template specialization 'std::__1::__thread_proxy<std::__1::tuple<void (Foo::*)(), std::__1::reference_wrapper<Foo> > >' requested here
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
^
../untitled/main.cpp:7:15: note: in instantiation of function template specialization 'std::__1::thread::thread<void (Foo::*)(), std::__1::reference_wrapper<Foo> , void>' requested here
_th = std::thread(&Foo::threadFunc, std::ref(*this));
^
/usr/bin/../lib/c++/v1/type_traits:833:5: note: function has been explicitly marked deleted here
~__nat() = delete;
^
What am I doing wrong? I don't have such problem on Windows with VS2012. I also didn't have this problem with default stdlib implementation on Mac, but now I have to use libc++.
My compiler flags:
-std=c++11 -mmacosx-version-min=10.7 -stdlib=libc++
_th = std::thread(&Foo::threadFunc, *this);
This tries to make a copy of *this to store in the new thread object, but your type is not copyable because its member _th is not copyable.
You probably want to store a pointer to the object, not a copy of the object:
_th = std::thread(&Foo::threadFunc, this);
N.B. your program will terminate because you do not join the thread. In your type's destructor you should do something like:
~Foo() { if (_th.joinable()) _th.join(); }
please help me, i want to implement timer using c in ubunto. i have the written the code but it is giving two errors. I am compiling it using -lrt option of gcc.
Errors i am getting is:
timer1.c: In function ‘main’:
timer1.c:18:52: error: ‘SIG’ undeclared (first use in this function)
timer1.c:18:52: note: each undeclared identifier is reported only once for each function it appears in
timer1.c:21:23: error: ‘handler’ undeclared (first use in this function)
My code is:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
timer_t timerid;
int main(int argc, char *argv[])
{
struct sigevent sev;
struct itimerspec its;
long long freq_nanosecs;
sigset_t mask;
struct sigaction sa;
printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
printf("timer ID is 0x%lx\n", (long) timerid);
// timer_create(CLOCKID, &sev, &timerid);
/* Start the timer */
its.it_value.tv_sec = 1000;
its.it_value.tv_nsec =0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
timer_settime(timerid,0, &its, NULL);
sleep(10);
}
static void handler(int sig, siginfo_t *si, void *uc)
{
if(si->si_value.sival_ptr != &timerid)
{
printf("Stray signal\n");
}
else
{
printf("Caught signal from timer\n");
}
}
SIG is undeclared because you never declare it, and we can't tell you how to fix it since we don't know what it's supposed to be. handler is undeclared because you forgot the forward declaration. Put a copy of the function signature followed by a semicolon before the function where it's used.
static void handler(int sig, siginfo_t *si, void *uc);
int main(int argc, char *argv[])
{
...
I'm trying to compile a network application on Ubuntu 12.04 using GCC and glibc 2.15
Let's consider the following code as the example:
a.cpp:
#include <sys/select.h>
void func ()
{
int fd;
fd_set fds;
FD_SET(fd, &fds);
}
I can successfully compile these lines with the command "gcc -c -Wsign-conversion a.cpp", but I have the following warning after I add either -O1 or -O2 option:
gcc -c -O1 -Wsign-conversion a.cpp
a.cpp:6: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
I have the warning for both gcc 4.4, 4.5 and 4.6.
UPD: If I understand correctly, my example strictly conforms to FD_SET semantics, so I should have no warnings in this case.
What's the reason of this? How can I avoid it?
Thanks.
UPD: Looks like it's the known issue now - http://comments.gmane.org/gmane.comp.lib.glibc.alpha/22344 . But I can't understand what should I do with it on GLIBC 2.15? Just wait for the next GLIBC?
To fix this problem, I used a separate wrapper source file, and disabled warnings for just that source file.
CMacroWrapper.h:
#include <sys/select.h>
namespace CMacroWrapper
{
void FdSet(int fd, fd_set *set);
}
CMacroWrapper.cpp:
#include "CMacroWrapper.h"
#pragma GCC diagnostic ignored "-Wsign-conversion"
void CMacroWrapper::FdSet(int fd, fd_set *set)
{
FD_SET(fd, set);
}
main.cpp:
#include "CMacroWrapper.h"
void func ()
{
int fd;
fd_set fds;
FD_ZERO(&fds);
CMacroWrapper::FdSet(fd, &fds);
}
I find that CMacroWrapper namespace useful to just add any new system header macros that pop up in compiler warnings. It does add an extra function call, but it's worth it for the warning peace-of-mind. Or you can wrap all the FD_ macros for consistency, if you like.