简体   繁体   English

为什么我得到这个错误,'method'的左边必须有class / struct / union?

[英]Why am I getting this error, left of 'method' must have class/struct/union?

I am building three classes, Maze, MazeRow, and MazePoints, to hold a maze structures and I am having trouble with setting up my vector for the MazeRows.The below code is from my Maze class code. 我正在构建三个类,Maze,MazeRow和MazePoints,以保持迷宫结构,我在为MazeRows设置矢量时遇到问题。下面的代码来自我的Maze类代码。 I have included my header file for MazeRow. 我已将我的头文件包含在MazeRow中。 I am getting 3 errors each where i am calling a vector method. 我正在调用一个向量方法,每次得到3个错误。 Also myMazeRows is a private member variable of Maze Class 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);

At least one error the Maze::GetRow() should be: Maze :: GetRow()应该至少有一个错误:

MazeRow*       Maze::getRow(int row)  
{ 
  return &myMazeRows.at(row);  // note the change from * to &
} 

Another possibly is that your loop in Maze constructor is to i<rows-1 -- most likely should be i<rows . 另一个可能是你的Maze构造函数中的循环是i<rows-1 - 很可能应该是i<rows This will not cause compilation error, but runtime problems. 这不会导致编译错误,但会导致运行时问题。

As Attila said, an error can be seen at this function: 正如Attila所说,在这个功能上可以看到一个错误:

MazeRow *Maze::getRow(int row) 
{
    return *myMazeRows.at(row); 
}

If myMazeRows were containing a MazeRow ** , then this would be valid, but You probably meant to take the address of the MazeRow object, like so: 如果myMazeRows包含MazeRow ** ,那么这将是有效的,但你可能想要获取MazeRow对象的地址,如下所示:

MazeRow *Maze::getRow(int row) 
{
    // Ampersand (&) take the address of the row
    return &myMazeRows.at(row); 
}

For the std::vector errors, make sure you either have using namespace std; 对于std::vector错误,请确保using namespace std; at the top of your header file, or are using std::vector , and ensure you have #include <vector> as well. 在头文件的顶部,或者正在使用std::vector ,并确保你也有#include <vector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM