function as a parameter to another function error - c++ [closed] - c++

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 8 years ago.
Improve this question
I have the following function:
CompareType CompareByCitizensNum(const City& c1, const City& c2) {
if (c1.getNumCitizens() > c2.getNumCitizens()
|| ((c1.getNumCitizens() == c2.getNumCitizens())
&& (c1.getCityId() > c2.getCityId()))) {
return BIGGER;
} else if (c1.getCityId() == c2.getCityId()) {
return EQUALS;
}
return SMALLER;
}
and this is the method that needs to use this function:
avlTreeVertix(City newKey, avlTreeVertix* fatherToBe,
CompareType (*compare)(const City&, const City&)) :
bf(0), key(newKey), RightHigh(0), LeftHigh(0), vertexesInSubTree(1), father(
fatherToBe), childL(NULL), childR(
NULL), compare(compare) {
CHECK_NULL(father,);
if (compare(key, father->key) == BIGGER) {
//if (isGreater(father)) {
father->childR = this;
} else {
father->childL = this;
}
}
I tried to call it by the following line:
rankTree = new avlTreeVertix(c, NULL, CompareByCitizensNumx);
But it says:
no matching function for call to wet2::avlTreeVertix::avlTreeVertix(wet2::City&, NULL, <unresolved overloaded function type>)
Any ideas?
Thanks!

OK guys, the problem was that the function was a member function, so I needed to add a "static" at the function decleration and now it's working! fool on me..

Related

