2

I have an array of struct employee which I am trying to call in the function readFile.

I have tried using pointers to call but they all fail and give me the same error message.

#include <iostream>
#include <fstream>
using namespace std;

int readFile (ifstream *inFile, struct employee array[]);

int main () {
    int size = 10;
    struct employee {
        string name;
        int id;
        float salary;
    } array[size];
    ifstream inFile;
    readFile(&inFile, array);
    return 0;
}

int readFile (ifstream *inFile, struct employee array[]) {
    inFile->open("pay.txt");
    if(inFile->fail()) {
        cout << "fail";
        exit(1);
    } else {
        cout << "success";
    }
    return 0;
}

The error message I get is this:

.cpp:16:25: error: cannot convert 'main()::employee*' to 'employee*' for argument '2' to 'int readFile(std::ifstream*, employee*)'
readFile(&inFile, array);

2
  • 3
    By the way, my salary cannot be represented exactly in an IEEE754 float (I could negotiate a raise to the next dyadic rational?). Your best bet is to work in cents when modelling money. Commented May 22, 2019 at 7:34
  • 4
    You should move your struct definition outside of the main function. Commented May 22, 2019 at 7:35

2 Answers 2

6

Your struct employee is local to your main function and the other one is global, so it cannot convert 'main()::employee*' to 'employee*' because both are different types.

Move it outside to have uniform struct employee throughout the program and create a local object of struct employee inside the main function (not a good practice to create a global variable).

By the way in C++ you don't need to append struct everywhere when using an already defined structure. Your original post looks more like programmed in C

#include <iostream>
#include <fstream>
using namespace std;

struct employee {
        string name;
        int id;
        float salary;
};

int readFile (ifstream *inFile, employee array[]);

int main () {
    int size = 10;
    employee array[size];

    ifstream inFile;
    readFile(&inFile, array);
    return 0;
}

int readFile (ifstream *inFile, employee array[]) {
    inFile->open("pay.txt");
    if(inFile->fail()) {
        cout << "fail";
        exit(1);
    } else {
        cout << "success";
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

4

There are two classes named employee in your code. One is in the global namespace, which is declared (not defined) in the declaration of the function readFile. The other class named employee is defined locally in the main function. These two are absolutely unrelated types.

That's why the compiler is saying that it cannot convert main()::employee* to employee*: it's trying to convert from the main-local type to the global type (and of course that's not possible).

What you should do is move the definition of struct employee outside main, so that main creates objects of the global type:

#include <iostream>
#include <fstream>
using namespace std;

struct employee {
    string name;
    int id;
    float salary;
} array[size];

int readFile (ifstream *inFile, employee array[]);

int main () {
    int size = 10;
    employee array[size];
    ifstream inFile;
    readFile(&inFile, array);
    return 0;
}

int readFile (ifstream *inFile, employee array[]) {
    inFile->open("pay.txt");
    if(inFile->fail()) {
        cout << "fail";
        exit(1);
    } else {
        cout << "success";
    }
    return 0;
}

Also note that in C++, it's not necessary (nor common) to write struct employee, just employee is enough (as I did in the code above). Unlike in C, there is no "tag namespace", type names use the same scoping rules as everything else.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.