Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 9th, 2007, 7:14 PM   #1
codenemesis
Newbie
 
codenemesis's Avatar
 
Join Date: Nov 2007
Location: Oregon
Posts: 3
Rep Power: 0 codenemesis is on a distinguished road
Question Constructor Syntax help

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)
{
}
codenemesis is offline   Reply With Quote
Old Nov 10th, 2007, 1:18 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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;
}
and your copy constructor will, preferably, be something like 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.
grumpy is offline   Reply With Quote
Old Nov 10th, 2007, 1:23 AM   #3
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Re: Constructor Syntax help

What grumpy said (but with an extra '['). Also, is there any point to that last constructor which takes a pointer?
Klipt is offline   Reply With Quote
Old Nov 10th, 2007, 2:02 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Constructor Syntax help

Oh, bugger! One time when I wish to edit my post to make the content clearer, and can't.
grumpy is offline   Reply With Quote
Old Nov 17th, 2007, 6:09 PM   #5
codenemesis
Newbie
 
codenemesis's Avatar
 
Join Date: Nov 2007
Location: Oregon
Posts: 3
Rep Power: 0 codenemesis is on a distinguished road
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.
codenemesis is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC