Crunch Time, Need Help (C++)
Moderator: Moderators
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Crunch Time, Need Help (C++)
I have been trying to figure out what is wrong and how to fix it for the past two days. I started with a 102 errors and now down to 31. The main problem is my Dynamicly allowcated memory which the compiler seems not to like. I will post the constructor in which they appear, and then some of the errors, but I will not post all of the program due to its three objects and a driver. The three objects are connected through Inheritence, and the main problem is accuring in the base object (base - derived - derived). If you need to see more of the program, just ask.<br><br><br>Here is the class ------------------------------------------<br>class Employee<br>{<br>        protected:<br>                //Variables<br>                char &empName;<br>                char &sSN;<br>                char &EmpNum;<br>                char &HireDate;<br>                //Validations and Errors<br>                void memError ();<br>                int sSNvalid ();<br>                void sSNinvalid ();<br>                int enValid ();<br>                void enInvalid ();<br>                int HireDateValid ();<br>                void HireDateInvalid ();<br><br>        public:<br>                //Employee (char&);<br>                //Employee (char&, char&);<br>                //Employee (char&, char&, char&);<br>                Employee (char&, char&, char&, char&);<br>                //Set Functions<br>                void setEmployeeName (char&);<br>                void setSocialSecurityNumber (char&);<br>                void setEmplyeeNumber (char&);<br>                void setHireDate (char&);<br>                //Get Functions<br>                char& getEmployeeName ();<br>                char& getSSN ();<br>                char& getEmpNum ();<br>                char& getHireDate ();<br>                //Destructor<br>                //~Employee () {delete &empName;};<br>                //~Employee () {delete &empName; delete &sSN;};<br>                //~Employee () {delete &empName; delete &sSN; delete &EmpNum;};<br>                ~Employee () {delete &empName; delete &sSN; delete &EmpNum; delete &HireDate;};<br>};<br>#endif;<br><br>Here is the constructor--------------------------------------<br>Employee::Employee (char &empNameM, char &sSNM, char &EmpNumM, char &HireDateM)<br>{<br>        int len = 0;<br><br>        //Employee Name Is Inputted<br>        len = strlen(&empNameM);<br>        empName = new char[len + 1];<br>        if (&empName == NULL)<br>                {<br>                memError();<br>                }<br>        strcpy(&empName, &empNameM);<br><br>        //Social Security Number Is Inputted & Tested<br>        len = strlen(&sSNM);<br>        sSN = new char[len + 1];<br>        if (&sSN == NULL)<br>                {<br>                memError();<br>                }<br>        strcpy(&sSN, &sSNM);<br>        if (sSNvalid() == 0)<br>                {<br>                sSNinvalid();<br>                }<br><br>        //Employee Number Is Inputted & Tested<br>        len = strlen(&EmpNumM);<br>        EmpNum = new char[len + 1];<br>        if (&EmpNum == NULL)<br>                {<br>                memError();<br>                }<br>        strcpy(&EmpNum, &EmpNumM);<br>        if (enValid() == 0)<br>                {<br>                enInvalid();<br>                }<br><br>        //Hire Date Is Inputter<br>        len = strlen(&HireDateM);<br>        HireDate = new char[len + 1];<br>        if (&HireDate == NULL)<br>                {<br>                memError();<br>                }<br>        strcpy(&HireDate, &HireDateM);<br>        if (HireDateValid() == 0)<br>                {<br>                HireDateInvalid();<br>                }<br>};<br><br><br>List of errors for this part of the program---------------------<br>(126) : error C2758: 'empName' : must be initialized in constructor base/member initializer list<br>(16) : see declaration of 'empName'<br>(126) : error C2758: 'sSN' : must be initialized in constructor base/member initializer list<br>(17) : see declaration of 'sSN'<br>(126) : error C2758: 'EmpNum' : must be initialized in constructor base/member initializer list<br>(1<!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> : see declaration of 'EmpNum'<br>(126) : error C2758: 'HireDate' : must be initialized in constructor base/member initializer list<br>(19) : see declaration of 'HireDate'<br>(131) : error C2440: '=' : cannot convert from 'char *' to 'char'<br> This conversion requires a reinterpret_cast, a C-style cast or function-style cast<br>(140) : error C2440: '=' : cannot convert from 'char *' to 'char'<br> This conversion requires a reinterpret_cast, a C-style cast or function-style cast<br>(153) : error C2440: '=' : cannot convert from 'char *' to 'char'<br> This conversion requires a reinterpret_cast, a C-style cast or function-style cast<br>(166) : error C2440: '=' : cannot convert from 'char *' to 'char'<br> This conversion requires a reinterpret_cast, a C-style cast or function-style cast <p><!--EZCODE BOLD START--><strong>-LoS-</strong><!--EZCODE BOLD END--><!--EZCODE ITALIC START--><em>TimberWolf</em><!--EZCODE ITALIC END-->{FF}<br>Vehicle Specilist</p><i></i>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Crunch Time, Need Help (C++)
I think your problem is in the references. In the declarations for class Employee, change <br><br>char &empName;<br><br>to <br><br>char *empName;<br><br>and see what happens. <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> Same for the other three variables, of course. <br> <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: Crunch Time, Need Help (C++)
Thanks that was it. Not use to using the star. Well I fixed all the other problems but when I tried to run the computer came up and asked to send an error report. Not much longer till its due but since it runs I will get 8 out of 10 points. <p><!--EZCODE BOLD START--><strong>-LoS-</strong><!--EZCODE BOLD END--><!--EZCODE ITALIC START--><em>TimberWolf</em><!--EZCODE ITALIC END-->{FF}<br>Vehicle Specilist</p><i></i>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Crunch Time, Need Help (C++)
References (&) and pointers (*, ->) are TERRIBLY important to a C programmer. They're also the cause of more errors than any other feature, since you can write or read memory areas that have nothing to do with your program.<br><br>You need to understand what they are, what they represent, and what your code does to those memory spaces. If you don't learn anything else about C, learn pointers/memory management. <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
-
- Inmate
- Posts: 966
- Joined: Thu Jan 17, 2002 5:26 pm
Re: Crunch Time, Need Help (C++)
Just out of boredom in my C++ classes I would write simple programs that would read and display the contents of memory. You find mostly garbage looking characters but there is plenty in the way of interesting strings of text in there.... <p><!--EZCODE FONT START--><span style="color:red;font-size:large;">C</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:orange;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:green;font-size:large;">L</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:blue;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:indigo;font-size:large;">R</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:violet;font-size:large;">S</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:pink;font-size:xx-large;">!</span><!--EZCODE FONT END--></p><i></i>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Crunch Time, Need Help (C++)
Ya. You can cause protection faults on Windows systems that way, too. <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>