PDA

View Full Version : Weird programming bug I don't understand.


MentalSentinel
08-14-2005, 11:53 AM
Hey.

I'm currently in my C++ learning phase. (again..)
I made a small program that lets you add an employee or call upon all employees and list their employee number, salary and the amount of years the employee has been.. employed.

But, I'm getting two errors on line 54 and 55, saying that what's left of .salaris and .jaarInDienst must be class/struct/union typed.

Here is the entire error message (using Visual C++ 6.0):


(54) : error C2228: left of '.salaris' must have struct/class/union type.



And the same message of line 55.

This is my code:

#include <iostream>

using namespace std;

class Werknemer
{
public:
int jaarInDienst;
int salaris;
Werknemer(int salarisWerknemer, int jaarInDienstWerknemer);
};

Werknemer::Werknemer(int salarisWerknemer, int jaarInDienstWerknemer)
{
salaris = salarisWerknemer;
jaarInDienst = jaarInDienstWerknemer;
cout << "Employee added!";
}

int main()
{
int restart;
int action;
Werknemer *werknemerarray[3];
int i = 0;
while(true)
{
cout << "What would you like to do?";
cout << "\n" << "(1) Add an employee.";
cout << endl << "(2) List all employees.\n\n";

cin >> action;

if(action == 1)
{
int employeesalary;
int jaarInDienst;
cout << "\n\nWelcome to the add-an-employee section!\n";
cout << "Please enter an employee salary. ";
cin >> employeesalary;
cout << "\nPlease enter the amount of years this employee has served. ";
cin >> jaarInDienst;
cout << "\nThank you. Processing your request....\n";
werknemerarray[i] = new Werknemer(employeesalary, jaarInDienst);
i++;
}
if (action == 2)
{
int tempi = 0;
cout << "\n Listing all employees..";
while(tempi < i)
{
cout << "Employee number: " << tempi;
cout << "\nEmployee salary: " << werknemerarray[tempi]->salaris;
cout << "\nYears served: " << werknemerarray[tempi]->jaarInDienst;
cout << endl << endl;
tempi++;
}
}
cout << "Do you wish to restart (1/0)?\n";
cin >> restart;
if(restart == 0)
break;
}

return 0;
}

Can anyone please tell me how to fix this and what this means?

Anyway, forgive my lack of comment and partly Dutch and English.

Cheers, MentalSentinel.

Farlander
08-14-2005, 12:21 PM
The . operator is being called on the pointer before it is dereferenced by the *. You need to do this:

(*werknemerarray[tempi]).salaris;

Or, there is another operator that combines those for convenience:

werknemerarray[tempi]->salaris;

MentalSentinel
08-14-2005, 12:34 PM
Yeah, I just remembered that before checking this topic again.
Did it and changed an unending loop. The program works now. Although it has only room for 3 employees.

Does anyone know of a way to add an almost infinite amount of employees without declaring werknemerarray with thousands of entries and thus wasting unnecessary memory storage?

PS: I edited the original post with my current code.

EDIT: Does anyone know why if(restart = "y") requires a 2 entry char array and can't use a simple char?

pjohnsonjr
08-14-2005, 01:52 PM
there's two ways you can do that, unfortunately one way still remains static even if it is dynamically allocated... Your best bet is to learn to use a vector. A vector can be scaled and shrunk at runtime. Most good C++ books will scratch the surface on vectors, if not more.

MentalSentinel
08-14-2005, 01:57 PM
Oh ok. I haven't read that chapter yet. I think this book covers it.

Tedades
08-14-2005, 07:07 PM
Or use a old fashioned linked list http://forums.3drealms.com/ubbthreads/images/graemlins/tongue.gif

Rider
08-15-2005, 04:24 AM
wow, that's exactly why I never use english AND dutch together in the same code.


I don't know much of C, but it looks like you used salary and Werknemersalaris in your code at the same time. I can't tell if they're related, but it seems like mistakes are easily made that way...

just my 0.02 http://forums.3drealms.com/ubbthreads/images/graemlins/smile.gif