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;
Related
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.
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.
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;
};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have seen other similar questions being asked but I could not figure out what the problem is. I have a declaration in an inventory class as:
class Inventory
{
Public:
void print();
void sell(Item*);
void add();
void find(string);
Private:
Item* first;
}
And then in the inventory.cpp I have:
void sell(Item* item_name)
{
..........................
}
And the error comes from calling it in main() as:
Inventory store_inventory;
Item* cur_item;
cout<<"Item name: ";
string name;
cin>>name;
cur_item = find(name); //find returns Item*
store_inventory.sell(cur_item);
The error is one the line for the call to sell. Any ideas?
The definitions need to indicate that it is a member function of Inventory:
void Inventory::sell( Item* item_name )
{
// ...
}
Also, there are issues with the find() function. You declare it as a void member function, but use at as if it is a free function that returns an Item*.
Is Item a class?
You should change the void type to the type you want to be returned item.
Also whem you call your sell function, at the parameter if you want to pass the same pointer, you must use the & before the variable name like sell(item* &varname).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a list of structures(structure having parameters as name and number(both of string type)).
I need to search the list for all the matches for a given string(name or number) input by user.
I tried to use the std::search but i am not able to do that.
Is there a way i can do that?
If I understood, you have a struct A and an object list of A, so:
struct A {
string name;
int num;
}
class B {
public:
list<A>::iterator findName(const string& n) {
list<A>::iterator it = listOfA.begin();
for(; it != listOfA.end(); ++it) {
if((*it).name == n) {
return it;
}
}
return 0;
}
list<A>::iterator findNum(const int& n) {
list<A>::iterator it = listOfA.begin();
for(; it != listOfA.end(); ++it) {
if((*it).num == n) {
return it;
}
}
return 0;
}
private:
list<A> listOfA;
}
For the sorting of your list there are a lot of methods; for instance you can see here.