Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自第三类对象C++的垃圾值_C++ - Fatal编程技术网

来自第三类对象C++的垃圾值

来自第三类对象C++的垃圾值,c++,C++,以下是股票源的实现文件片段: stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate ) : m_sharePrice( sharePrice ) { if ( symbol ) { size_t length = strlen( symbol ) +1; m_symbol = new char[length

以下是股票源的实现文件片段:

 stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate )
 : m_sharePrice( sharePrice )
 {
    if ( symbol )
    {
        size_t length = strlen( symbol ) +1;
        m_symbol = new char[length];
        strcpy_s( m_symbol, length, symbol );
    }
    else m_symbol = NULL;
    if ( name )
    {
            size_t length = strlen( name ) +1;
       m_name = new char[length];
       strcpy_s( m_name, length, name );
    }
    else m_name = NULL;

    dateObj = &priceDate;
 }
这就是我用cstring分配内存的方法。主要的参数是这样传递的,符号,名称,10,日期::1967年1月17日。日期是枚举其月份的对象。这是我遇到的最后一个参数日期类型。当从一个源文件移动到下一个源文件时,我很难保留属性。我在上面的函数中看到,dateObj具有我需要的属性。但是当我将它传输到另一个源文件时,值就消失了

pragma once
#include <ostream>
using namespace std;

class date
  {
  public:
    typedef enum {INVALID, JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
              JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER,DECEMBER}
    Month;

    date(Month month, int day, int year);
    date(const date& date);         // copy constructor
    date(void);                              // default constructor
    ~date(void);

    friend ostream& operator<<(ostream& out, const date& d);

 private:
    Month   month;
    int     day;
    int     year;
};
因此,当我需要添加到哈希表中时,或者当我需要添加到哈希表中时,我最终得到了可加值

bool hashmap::get(char const * const symbol, stock& s) const
{
    int index = 0;
    // search for the stock associated with the symbol.
    while ( index < maxSize )
    {
        if ( hashTable[index].m_symbol == NULL ) 
        {
            index++;
        }
        else if ( strcmp( symbol, hashTable[index].m_symbol  ) == 0 )
        {
        //s = *hashTable; // call assignemnt overload
             s.m_name = hashTable[index].m_name; // has correct value
             s.dateObj = hashTable[index].dateObj; // GARBLE!
             s.m_sharePrice = hashTable[index].m_sharePrice; // correct 
             s.m_symbol = hashTable[index].m_symbol; // correct
             return true;
        }
    }
        return false;
    }
也许我可以在其中一个头文件中添加一些东西来简化这个过程

 #include "date.h"

 using namespace std;

 class stock
 {
  public:
    stock(char const * const symbol, char const * const name, int sharePrice, date priceDate);
    stock(const stock& s);  // copy constructor
    stock(void);                                     // default constructor
    char const * const getSymbol(void) const;
    stock& operator=(const stock& s);
    stock& operator=(stock const * const s);
    ~stock(void);

    // display column headers
    static void displayHeaders(ostream& out);

     friend ostream& operator<<(ostream& out, const stock& s);

     friend class hashmap;
     private:

     date *dateObj;
     char *m_symbol;
     char *m_name;
     int   m_sharePrice;

     static int   maxSize;
  };

 #include "stock.h"

 class hashmap
 {
  public:
hashmap(int capacity);
~hashmap(void);
bool get(char const * const symbol, stock& s) const;

    bool put(const stock& s, int& usedIndex, int& hashIndex, int& symbolHash);
    bool remove(char const * const symbol);

    friend ostream& operator<<(ostream& out, const hashmap& h);

private:
        static int hashStr(char const * const symbol);
    friend class stock;

    stock *hashTable;
 };
这可能是我可以解决日期属性不保留其值的问题的地方。我只是不知道该在哪里做什么

 #include "date.h"

 date::date(Month month, int day, int year)
 {
    this->month = month;
    this->day  = day;
    this->year = year;
 }

date::date(const date& date)
{
}

date::date()
{
    day = this->day;
    year = this->year;  
        month = this->month;
}

date::~date(void)
{

}

ostream& operator<<(ostream& out, const date& d)
{
   out << d.day << d.month << d.year << endl;
        return out;
}

那些日期构造器看起来是错的。无参数或复制构造函数实际上都不会初始化任何字段

stock::stock(/* snip */ date priceDate ) : m_sharePrice( sharePrice ) { /* Snip */ dateObj = &priceDate; }
不要那样做。priceDate是堆栈上的参数之一。一旦构造函数结束,&priceDate就成为一个过时的指针。考虑实际复制对象,而不是使用指针,例如声明日期DATEOBJ;并用dateObj=priceDate赋值

我可以这样做吗?我是否需要使用“:”在dates-ctor-init列表中初始化?最好使用:,尽管您也可以只分配构造函数体中的字段。我建议只删除date的复制构造函数,而使用编译器生成的复制构造函数和赋值运算符。我不确定无参数构造函数在做什么。。。我希望像date::date{std::time\u t now=std::timeNULL;std::tm local=*std::localtime&now;day=local.tm\u mday;month=local.tm\u mon+1;year=local.tm\u year+1900;}是的,这就是问题所在。愚蠢的我和愚蠢的陈腐的ptr。非常感谢你!