2012-03-22 33 views
1

我正在建造三个类,迷宫,MazeRow和MazePoints,以保持迷宫结构,并且我无法为MazeRows设置我的矢量。下面的代码来自我的Maze类代码。我已经包含MazeRow的头文件。每次我调用矢量方法时,都会收到3个错误。也myMazeRows是迷宫的私有成员变量为什么我得到这个错误,'method'的左边必须有class/struct/union?

//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); 
+2

哪条线显示错误?什么是* actual *错误信息(复制和粘贴)?还请显示'myMazeRows'的实际定义。 – 2012-03-22 02:38:02

+0

你有没有合适的#include? – Benoir 2012-03-22 02:50:04

+0

如果您向我们展示'myMazeRows'的声明,可能会有所帮助。而且,正如@Benoir暗示的那样,您可以向我们展示'#include'语句。 – aldo 2012-03-22 03:08:12

回答

2

至少一个错误的迷宫:: GETROW()应为:

​​

另一个可能是你在迷宫构造函数中的循环是i<rows-1 - 最有可能应该是i<rows。这不会导致编译错误,但会导致运行时问题。

1

正如阿提拉说,错误可以在此功能中可以看出:

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,并确保你哈ve #include <vector>也是如此。

相关问题