Question

I'm trying to get the function PrintInformation(Employee sEmployee), declared in employ.h and defined in employ.cpp, to print out every field of an Employee struct variable that it receives as a parameter, but it's not showing up on the console. Every other cout statement in the programme works fine and I don't remember having any issues when I had all my declarations and definitions in main.cpp. I'm using CodeBlocks on a Mac OSX 10.6.8 and my compiler is GNU GCC. Here are all the files:

employ.h

#ifndef EMPLOY_H
#define EMPLOY_H

struct Employee
{
    int nID;
    int nAge;
    float fWage;
};

void PrintInformation(Employee sEmployee);

#endif // EMPLOY_H

employ.cpp

#include <iostream>
#include "employ.h"

void PrintInformation(Employee sEmployee)
{
    using namespace std;
    cout << "ID:   " << sEmployee.nID << endl;
    cout << "Age:  " << sEmployee.nAge << endl;
    cout << "Wage: " << sEmployee.fWage << endl << endl;
}

main.cpp

#include <iostream>
#include "employ.h"

int main()
{
    using namespace std;
    cout << "The size of Employee is " << sizeof(Employee) << endl;

    Employee sJoe;
    sJoe.nID = 14;
    sJoe.nAge = 32;
    sJoe.fWage = 24.15;

    Employee sFrank;
    sFrank.nID = 15;
    sFrank.nAge = 28;
    sFrank.fWage = 18.27;

    // Frank got a promotion
    sFrank.fWage += 2.50;

    //Today is Joe's birthday
    sJoe.nAge ++;

    void PrintInformation(Employee sJoe);
    void PrintInformation(Employee sFrank);


    if (sJoe.fWage > sFrank.fWage)
        cout << "Joe makes more than Frank" << endl;
    return 0;
}

Thanks in advance!


EDIT:

I forgot to specify that I previously tried calling the function with the statement PrintInformation(Employee sJoe) and got this message from the compiler:

error: expected primary-expression before 'sJoe'
Était-ce utile?

La solution

You're not calling the function, you're declaring it. Twice.

You want

PrintInformation(sJoe);
PrintInformation(sFrank);

Autres conseils

void PrintInformation(Employee sJoe);
void PrintInformation(Employee sFrank);

These are declarations, not function calls. A function can be declared as many times as you want. You're allowed to declare functions inside other functions (but not to define them). The name of a parameter is optional and has got no effect on function's signature, onyl its type matters. That's why you can name it differently at each declaration and the compiler will not complain. It's only needed at functions definitions where you usualy want to actualy use the parameter.

You need:

PrintInformation(sJoe);
PrintInformation(sFrank);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top