Dhyana
Dhyana

Reputation: 51

Class Syntax Issue

Super new to this. Please explain in the dumbest way possible.

I want to call the .size() function on an array and make it a parameter of the for loop so I can go through it index by index. However, when I type:

encryptedtxt.size() 

I get an error suggesting I need a class specifier. When I include the specifier:

FileDecrypt.encryptedtxt.size()

It says the type name is not allowed.

I've included the relevant stuff below, if it helps understand my question.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class FileDecrypt {
public:
    int x;
    int encryptedtxt[4];
    ifstream encryptedchars;

    int readin() {
        encryptedchars.open("encrypted.txt");
        for (int i=0;i < 4;i++) {
            encryptedchars >> encryptedtxt[i];
        }
        return 0;
    }

    int size = FileDecrypt.encryptedtxt.size();
    int decrypt() {
        for (int j = 0;j < encryptedtxt.size();j++) {
        }
    }
};

Upvotes: 1

Views: 41

Answers (1)

john
john

Reputation: 87932

Dumbest way possible. There is no size function on an array.

In C++ you should use std::vector or std::array both of which have size functions.

E.g.

#include <array>

std::array<int, 4> encryptedtxt;

Regular arrays are something that C++ inherits from C, but C++ has far superior alternatives. Either std::array if you want a fixed size array, or std::vector if you want a variable size array.

Upvotes: 3

Related Questions