My question is: why does VC emit warning 4365 for only one of the commented lines below, and not the other?
#pragma warning(1: 4365)
void test1(const unsigned short) {}
unsigned short test2() { return 0; }
int main()
{
const unsigned short a = 0;
const unsigned short b = 0;
test1(a + b); // This line gives no warning
test1(test2() + b); // This line gives C4365
return 0;
}
Tested under VS2010 and VS2012 Express.
For reference, the full warning text is this:
warning C4365: 'argument' : conversion from 'int' to 'const unsigned short', signed/unsigned mismatch
Using Clang 3.3 (through Clang-Win32 and ClangVSx), no warnings are reported in this code (except of course the unknown pragma).
Related
I have my project set to treat compiler warnings as errors and this seemingly trivial lambda capture is proving to be a head scratcher of an issue for me:
#pragma warning(default : 4626)
struct B{
B& operator=(const B&) = delete;
void f(){
auto l = [this](){ // <== compiler warning
/*body*/
};
}
};
int main(){
return 0;
}
The code above produces the following warning in MSVC 2019 (16.2.5):
warning C4626: 'B::f::<lambda_1>': assignment operator was implicitly defined as deleted
The warning is emitted for the lambda that captures this (and if I don't capture the this it compiles with no warnings).
Could someone explain to me the reason for the warning and if there's a way to write the code such that it compiles cleanly?
I understand that lambdas' assignment operators are implicitly deleted but not sure how (if) is that relevant to the warning I'm getting.
iam in a similar situation and reproduced the warning like this:
#pragma warning(default : 4626)
struct A
{
A& operator=(const A&) = delete;
};
struct B : public A
{
//B& operator=(const B&) = delete;
void f(){
auto l = [](){ // <== compiler warning
/*body*/
};
}
};
int main(){
return 0;
}
Compiled with /std:c++latest /W4
in my case it makes no difference if you capture this or not.
Is you class Inheriting from another class with a deleted operator= ?
I avoid that warning by explicitly deleting the operator= in my case.
This warning occured in MVS 19 16.3.0 compiling with /latest. with /std:c++17 there is no such warning.
EDIT
this warning also happens as soon something captured in the lamdba is used (captured by copy or ref doesnt matter):
#pragma warning(default : 4626)
int main()
{
int foo;
auto bar = [&]() {
foo;
};
return 0;
}
<source>(8): warning C4626: 'main::<lambda_1>': assignment operator was implicitly defined as deleted
(with /std:c++latest /W4)
I would try to remove a specific character from a given string in the following code.
int main(void){
string query="a*de*da";
string org;
uint8_t rmc='*';
std::vector<string::const_iterator> wpos;
for(string::const_iterator itr = org.begin();
itr!=org.end();
++itr){
if(*itr==rmc){
wpos.push_back(itr);
}
}
uint64_t wcnt=0;
for(auto witr: wpos){
org.erase( witr-(wcnt++) );
}
query=org;
return 0;
}
In this code, I would expect that query="adeda" however, I got an error
error: no matching function for call to ‘std::basic_string<char>::erase(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >)’
org.erase(witr-wcnt);
My experimental setting is g++ 4.9.2 of devtoolset-3 on CentOS6.7
From C++98 to C++11, the signature of std::string::erase changed from
iterator erase(iterator p)
to
iterator erase(const_iterator p)
It seems like g++4.9.2 still uses the old version. Your example should compile if you change string::const_iterator to string::iterator.
i have recently started using threads ,so pretty new to this stuff. please no hate .i was working on a program which would calculate prime numbers till a limit N provided through the command line.
i would distribute this N to num_threads i.e. 4 .lets say the number is 40 and num_threads=4 then each thread should get 10 indexes.i created a structure containing the upper and lower limits for each thread.i passed a pointer of this structure and used the "high" and "low" attributes of this structure to control the flow.
there seems to be some kind of problem.if anyone of you can point out the mistake or tell me if this is the right thing to do i would be pretty grateful :)
geany gives the the following errors:
g++ -Wall -o "prime5000" "prime5000.cpp" (in directory: /home/jarrar/operating systems/threads)
/tmp/ccAHGsIw.o: In function `main':
prime5000.cpp:(.text+0xc9): undefined reference to `pthread_create'
prime5000.cpp:(.text+0xf6): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Compilation failed.
if executed from the terminal using the following command:
g++ prime5000.cpp -lpthread -o 5prime
./5prime
it will give :
Segmentation fault(Core dump)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define num_thread 4
void* calc(void* param);
struct fml
{
long low;
long high;
};
fml* x;
int main(int argc,char* argv[])
{
pthread_t threadArray[num_thread];
int thread;
for(thread=0;thread<num_thread;thread++)
{
x->low=thread*atoi(argv[1])/num_thread;
x->high=x->low+(thread+1)*atoi(argv[1])/num_thread;
pthread_create(&threadArray[thread],NULL,calc,&x);
}
int i;
for(i=0;i<num_thread;i++)
{
pthread_join(threadArray[i],NULL);
}
return 0;
}
void* calc(void* param)
{
fml* temp=(fml*)param;
long lowerLimit=temp->low;
long upperLimit=temp->high;
int i;
int j;
for(i=lowerLimit;i<upperLimit;i++)
{
for(j=2;j<i;j++)
{ if(i!=1||i!=0)
{
if(!(i%j==0))
{
printf("%d ",i);
}
}
}
}
pthread_exit(NULL);
}
#include<stdio.h>
#include<math.h>
#include<pthread.h>
#include<stdlib.h>
long double x,fact[150],pwr[150],s[1];
int i,term;
void *Power(void *temp)
{
int k;
for(k=0;k<150;k++)
{
pwr[k] = pow(x,k);
//printf("%.2Lf\n",pwr[k]);
}
return pwr;
}
void *Fact(void *temp)
{
long double f;
int j;
fact[0] = 1.0;
for(term=1;term<150;term++)
{
f = 1.0;
for(j=term;j>0;j--)
f = f * j;
fact[term] = f;
//printf("%.2Lf\n",fact[term]);
}
return fact;
}
void *Exp(void *temp)
{
int t;
s[0] = 0;
for(t=0;t<150;t++)
s[0] = s[0] + (pwr[t] / fact[t]);
return s;
}
int main(void)
{
pthread_t thread1,thread2,thread3;
printf("Enter the value of x (between 0 to 100) (for calculating exp(x)) : ");
scanf("%Lf",&x);
printf("\nThreads creating.....\n");
pthread_create(&thread1,NULL,Power,NULL); //calling power function
pthread_create(&thread2,NULL,Fact,NULL); //calling factorial function
printf("Threads created\n");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf("Master thread and terminated threads are joining\n");
printf("Result collected in Master thread\n");
pthread_create(&thread3,NULL,Exp,NULL);
pthread_join(thread3,NULL);
printf("\nValue of exp(%.2Lf) is : %Lf\n\n",x,s[0]);
exit(1);
}
I was trying to run the above program in linux ubuntu. It is giving following errors
parallelcomp.cpp:(.text+0x1ec): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x207): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x222): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x233): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x262): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x273): undefined reference to `pthread_join'
The error is mostprobably due to linking binary with pthreads.
Is there any command in ubuntu terminal whcih can solve this problem.?
I have tried with several commands given in this community forum, non of them is helpful.
Is there anyone who would like to help me?
I am also very new to Linux ubuntu.
Any kind of suggestion is appreciable.
How to include libpthread ?
When I am putting the following command, gcc -pthread -o term term.c, in terminalwe i get the following error: Command line option 'p' [from -pthread] is not known.
Please try the following -lpthread. Which version of gcc are you using?
Why do I get "undefined reference" errors even when I include the right header files?
While compiling add link to pthread library -lpthread
What does that warning says?
I could not find any typos or extra characters in the code.:
if (!(connectedComp = (Vertex**)malloc(sizeof(Vertex*)))) {
AllocationError();
}
if (!(created = (unsigned int*)malloc(sizeof(unsigned int)))) {
AllocationError();
}
connectedComp[++counter] = CreateVertex(id, edgesMatrix, maxValue, created);
I declared
Vertex** connectedComp = NULL;
and the function signature is:
Vertex* CreateVertex(unsigned int id, unsigned int** edgesMatrix, unsigned int maxValue);
Thanks in advance,
Function prototype of CreateVertex has 3 arguments and you are trying to send 4. That is the warning
Well your function signature is
Vertex* CreateVertex(unsigned int id, unsigned int** edgesMatrix, unsigned int maxValue);
And you are passing 4 arguments. This is the problem and hence compiler is giving the following compilation error.
connectedComp[++counter] = CreateVertex(id, edgesMatrix, maxValue, created);
By looking your code your call should be
connectedComp[++counter] = CreateVertex(id, edgesMatrix, maxValue);
BTW, you should avoid using malloc/free in C++ instead of that use new/delete or smart_pointer mechanism.