[英]Getting error code C2228: left of '._Ptr' must have class/struct/union
[英]Why am I getting this error, left of 'method' must have class/struct/union?
我正在構建三個類,Maze,MazeRow和MazePoints,以保持迷宮結構,我在為MazeRows設置矢量時遇到問題。下面的代碼來自我的Maze類代碼。 我已將我的頭文件包含在MazeRow中。 我正在調用一個向量方法,每次得到3個錯誤。 myMazeRows也是Maze Class的私有成員變量
//Maze Header File
#include "MazeRow.h"
#include <vector>
using namespace std;
namespace MazeSolver
{
class Maze
{
public:
Maze(int rows, int columns);
MazeRow * getRow(int row);
private:
vector<MazeRow> myMazeRows();
//Maze Implementation File
#include "stdafx.h"
#include "Maze.h"
#include <vector>
using namespace std;
using namespace MazeSolver;
Maze::Maze(int rows, int columns)
{
//Recieving the Compile Error (C2228)
myMazeRows.resize(rows);
//Initializing Each Row
for(int i=0; i< rows;i++) //Recieving the Compile Error ( C2228 )
myMazeRows.push_back(MazeRow(i,columns));
}
MazeRow* Maze::getRow(int row)
{
//Recieving the Compile Error (C2228)
return &myMazeRows.at(row);
}
//Maze Row Header File
class MazeRow
{
public:
MazeRow(int rowNum, vector<MazePoint>);
MazeRow(int rowNum, int mazPoints);
Maze :: GetRow()應該至少有一個錯誤:
MazeRow* Maze::getRow(int row)
{
return &myMazeRows.at(row); // note the change from * to &
}
另一個可能是你的Maze構造函數中的循環是i<rows-1
- 很可能應該是i<rows
。 這不會導致編譯錯誤,但會導致運行時問題。
正如Attila所說,在這個功能上可以看到一個錯誤:
MazeRow *Maze::getRow(int row)
{
return *myMazeRows.at(row);
}
如果myMazeRows
包含MazeRow **
,那么這將是有效的,但你可能想要獲取MazeRow
對象的地址,如下所示:
MazeRow *Maze::getRow(int row)
{
// Ampersand (&) take the address of the row
return &myMazeRows.at(row);
}
對於std::vector
錯誤,請確保using namespace std;
在頭文件的頂部,或者正在使用std::vector
,並確保你也有#include <vector>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.