【问题标题】:C++11 STL container array without a name没有名称的 C++11 STL 容器数组
【发布时间】:2014-03-11 20:35:28
【问题描述】:

所以我有这个作业要做,我正在尝试处理一个整数数组......并且我得到了一些由我的指令编写的代码,我很困惑如何在没有名称的情况下处理一个数组通过...这里我会告诉你我的意思。

我已经获得了这个类,我需要编写一些成员函数。

class TurtleGraphics
{
private:
const static size_t NROWS = 22;  // number of rows in floor
const static size_t NCOLS = 70;  // number of colums in floor

const static int STARTING_ROW = 0;    // row that turtle will start in
const static int STARTING_COL = 0;    // column that turtle will start in

const static int STARTING_DIRECTION = 6; // direction that turtle 
                      // will be facing at the start
                      // 6 as in 6 o'clock on an analog clock
                      // The other 3 possible values are 3,9 and 12 o'clock

const static bool STARTING_PEN_POSITION = false; // Pen will be up when 
                            // program starts
                            // false means pen up, true means pen down

void displayFloor() const;  // will display floor on the screen

std::array <std::array <bool, NCOLS>, NROWS> m_Floor;

public:
const static int ARRAY_SIZE = 250;

TurtleGraphics(void); //ctor will init. floor to all "false" values, 
                      //     as well as initialization of other data members
void processTurtleMoves( const std::array< int, ARRAY_SIZE> );  // will process
                   // the commands contained in array "commands"    
};

我正在尝试编写 void processTurtleMoves(const std::array );

谁能告诉我为什么数组没有名字,或者为什么它不需要名字,或者这只是一个错误?

主要问题是我试图在没有名称的情况下处理这个数组,我很困惑。

PS。没时间联系导师。

【问题讨论】:

  • 数组应该通过 const ref 或按值传递。
  • 不相关,但您可能应该通过引用传递数组。

标签: c++ arrays stl


【解决方案1】:

在函数定义中,你给函数参数一个名字:

void TurtleGraphics::processTurtleMoves(const std::array<int, ARRAY_SIZE> commands)
//                                                                        ^^^^^^^^
{
    // ...
}

【讨论】:

  • 扩展一点@Kerrek 的回答:函数声明(没有函数体)可以有一个参数名称,也可以没有,没关系(编译器实际上忽略了它)。定义必须具有名称 - 如果名称在声明和定义之间不匹配,则不会出错。虽然,IMO,最好给参数一个名称(对于函数的用户可能更容易),最好在声明和定义之间保持名称相同。
  • @Steve:定义也不需要参数名称。仅当您想引用参数时才会这样做。
猜你喜欢
  • 2014-07-19
  • 2016-01-13
  • 2013-07-02
  • 2012-10-07
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多