There is a class that contains some data and it sorts them at some point of time. I use qsort() and I'd like to keep the comparing function within the class as a method. The question is how to pass a method to qsort() so that the compiler (g++) don't throw any warnings?
Attempt 1:
int Data::compare_records(void * rec_1, void * rec_2){
// [...]
}
void Data::sort(){
qsort(records, count, sizeof(*records), &Data::compare_records);
}
This way generates an error:
error: cannot convert ‘int (Data::*)(const void*, const void*)’ to ‘int (*)(const void*, const void*)’ for argument ‘4’ to ‘void qsort(void*, size_t, size_t, int (*)(const void*, const void*))’
Attempt 2 :
void Data::sort(){
qsort(
records, count, sizeof(*records),
(int (*)(const void*, const void*)) &Data::compare_records
);
}
This way generates a warning:
warning: converting from ‘int (Data::*)(const void*, const void*)’ to ‘int (*)(const void*, const void*)’
How to do it the right way then?
If you must use qsort and not std::sort (recommended), declaring the member method as static should be enough.
You pass the function as &Data::compare_records, but you should pass it as Data::compare_records and also make it static
Don't use qsort in C++. Use std::sort and boost/std::bind. Member function-pointer cannot been converted to function-pointer. Your method should be static, or it should be free function.
see Is the type of “pointer-to-member-function” different from “pointer-to-function”? for an explanation.
This code may also help as a hint, for std::sort despite the fact I Use Qt's qSort()
Functors can be very cool.
struct randomWSort
{
SatoshiGame* This;
randomWSort(SatoshiGame* g){This=g;}
bool operator()(QString& a, QString& b)
{
return This->randomWSort(a,b);
}
};
bool SatoshiGame::randomWSort(QString& a, QString& b)
{
return rand->rnd() %2;
}
QString SatoshiGame::getRandomString(QStringList words)
{
qSort(words.begin(), words.end(), ::randomWSort(this) );
return words.at(0);
}
Related
a simple and I guess easy to answer question (if I did not already got it myself). The following overloaded functions:
void BR(const bool set) { backwardReaction_[nReac_] = set; }
bool BR(const int reactionNumber) const { return backwardReaction_[reactionNumber]; }
The first function is a setter and the second a getter function. backwardReaction_ is of type std::vector<bool>. The problem occurs whenever I want to call the second function. Here I get a compiler error overload function BR(xy) ambigious.
int main()
.
.
const int i = 3;
bool a = chem.BR(i);
The compiler error is equal to:
chemistryTestProg.cpp: In function ‘int main()’:
chemistryTestProg.cpp:74:34: error: call of overloaded ‘BR(const int&)’ is ambiguous
const bool a = chem.BR(i);
^
In file included from ../../src/gcc/lnInclude/chemistryCalc.hpp:38:0,
from ../../src/gcc/lnInclude/chemistry.hpp:38,
from chemistryTestProg.cpp:35:
../../src/gcc/lnInclude/chemistryData.hpp:266:18: note: candidate: void AFC::ChemistryData::BR(bool)
void BR(const bool);
^~
../../src/gcc/lnInclude/chemistryData.hpp:322:22: note: candidate: bool AFC::ChemistryData::BR(int) const
bool BR(const int) const;
^~
I guess that I get the problem because of the types bool and int which are identically (true => int(1), false => int(0). As I am changing the getter name to, e.g., bool getBR(const int reactionNumber) {...} everything works fine. So I guess the problem is about the similarities of the bool and int treatment within c++. I also tried a variety of different calls such as:
const bool a = chem.BR(4)
const bool a = chem.BR(int(5))
const bool a = chem.BR(static_cast<const int>(2))
bool a = chem.BR(...)
Thus, I think it is really related to the bool andint overloading arguments. Nevertheless, I made a quick search and did not find too much about these two overload types and resulting problems. Tobi
This is because you declared BR(int), but not BR(bool), to be const. Then when you call BR(int) on a non-const object, the compiler has two conflicting matching rules: parameter matching favours BR(int), but const-ness matching favours BR(bool).
I setup a class called tagToken.
It has setter functions, one is void setString(QString)
I have it defined/declared as such
.h file
void setString(QString string);
Private:
QString stringOfTagToken ;
.cpp file
void tagToken::setString(QString string)
{
stringOfTagToken = string;
}
When I try to run this code:
if (linePosition == 1)
{
QVector<tagToken> temp(0);
//errors
//temp.at(0).setString(line);
temp.at(0).setString("test");
//tags.at(0).setString(line);
//tags.push_back();
tagTokenCounter++;
}
I get this error:
C:\Dev\DiffMatchPatch\diffmatchpatch.cpp:316: error: passing 'const tagToken' as 'this' argument of 'void tagToken::setString(QString)' discards qualifiers [-fpermissive]
temp.at(0).setString("test");
QVector's at function returns data as const. Use at when you don't want to (accidentally) change the vector data, or operator[] in general.
temp[0].setString("test");
QVector::at() returns a const ref to your data, you cannot call a non-const method like setString on that
From http://qt-project.org/doc/qt-4.8/qvector.html#at
const T & QVector::at ( int i ) const
Returns the item at index position i in the vector.
i must be a valid index position in the vector (i.e., 0 <= i < size()).
Consider using operator[] instead
I'm trying to return a pointer to function without the use of typedef, but the compiler (gcc) is emitting a strange error, as if I could not do that kind of setting.
Remarks: With the use of typedef code works.
code:
void catch_and_return(void (*pf)(char*, char*, int&), char *name_one, char* name_two, int& number)(char*, char *, int&)
{
pf(name_one, name_two, number);
return pf;
}
Error:
'catch_and_return' declared as function returning a function
Can you explain to me why the compiler does not let me do this? Thank you!
Declare your function as the following:
void (*catch_and_return(void (*pf)(char*, char*, int&), char *name_one, char* name_two, int& number))(char*, char *, int&)
{
pf(name_one, name_two, number);
return pf;
}
The syntax for functions that returns functions is:
returned-function-return-type (* function-name (parameter-list) ) (function-to-return-parameter-list)
Note: This declarations can be cumbersome to understand at first sight, use typedef whenever is possible
I am new to Boost.Threads and am trying to understand how to pass function arguments to the boost::thread_groups::create_thread() function. After reading some tutorials and the boost documentations, I understand that it is possible to simply pass the arguments to this function but I can't get this method to work.
The other method I read about is to use functors to bind the parameters to my function but that would create copies of the arguments and I strictly require that const references be passed since the arguments will be big matrices(this I plan to do by using boost::cref(Matrix) once I get this simple example to work).
Now, let's get down to the code:
void printPower(float b, float e)
{
cout<<b<<"\t"<<e<<"\t"<<pow(b,e)<<endl;
boost::this_thread::yield();
return;
}
void thr_main()
{
boost::progress_timer timer;
boost::thread_group threads;
for (float e=0.; e<20.; e++)
{
float b=2.;
threads.create_thread(&printPower,b,e);
}
threads.join_all();
cout << "Threads Done" << endl;
}
This doesn't compile with the following error:
mt.cc: In function âvoid thr_main()â:
mt.cc:46: error: no matching function for call to âboost::thread_group::create_thread(void (*)(float, float), float&, float&)â
/usr/local/boost_1_44_0/include/boost/thread/detail/thread.hpp: In member function âvoid boost::detail::thread_data<F>::run() [with F = void (*)(float, float)]â:
mt.cc:55: instantiated from here
/usr/local/boost_1_44_0/include/boost/thread/detail/thread.hpp:61: error: too few arguments to function
What am I doing wrong?
You can't pass arguments to boost::thread_group::create_thread() function, since it gets only one argument. You could use boost::bind:
threads.create_thread(boost::bind(printPower, boost::cref(b), boost::cref(e)));
# ^ to avoid copying, as you wanted
Or, if you don't want to use boost::bind, you could use boost::thread_group::add_thread() like this:
threads.add_thread(new boost::thread(printPower, b, e));
For more flexibility you can use:
-Lambda functions (C++11): What is a lambda expression in C++11?
threads.create_thread([&b,&e]{printPower(b,e);});
-Functors that store the arguments as const references.
struct PPFunc {
PPFunc(const float& b, const float& e) : mB(b), mE(e) {}
void operator()() { printPower(mB,mE); }
const float& mB;
const float& mE;
};
-std::bind (C++11) or boost::bind
I'm trying to port some windows"s MFC class to linux because I have to port a windows software to linux.
here is the code I need to port
165: SMapCI it = _s$.find("nchairs");
166: if (it==_s$.end()) return 10;
167: int n = strtoul(it->second.text.GetString(), NULL, 10);
and _s$ and SMapCI are defined like this
typedef std::map<CString, STablemapSymbol> SMap;
SMap _s$;
typedef SMap::const_iterator SMapCI;
So, here is my CString class
class CString {
protected:
std::string str;
public:
CString(const char *_cstr) { str = _cstr;; };
bool operator<(char *_cstr) const { return str < _cstr;};
const char *GetString() { return str.c_str();};
};
And When I build my code, I get following error:
CTablemap/CTablemap.h:167:54: error: passing ‘const CString’ as ‘this’ argument of ‘const char* const CString::GetString()’ discards qualifiers [-fpermissive]
I don't understand this error.
g++ documentation say that
passing 'const OBJECT' as 'this' argument of 'FUNCTION' discards qualifiers
*Message found in GCC version 4.5.1
*you're returning an address
*you're attempting to access a container element with a const_iterator using a member function that has no non-const versions. The non-const function does not guarantee it will not alter the data
but ... my GetString function is defined as "const char *", so I have the keyword const ...
So I don't get it ... any help will be more than welcome
note: I'm using my own CString class instead of directly changing it with std::string because the code I want to port is too huge, and I want to do the minimum modification on it. (and some function defined in CString are not defined in std::string)
thanks in advance for any help !!
Your function signature should be
const char *GetString() const
Notice the last const.
Right now, you're saying, "I'm returning a const char pointer from a non-const CString instance". This is fine, but what you're being asked to do is define a function that can be used on a const CString instance - and that's what the last const (after the function parameter list) does, specify that that function can be called on a const CString.
You may want both versions of that function, but in particular, you need it here because the const_iterator exposes its contents as const objects, regardless of what they are inside the container itself.
The prototype of GetString should be:
const char *GetString() const;
The first const means that the caller cannot modify the returned value. The second const means that this method can be called for a const CString.
On the other hand, I would also change the operator< and use:
bool operator<(const char *_cstr)