C++ 遍历指向类的指针向量

C++ 遍历指向类的指针向量,c++,class,pointers,iterator,C++,Class,Pointers,Iterator,所以我有一个问题,我试图得到一艘船的坐标并返回一个指针。但是我遇到了一个问题,编译器没有检测到我为ship类创建的私有变量 在我的.h文件中 #ifndef BOARD_H #define BOARD_H #include "Ship.h" #include<vector> class Board { private: std::vector<Ship *> shipList; char score[10][10]; Ship *shipAt

所以我有一个问题,我试图得到一艘船的坐标并返回一个指针。但是我遇到了一个问题,编译器没有检测到我为ship类创建的私有变量

在我的.h文件中

#ifndef BOARD_H
#define BOARD_H
#include "Ship.h"
#include<vector>
class Board
{

  private:
    std::vector<Ship *> shipList;
    char score[10][10];
    Ship *shipAt(int x, int y);
};
#endif
我的职能

Ship *Board::shipAt (int x, int y){

    vector <Ship*> :: iterator locate; //locating the appropiate ship with this iterator

    for ( locate = shipList.begin(); locate != shipList.end() ; locate++ ){
            if( *locate.x1 == *locate.x2){
                    if(( y <= *locate.y1 && y >= *locate.y2) || (y <= *locate.y2 && y >= *locate.y1)){
                            return locate;
                    }
            }

            else if ( *locate.y1 == *locate.y2){
                            if(( x <= *locate.x1 && x >= *locate.x2) || ( x <= *locate.x2 && *locate.x1)){
                                    return locate;
                            }
                    }
    }
}


 I'm getting the error

 Board.cpp:54:15: error: ‘std::vector<Ship*>::iterator’ has no member named ‘x1’
   if( *locate.x1 == *locate.x2){
           ^
Board.cpp:54:29: error: ‘std::vector<Ship*>::iterator’ has no member named ‘x2’
   if( *locate.x1 == *locate.x2){
Ship*Board::shipAt(整数x,整数y){
vector::iterator locate;//使用此迭代器查找适当的发货
对于(locate=shipList.begin();locate!=shipList.end();locate++){
如果(*locate.x1==*locate.x2){
if((y=*locate.y2)| |(y=*locate.y1)){
返回定位;
}
}
else if(*locate.y1==*locate.y2){

if((x=*locate.x2)| |(x您遇到的第一个问题是运算符优先级。您试图取消对
locate.x1
的引用,而实际上您想做的是首先取消对
locate
的引用以获取指针,然后访问
x1
成员。因此您想要的代码是
(*locate.x1
(见下一段)

还有两个问题。由于您有一个指针,因此要访问
x1
,您需要使用
->
,而不是“

最后,您将遇到可见性问题,因为
x1
是私有的

错误消息为您提供了一个良好的诊断:

错误:“std::vector::iterator”没有名为“x2”的成员


编译器告诉您,
迭代器
类型没有成员
x2
,这应该暗示您试图从错误类型的对象访问
x2
。您试图从
发货

访问x2。您遇到的第一个问题是运算符优先级。您试图取消对
loca的引用te.x1
实际上,您想做的是首先取消引用
locate
以获取指针,然后访问
x1
成员。因此,您想要的代码是
(*locate).x1
(见下一段)

还有两个问题。由于您有一个指针,因此要访问
x1
,您需要使用
->
,而不是“

最后,您将遇到可见性问题,因为
x1
是私有的

错误消息为您提供了一个良好的诊断:

错误:“std::vector::iterator”没有名为“x2”的成员


编译器告诉您,
迭代器
类型没有成员
x2
,这应该暗示您试图从错误类型的对象访问
x2
。您试图从
发货

访问x2。您遇到的第一个问题是运算符优先级。您试图取消对
loca的引用te.x1
实际上,您想做的是首先取消引用
locate
以获取指针,然后访问
x1
成员。因此,您想要的代码是
(*locate).x1
(见下一段)

还有两个问题。由于您有一个指针,因此要访问
x1
,您需要使用
->
,而不是“

最后,您将遇到可见性问题,因为
x1
是私有的

错误消息为您提供了一个良好的诊断:

错误:“std::vector::iterator”没有名为“x2”的成员


编译器告诉您,
迭代器
类型没有成员
x2
,这应该暗示您试图从错误类型的对象访问
x2
。您试图从
发货

访问x2。您遇到的第一个问题是运算符优先级。您试图取消对
loca的引用te.x1
实际上,您想做的是首先取消引用
locate
以获取指针,然后访问
x1
成员。因此,您想要的代码是
(*locate).x1
(见下一段)

还有两个问题。由于您有一个指针,因此要访问
x1
,您需要使用
->
,而不是“

最后,您将遇到可见性问题,因为
x1
是私有的

错误消息为您提供了一个良好的诊断:

错误:“std::vector::iterator”没有名为“x2”的成员


编译器告诉您,
迭代器
类型没有成员
x2
,这应该暗示您试图从错误类型的对象访问
x2
。您试图从
Ship

访问x2。尽管frozenkoi的答案很好,但您可能对以下问题感兴趣

  • 不要使用指针。使用智能指针,例如
  • 记住增量前后的区别。始终使用迭代器的预入/减量
  • 使用众所周知的算法来解决众所周知的和有文档记录的问题

  • 尽管frozenkoi的回答很好,但您可能对以下问题感兴趣

  • 不要使用指针。使用智能指针,例如
  • 记住增量前后的区别。始终使用迭代器的预入/减量
  • 使用众所周知的算法来解决众所周知的和有文档记录的问题

  • 尽管frozenkoi的回答很好,但您可能对以下问题感兴趣

  • 不要使用指针。使用智能指针,例如
  • 记住增量前后的区别。始终使用迭代器的预入/减量
  • 使用众所周知的算法来解决众所周知的和有文档记录的问题

  • 尽管frozenkoi的回答很好,但您可能对以下问题感兴趣

  • 不要使用指针。使用智能指针,例如
  • 记住增量前后的区别。始终使用迭代器的预入/减量
  • 使用知名算法解决知名和知名文档
    Ship *Board::shipAt (int x, int y){
    
        vector <Ship*> :: iterator locate; //locating the appropiate ship with this iterator
    
        for ( locate = shipList.begin(); locate != shipList.end() ; locate++ ){
                if( *locate.x1 == *locate.x2){
                        if(( y <= *locate.y1 && y >= *locate.y2) || (y <= *locate.y2 && y >= *locate.y1)){
                                return locate;
                        }
                }
    
                else if ( *locate.y1 == *locate.y2){
                                if(( x <= *locate.x1 && x >= *locate.x2) || ( x <= *locate.x2 && *locate.x1)){
                                        return locate;
                                }
                        }
        }
    }
    
    
     I'm getting the error
    
     Board.cpp:54:15: error: ‘std::vector<Ship*>::iterator’ has no member named ‘x1’
       if( *locate.x1 == *locate.x2){
               ^
    Board.cpp:54:29: error: ‘std::vector<Ship*>::iterator’ has no member named ‘x2’
       if( *locate.x1 == *locate.x2){
    
    Ship* Board::shipAt (int x, int y)
    {
        for ( auto s : shipList )
        {
            if( x < min(s->x1,s->x2) ) continue;
            if( x > max(s->x1,s->x2) ) continue;
            if( y < min(s->y1,s->y2) ) continue;
            if( y > max(s->y1,s->y2) ) continue;
            return s;
        }
        return nullptr;
    }