-3

I'm struggling with coding a-star search algorithm, I have to make it in c++, despite I'm not really familiar with it. I've decided to use classes and lists, but I have one problem. My code looks like this:

class gridPoint
{
    public:
    int x;
    int y;
    int field;
}
gridPoint mapa[20][20];
startX=1;
startY=1;
//code to set the values of attributes
int main(){

mapa[startX,startY] = 1;
list<gridPoint> listZ;
listZ.push_back(*mapa[startX,startY]);
}

To this point it seems to work, compiles and executes with no errors. Now I need to get somehow to this element of list, but I have really no clue how to do that. I've tried simply:

list<gridPoint>::iterator it = listZ.begin();
cout<<listZ[it].x;

or:

cout<<*it.x;

but it doesn't work, either with ".x" or without

Later I would need to find specific object in a list with specific value of attribute and/or delete it, but without this mentioned above, I couldn't do it anyway. Any clues, how to make it work?

Kozikom
  • 83
  • 1
  • 1
  • 5
  • `*mapa[startX,startY]` simply isn't valid c++ syntax. What are you trying to do at all? Why are you using a raw c-style array in your code in 1st place. Why are you using `std::list` in preference to `std::vector`? Are you trying to port some java like code to c++? – πάντα ῥεῖ Dec 22 '18 at 12:58
  • `compiles and executes with no errors`: I don't believe it: Missing semicolon after class definition; `startX` and `startY` undeclared; assignment of incompatible types in `mapa[startX,startY] = 1;` –  Dec 22 '18 at 12:59
  • `cout<< it->x;` – bruno Dec 22 '18 at 13:04
  • I'd suggest reading [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (or two). – Jesper Juhl Dec 22 '18 at 13:05
  • Semicolons and defining startx/y is not the issue, I just didn't put all my code, but the mostly problematic part. Thanks bruno, this is what I was looking for! – Kozikom Dec 22 '18 at 13:18
  • @Kozikom I put a corrected version of your code in an answer, as you can see there were many problems – bruno Dec 22 '18 at 13:21
  • Even if that is the case, the line `mapa[startX,startY] = 1;` with the given definition of `mapa` must produce a compilation error, @bruno gave you the correct syntax. Guessing syntax is not a good idea without looking it up in a reference. There are many unexpected or undefined behaviors and exceptions that may compile and occasionally even run seemingly without problem. –  Dec 22 '18 at 20:52

1 Answers1

0

your code corrected, with explications/remarks

#include <iostream>
#include <list>

using namespace std;

class gridPoint
{
  public:
    int x;
    int y;
    int field;
}; // the ';' was missing

gridPoint mapa[20][20]; // why global ?
int startX=1;  // why global ?, in case : the first index is 0 rather than 1 if you do not know that
int startY=1;  // why global ?, in case : the first index is 0 rather than 1 if you do not know that

//code to set the values of attributes
int main() {
  mapa[startX][startY] = { 1, 2, 3 }; // each index in its [], mapa[][] is not an int

  list<gridPoint> listZ;

  listZ.push_back(mapa[startX][startY]); // '*' removed, mapa[][] is a gridPoint, not a pointer to

  list<gridPoint>::iterator it = listZ.begin(); // can be a const_iterator because let the list unchanged

  cout << it->x << endl; // it->attr it->oper() etc, not it.attr etc
}
bruno
  • 32,421
  • 7
  • 25
  • 37
  • There is also `auto it = ` to avoid writing out long type names that do not add to correctness/readability and if the iterator is not actually used as iterator, a reference obtained from `listZ.front()` may be preferable. –  Dec 22 '18 at 20:56
  • @user10605163 the tag is just c++, not c++11 or other, so I used 'old' c++. Yes about _front()_ but his problem was how to use an iterator :-) – bruno Dec 22 '18 at 21:26
  • Yes that's reasonable, the comment was meant to be additional advice for the questioner. –  Dec 22 '18 at 21:43