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.
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 have some 15-year-old C++ code that I am trying to bring up to more modern times. At this stage, I'm trying to get code that compiled with Visual C++ 6.0 to now compile with VS 2003 (Microsoft Visual C++ .NET 69462-335-0000007-18915). If we can get this to compile cleanly & run properly, then we can take another step to get it into a more recent version of VS. But I'm having a number of problems...
Here is a snippet of the (simplified) code:
class toS
{
public:
toS() { buff[0] ='\0'; }
operator LPCTSTR() { return buff; }
protected:
void Append (TCHAR c)
{
LPTSTR p = buff + _tcslen(buff);
*p++ = c;
*p = '\0';
}
TCHAR buff[40];
};
class LtoS : public toS
{
public:
LtoS(LONG n, TCHAR c = '\0')
{
_ltot(n, buff, 10);
Append(c);
}
};
void WriteBool(const CString& Section, const CString& Key, bool Value);
CString Section;
int nLine = 0;
std::vector<bool> *BoolVect;
std::vector<bool>::iterator vi;
...
for (vi = BoolVect->begin(); vi != BoolVect->end(); vi++)
WriteBool(Section, "LineVis " + LtoS(nLine++), *vi);
...
From this I get the following error message:
error C2677: binary '+' : no global operator found which takes type 'LtoS' (or there is no acceptable conversion)
Any idea how this code ever worked? If I can find out what it did in the past, I can begin to define the overloaded operator+ to match the functionality.
Compiler error goes away when I make class tos inherit from CString with:
class tos : public CString { ... }
Hopefully this will not only compile, but will execute correctly...
Deriving from several of the comments, try adding a public conversion operator to class toS as follows:
operator LPCTSTR() const { return &buff[0]; }
You may need to explicitly construct the string in the for loop as well, e.g.:
WriteBool(Section, CString("LineVis ") + static_cast<LPCTSTR>(LtoS(nLine++)), *vi);
(Side note: As you probably know since you just extracted code for an example, there's a problem here:
std::vector<bool> BoolVect;
...
for (vi = BoolVect->begin(); vi != BoolVect->end(); vi++)
The notation you're using to access the BoolVect implies that it is a pointer, but it's not being declared as such in your example.)
I'm using asn1c to generate C++ encoder/decoder codes. There is a problem in the generated code (which is a huge code) preventing it from compile on VC++ which I try to make it simple here:
There is A_SET_OF macro in the generated code defined as:
#define A_SET_OF(type) \
struct { \
type **array; \
int count; /* Meaningful size */ \
int size; /* Allocated size */ \
void (*free)(type *); \
}
This macro is later used in several parts of code. For instance:
A_SET_OF(struct MyStructure {
long myNumber;
char* myPointer;
} ) myList;
I get a C2226: syntax error : unexpected type error on these parts. To find what actually is causing the problem, I substituted the actual macro definition:
struct {
struct MyStructure { long myNumber; char* myPointer; } **array;
int count; /* Meaningful size */
int size; /* Allocated size */
void (*free)(struct MyStructure { long myNumber; char* myPointer; } *);
} myList;
The error is caused by the void(*free) line. Apparently, having struct declaration as a function parameter is a non-standard feature which VC++ do not support (and gcc probably supports as asn1c primary targets Linux).
I am looking for a workaround to this issue, preferably fixing the A_SET_OF macro definition as there are many references to it. typedefing the struct passed to free function might solve this issue but I am not sure how it could be done for these anonymous structures.
#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.