2012-03-22 32 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>也是如此。

相關問題