How avoid error on not evaluated code C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Apologies for the most ambiguous and bizarre title.
Suppose we have 2 classes A and B.
class B has interface hasSmth but class A has not.
How to make this code evaluate without compile errors?
class A {
//..implementation
int id() { return 1; }
};
class B {
//..implementation
int id() { return 2; }
bool hasSmth() { return true; }
};
int main()
{
auto obj = someFunction();//returns A or B
if (obj.id() == 1 || (obj.id() == 2 && obj.hasSmth())) {
...
}
}
If the function returns obj of type B, then we are good.
But if it returns obj of type A, compiler will complain about A not having hasSmth, regardless of that part of if never been evaluated.
Can someone give a workaround please?
Can someone give a workaround please?
Read the declaration of someFunction to see what it returns. In the case it doesn't return B, then don't write obj.hasSmth(). Problem solved.
Now, let's change the question a bit. Let's say that you want to make this work without knowing the return type. Perhaps because rather than main you may be actually writing a template that works with different types. There are several approaches, but function overloads are a simple one:
bool check([[maybe_unused]] const A&) {
return true;
}
bool check(const B& b) {
return b.hasSmth();
}
template<bool returnsA>
void foo() {
auto obj = someTemplate<returnsA>(); // returns A or B
if (check(obj)) {

How can I optimize function calls while setting the string values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In the below code snippet, from ReadFile() function I am calling SetParams() and Execute() multiple times.
Can I optimize each SetParams() and Execute() with single call?
bool SubscriptionRead::ReadFile()
{
IVerification* pReader = new FileReader();
std::wstring oemPathPublicKey(oemFolderPath)
, oemPathSessionKey(oemFolderPath)
, oemPathUserChoices(oemFolderPath);
oemPathPublicKey.append(PUBLIC_KEY_FILE);
oemPathSessionKey.append(SESSION_KEY_FILE);
oemPathUserChoices.append(USERCHOICES_FILE);
pReader->SetParams((wchar_t*)oemPathPublicKey.c_str(), L"file");
pReader->Execute();
pReader->SetParams((wchar_t*)oemPathSessionKey.c_str(), L"file");
pReader->Execute();
pReader->SetParams((wchar_t*)oemPathUserChoices.c_str(), L"file");
pReader->Execute();
return True;
}
void FileReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
m_wszParamType = wszParamType;
m_wszParams = wszParams;
}
bool FileReader::Execute()
{
if (wcscmp(m_wszParamType, L"registry") == 0)
{
function1();
}
else
{
function2();
}
return true;
}
If your problem is calling the functions with different paras, in different lines, you can use std::ref as follows to iterate through the initializer_list of reference wrapper to the objects(i.e. std::wstring s), which reduces some typing:
#include <functional> // std::ref
#include <initializer_list> // std::initializer_list
bool SubscriptionRead::ReadFile()
{
IVerification* pReader = new FileReader();
// .... other code
for(auto strKey: {std::ref(oemPathPublicKey), std::ref(oemPathSessionKey), std::ref(oemPathSessionKey)})
{
pReader->SetParams((wchar_t*)strKey.c_str(), L"file");
pReader->Execute(); // does it needed to executed for every path? if no: outside the loop!
}
return True;
}
Also note that, in modern C++ you have smart pointers. Therefore use them, whenever appropriate and avoid manual memory allocations.

no matching function for call to in below program [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 3 years ago.
Improve this question
I am just working on a program to find topological order if possible But when I am executing the below program I am getting error
as no matching function call,I am not so good at oops concept , I mentioned in findOrder function
a comment where I am getting the error
I was solving this problem course schedule
class Solution {
public:
vector<int> findOrder(int n, vector<vector<int>>& pa) {
vector<int> g[n];
for(auto p : pa){
int u = p[1],v = p[0];
g[u].push_back(v);
}
vector<bool>visited(n,false);
vector<bool>instack(n,false);
vector<int>order;
for(int i=0;i<n;i++){
if(!visited[i]){
if(!dfs(i,visited,instack,g,order))return {};//getting error here
}
}
reverse(order.begin(),order.end());
return order;
}
bool dfs(int x,vector<int> &visited,vector<int> &instack,vector<int> g[],vector<int>order){
instack[x]=true;
visited[x]=true;
for(int i=0;i<g[x].size();i++){
if(instack[g[x][i]]==true)return false;
else if(!visited[g[x][i]] && !dfs(g[x][i],visited,instack,g))return false;
}
instack[x]=false;
order.push_back(x);
return true;
}
};
In the protoype of your function dfs you have defined the parameters instack and visited as vector<int>, but you are providing variables of type vector<bool> in the calling function.
The easiest way is probably to change:
bool dfs(int x,vector<int> &visited,vector<int> &instack,vector<int> g[],vector<int>order)
into
bool dfs(int x,vector<bool> &visited,vector<bool> &instack,vector<int> g[],vector<int>order)
This is happening because vector is a templated class and the implicit conversion of vector<int> to vector<bool> isn't defined.

A POINTER DECLARED IN CLASS NOT DECLARED IN THIS SCOPE [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 am kind a beginner to c++ and i dont really understand much about pointers.
There is an error in the code below. Soldier is a class in my program. The error states that 'targetsoldier was not declared in this scope'.
void level::battle(soldier *soldier, int targetx, int targety)
{
int x, y;
int enemyarmy;
soldier->getposition(x, y);
soldier *targetsoldier = getsoldier(targetx, targety);//THE ERROR OCCURS IN
THIS LINE
if(targetsoldier == nullptr){
return;
}
enemyarmy = targetsoldier->getarmy();
if(enemyarmy == soldier->getarmy()){
return;
}
int result = targetsoldier->takedamage(soldier->attack());
if(result ==1){
for(int h=0; h < _armies[enemyarmy].size(); h++){
if(_armies[enemyarmy][h] == targetsoldier) {
_armies[enemyarmy][h] = _armies[enemyarmy].back();
_armies[enemyarmy].pop_back();
delete *targetsoldier;
settile(targetx, targety, ' ', nullptr);
break;
}
}
}
}
The problem is that your function has a parameter named soldier; the name of that parameter then hides the name of the class soldier when it's in scope (i.e. inside the function). There are two possible solutions:
The sane one: rename the parameter (or the class)
The alternative: use class soldier instead of just solider to refer to the type when the parameter is in scope.

vector to non vector assignment in cpp [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
I am getting error on data *c=it;. I need to retrieve only one value.
class X
{
string value4;
vector<data> *T1;
}
class data
{
string value1;
int value2;
}
void doTask(X V1)
{
vector<data> *tempdata=V1.getData();
for (std::vector<data>::iterator it = tempdata->begin() ; it !=tempdata->end(); ++it)
{
data *c=it;
sendData(value3,c);
}
}
void sendData(string s,data d)
{
}
I am getting this error:
error: cannot convert 'std::vector::iterator {aka
__gnu_cxx::__normal_iterator >}' to 'data*' in initialization
I am new in this vector usage. Can somebody help me in this?
You have to dereference the iterator to get a reference to the contained item type, then get its address to get a pointer:
data *c = &*it;