I am a beginner in programming C++ and I'm learning all about classes and objects right now. For practice, I created a class called Employee and added some members in it. But I noticed that the record array is giving me an error stating a nonstatic member reference must be relative to a specific object. This only happens whenever I create an array in Employee class. But when I called it in my constructor Employee(), it is not highlighted as an error, as well as when I tried initializing it as a global variable, or even a local variable in my main.cpp(this is where main() is located). Kindly give advice or even better solution for this.
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Employee
{
private:
int recordSize = 100;
int fieldSize = 4;
string record[recordSize][fieldSize];
public:
Employee();
~Employee();
};
main.cpp:
#include "Employee.h"
#include<string>
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
Employee::Employee() {
ifstream inFile;
inFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt");
for (int index = 0; index < recordSize; index++) {
for (int index2 = 0; index2 < fieldSize; index2++) {
inFile >> record[recordSize][fieldSize];
}
}
}
Employee::~Employee()
{
}
I also include content of employee-info.txt
ID Firstname Lastname Sales
1 Bruno Mars 120000.00
2 Lebron James 150000.00
using namespace std;– never in a header file. –C:\\Users\\RJ\\Desktop\\employee-info.txt– content?string record[recordSize][fieldSize];-- This is not valid C++. It is the same error if you did this:int main() { int recordSize = 100; std::string record[recordSize];}, So are your arrays fixed size, or can they vary in size at runtime? The answer depends on what your intent is here.inFile >> record[recordSize][fieldSize];should beinFile >> record[index][index2];