1

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
11
  • 3
    using namespace std; – never in a header file. – C:\\Users\\RJ\\Desktop\\employee-info.txt – content? Commented May 2, 2019 at 7:58
  • 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. Commented May 2, 2019 at 8:01
  • 1
    You need to read some more about arrays in your favourite C++ book. Commented May 2, 2019 at 8:03
  • What you want to achive with your code? Array of Strings? Commented May 2, 2019 at 8:03
  • inFile >> record[recordSize][fieldSize]; should be inFile >> record[index][index2]; Commented May 2, 2019 at 8:09

2 Answers 2

3

The variables recordSize and fieldSize need to be both static and const (or constexpr) to be used as dimensions of std::string record 2D array.

They need to be const because the array bound should be an integer constant.

They need to be static because the record array is not allowed to have different sizes for different instantiations of Employee class.

If you want the 2D array to be able to hold different sizes in different instantiations of Employee class, you should go for an std::vector of std::vector<std::string> instead.

Sign up to request clarification or add additional context in comments.

2 Comments

This one answered my problem. Great info and advice. I still need to practice more tho. long way to go.
In fact, I would always recommend going for std::vector first, and then think about changing to std::array or a raw array if you find there is a need to. You certainly want a std::vector of records; for each individual record you might well prefer a struct/class.
-1

The sizes need to be constexpr when used to define array sizes

static constexpr int recordSize = 100;
static constexpr int fieldSize = 4;
string record[recordSize][fieldSize];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.