C ++获取对象的内存地址而不是对象具有的值

时间:2015-05-04 18:19:16

标签: c++

有点麻烦。所以我正在努力模拟交叉路口的交通。到目前为止,我有一类车辆,我进行处理的班级,以及驾驶员以及车辆的两个枚举类别(街道和方向)。

从switch语句后的Processor中的v.toString():

示例输出:http://gyazo.com/49bb512fc063493e1083d5d4d57ab0d9 [屏幕截图。]

编辑1:当有空格时,这样的枚举就好了。

http://gyazo.com/f96d7bb42cab5f3231d5a32d16b9ca82 [截图。]

这是我到目前为止所做的:

//Direction.h

#ifndef DIRECTION_H
#define DIRECTION_H


enum direction
{
    E = 0,
    W,
    N,

};

#endif

//Street.h

#ifndef STREET_H
#define STREET_H


enum street
{
    Main = 0,
    Church,
};

#endif

//Processor.h

#ifndef PROCESSOR_H
#define PROCESSOR_H
#include "Vehicle.h"
#include "Street.h"
#include "Direction.h"



class Processor
{
    private:
    int vCount; //number of cars created
    int carTime;
    direction tempDirect;
    street tempRoad;
    int lanes; //What queue its going in
    int vNum; // number of vehicle

    public:
    void createVehicles(int, int);
    void Traffic();


};

#endif

//Vehicle.h

#ifndef VEHICLE_H  
#define VEHICLE_H



#include "Street.h"
#include "Direction.h"


class Vehicle
{
   private:
    int vehicleNumber;
    int arrivalTime;
    int departureTime;
    street road;
    direction direct; 

   public:
    Vehicle(int, street, direction, int);
    Vehicle();
    void setVehicleNumber(int);
    int getVehicleNumber();
    void setArrivalTime(int);
    int getArrivalTime();
    void setDirection(direction);
    direction getDirection();
    void setStreet(street);
    street getStreet();
    void toString();


};

#endif


#include "Vehicle.h"
#include "Street.h"
#include "Direction.h"
#include <iostream>

using namespace std;


    Vehicle::Vehicle()
    {

    }


    Vehicle::Vehicle(int vNum, street b , direction a, int aTime)
    {
        vehicleNumber = vNum;
        arrivalTime = aTime;
        road = b;
        direct = a;

    }

    void Vehicle::toString()
    {
        cout<< vehicleNumber <<  road << direct << arrivalTime << endl;
    }




//Processor.cpp

#include "Vehicle.h"
#include "Processor.h"
#include "Street.h"
#include "Direction.h"
#include <stdlib.h> //random
#include <iostream>
#include <ctime> //to reset random seed so not the same number is generated every time
using namespace std;


void Processor::createVehicles(int min, int max)
{
    srand(time(NULL));
    int count = rand()%(max-min)+min; //how many vehicles will be created

    for(int i = 0 ; i < count; i++)
    {
        lanes = rand() % 6;
        switch(lanes)
        {
            case 0 : tempRoad = Church;
                tempDirect = W;
                break;
            case 1 : tempRoad = Church;
                tempDirect = E;
                break;
            case 2 : tempRoad = Main;
                tempDirect = N;
                break;
            case 3 : tempRoad = Main;
                tempDirect = W;
                break;
            case 4 : tempRoad = Main;
                tempDirect = N;
                break;
            case 5 : tempRoad = Main;
                tempDirect = E;
                break;


        }

        Vehicle v((vNum + 1), tempRoad, tempDirect, carTime);

        vNum++;
        vCount++;
        v.toString();

    }





}

//Driver.cpp

using namespace std;
#include "Processor.h"
#include <iostream>



int main()
{
    Processor process;

    process.createVehicles(8, 12);

    return 0;

}

2 个答案:

答案 0 :(得分:0)

您没有获得内存地址,但是您正在获取未初始化的数据。将toString()中的输出分割为

cout<< vehicleNumber << " " << road << " " << direct << " " << arrivalTime << endl

您可以看到每辆车的vehicleNumber增加1。如果它是一个记忆地址,那么每次更改值可能会超过4。它是一个奇怪的数字的原因是你永远不会给它一个值,所以它变成了变量恰好驻留的内存位置。 arrivalTime永远不会更改,因为您的代码中没有任何地方可以更改它,并且由于vehicleNumber的原因,它也会获得垃圾值。

答案 1 :(得分:0)

我不知道你在哪里使用指针或获取变量的地址。打印变量的地址通常是这样的:

  Direction d;
  d = S;
  cout << (void *)(&d) << endl;  

我注意到您正在打印enum变量。当您打印enum变量时,编译器将打印出序数或指定的枚举标识符值;不是标识符的名称或文本。

打印enum标识符名称

打印出enum标识符名称涉及enum值与文本相关联。两种流行的方法是使用std::map和查找表。

查找表

创建结构数组并搜索结构:

enum Direction { NORTH, WEST, SOUTH, EAST};
struct Direction_Entry
{
  Direction d;
  char const * text;
};

char const * Direction_To_String(enum Direction d)
{
  static const Direction_Entry dir_table[] =
  {
    {NORTH, "North"}, {WEST, "West"},
    {SOUTH, "South"}, {EAST, "East"},
  };
  static const unsigned int dir_entries =
    sizeof(dir_table) / sizeof(dir_table[0]);
  static char const text_unknown_dir[] = "Unknown direction";
  unsigned int i = 0U;
  char const * name = 0;
  for (i = 0U; i < dir_entries; ++dir)
  {
    if (dir_table[i].d == d)
    {
      name = dir_table[i].text;
    }
  }
  return name;
}

类似的功能可用于从文本转换为enum

使用std::map

也可以使用std::map

const std::string& Direction_To_String(Direction d)
{
  static const std::string unknown_dir = "Unknown Direction";
  static std::map<Direction, d> dir_map;
  if (dir_map.empty())
  {
    dir_map[NORTH] = "North"; dir_map[WEST] = "West";
    dir_map[SOUTH] = "South"; dir_map[EAST] = "East";
  }
  const std::map<Direction, d>::const_iterator iter = dir_map.find(d);
  if (iter != dir_map.end())
  {
    return iter->second;
  }
  return Unknown_Direction;
}