no matching function for call to in below program [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 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.

Related

What is the correct way of implementing this custom priority_queue [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 want to build a priority queue of an object having three elements having my own comparator. I tried the following code but is showing error .
#include<bits/stdc++.h>
using namespace std;
class triplet {
public:
int data;
int arr;
int index;
};
bool compare( triplet x, triplet y ){
if(x.data < y.data){
return true;
}
else{
return false;
}
}
int main(){
priority_queue<triplet,vector<triplet>,compare> pq1;
}
getting the following error
enter image description here
Comparison routines must return false if the two elements are equal, but your version returns true.
Try this instead
bool compare(triplet x, triplet y) {
if (x.data < y.data) {
return true;
}
else {
return false;
}
}
or to make it a little simpler like this
bool compare(triplet x, triplet y) {
return x.data < y.data;
}
But the important point is < not <=.
EDIT
Your code also uses compare incorrectly. compare is not a type, so it's not a valid argument for the priority_queue template.
The type of compare is bool (*)(triplet, triplet) so the right way to do this is to use that type in the template and pass the actual comparison function to the constructor. Like this
priority_queue<triplet, vector<triplet>, bool (*)(triplet, triplet)> pq1(compare);
BTW normally you would do this by creating a comparison functor, not a function. It's a little cleaner that way (and potentially more efficient).

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.

Initialization of the structure [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 5 years ago.
Improve this question
In my program I have structure:
struct point {
float x;
float y;
};
Edit: I need to create
struct Path{
Point array[];
}
Initialize it with function init_path(Path *p, int size).
My question is, how to define the function?
Thanks in advance.
Your Path may be like:
struct Path {
point* points;
};
void init_path(Path *path, int size) {
path->points = new point[size]();
}
but why your professor wants a function instead of proper constructor/destructor remains a mystery. Here you still need to call delete[] on points somewhere. With following structure you don't need any init function and object will properly delete its resources.
struct Path {
Path(unsigned size) : points{ new point[size] } {}
~Path() { delete[] points; }
point* points;
};

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;

Vector erase iterator outside range [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 8 years ago.
Improve this question
Porting some code from my game engine from Mac to Windows in C++, and I get this runtime error: "Vector erase outside range". It works on Mac!
void Entity::runDeferreds() {
for (auto it = deferreds.begin(); it != deferreds.end(); /* nothing */ ) {
if (it->Condition()) {
it->Execution();
it = deferreds.erase(it);
} else {
++it;
}
}
}
This iterates thru a list of "deferred" tasks, which are stored in a std::vector<DeferredCall> called deferreds. If the DeferredCall's Condition() is fulfilled, then its Execution() is run, and it should be removed from the vector. However, instead, I get the aforementioned error!
DeferredCall looks like this, not that it's too important:
struct DeferredCall {
std::function<bool()> Condition;
std::function<void()> Execution;
};
Help?!
EDIT:- Alternative Method
I also tried this, again working on Mac:
deferreds.erase(std::remove_if(deferreds.begin(), deferreds.end(),
[](DeferredCall &call) {
if (call.Condition()) {
call.Execution();
return true;
}
return false;
}
), deferreds.end());
However, in this case I get "vector iterators incompatible".
Although it doesn't answer where your error comes from, you may try to rework the code as follows:
const auto pred = [](Deferred& d){ return !d.Condition(); };
auto itMatch = std::partition( defs.begin(), defs.end(), pred);
const auto action = [](Deferred& d){ d.Execution(); };
std::for_each(itMatch, defs.end(), action);
defs.erase(itMatch, defs.end());
Also, std::vector::erase is guaranteed to return perfectly valid iterator. Could be vector::end() though.
Links: std::partition, std::for_each, std::vector::erase