![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Location: Oregon
Posts: 3
Rep Power: 0
![]() |
I've written code to search, add/remove items from a hashtable. My program compiles without errors, but it crashes with an access violation error(msvcr80d.dll!strcat...). I haven't found any solutions to that particular message, so I've decided to start with the constructors. I'd appreciate any suggestions. I cannot let this code defeat me! My professor tried to help me and finally suggested I see tutors. The tutor tried to help me for several hours and finally suggested the internet.
From stock.h: class stock
{
public:
stock(char const * const symbol, char const * const name, int sharePrice, date priceDate); // sharePrice is given as a number of CENTS
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);From stock.cpp: stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate):
sharePrice(sharePrice),
priceDate(priceDate)
{
int length = strlen(symbol);
this -> symbol = new char[length + 1];
strcpy(this -> symbol,symbol);
length = strlen(name);
this -> name = new char[length + 1];
strcpy(this -> name,name);
}
stock::stock(const stock& s):
sharePrice(s.sharePrice),
priceDate(s.priceDate)
{
//symbol = new char[strlen(symbol) + 1];
strcpy(symbol,s.symbol);
//name = new char[strlen(name) + 1];
strcpy(name,s.name);
}
stock& stock::operator=(const stock& s)
{
cout << s.sharePrice;
priceDate = s.priceDate;
sharePrice = s.sharePrice;
symbol = new char[strlen(symbol) + 1];
strcpy(symbol,s.symbol);
name = new char[strlen(name) + 1];
strcpy(name,s.name);
return *this;
}
stock& stock::operator=(stock const * const s)
{
return *this;
}
stock::stock():
symbol(symbol),
name(name),
sharePrice(sharePrice),
priceDate(priceDate)
{
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Constructor Syntax help
In the code you've given, your copy constructor and assignment operators are among the obvious candidate culprits.
The assignment operator will need to be something like this; stock &stock::operator=(const stock& s):
{
sharePrice = s.sharePrice;
priceDate = s.priceDate;
delete [] symbol;
delete [] name; // avoid memory leak. Note: this assignment operator is not exception safe
symbol = new char[strlen(s.symbol) + 1];
strcpy(symbol,s.symbol);
name = new char[strlen([color="RED"]s./color]name) + 1];
strcpy(name,s.name);
return *this;
}stock::stock(const stock& s):
symbol(new char[strlen(s.symbol) + 1]),
name(new char[strlen(s.name) + 1]),
sharePrice(s.sharePrice),
priceDate(s.priceDate)
{
strcpy(symbol,s.symbol);
strcpy(name,s.name);
}The default constructor (the one with no arguments) is also a problem, as the non-static members are uninitialised, and you are using them to initialise themselves. That is formally undefined behaviour. If you really need such a constructor, provide more sensible default values. It would probably be a good idea to use a std::string, rather than char * members, but that won't help you understand the problem you're having. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Re: Constructor Syntax help
What grumpy said (but with an extra '['). Also, is there any point to that last constructor which takes a pointer?
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Constructor Syntax help
Oh, bugger! One time when I wish to edit my post to make the content clearer, and can't.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2007
Location: Oregon
Posts: 3
Rep Power: 0
![]() |
Re: Constructor Syntax help
Thanks. After I corrected my constructors and copy consturctors, I was able to see all of my "other" compile errors. One of my copy constructor errors was that I had two copies.
Good times. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|