Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Constructor Syntax help (http://www.programmingforums.org/showthread.php?t=14383)

codenemesis Nov 9th, 2007 8:14 PM

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)
{
}


grumpy Nov 10th, 2007 2:18 AM

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.

Klipt Nov 10th, 2007 2:23 AM

Re: Constructor Syntax help
 
What grumpy said (but with an extra '['). Also, is there any point to that last constructor which takes a pointer?

grumpy Nov 10th, 2007 3:02 AM

Re: Constructor Syntax help
 
Oh, bugger! One time when I wish to edit my post to make the content clearer, and can't.

codenemesis Nov 17th, 2007 7:09 PM

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.


All times are GMT -5. The time now is 3:34 AM.

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