这是库存源的实现文件的片段:
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;
}
这就是我用cstrings分配内存的方法。在主要情况下,参数传入,例如“ symbol,” name“,10,date :: JANUARY,17,1967。日期是对象,其月数被枚举。这是最后一个参数” date“类型,即iim我在从一个源文件移动到另一个源文件时很难保留属性,我在上面的函数中看到“ 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;
};
因此,当我需要添加到hashTable或反之亦然时,我最终得到的是可靠的值。
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;
};
这可能是我可以解决日期属性不保留其值的问题的地方。 IM只是不确定在哪里做什么。
#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)
{
/ *片段* /
dateObj =&priceDate;
}
不要那样做priceDate
是堆栈上的参数之一。一旦构造函数结束,&priceDate
就会变成陈旧的指针。考虑实际复制对象(而不是使用指针,例如声明date dateObj;
并用dateObj = priceDate;
分配)
关于c++ - 来自第三类对象C++的垃圾值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1528609/