C++ error: field has incomplete type 'int []&#

2019-02-15 07:21发布

问题:

I'm making a virtual machine in C++ and I've run into this error,

error: field has incomplete type 'int []' int instrarr[];

I have absolutely no idea what is wrong with the int array. Can someone take a look and let me know what I've done wrong, I've been looking at it for over an hour and I can't seem to find what tiny detail I must have left out. My entire file is below incase you need it for reference.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

class vm {
    private:

        string finfo;
        string filedata;
        string fconv;
        string instruction;

        int instrarr[];
        int zerocount[];

    public:

        /* Helper functions */

        int countinstrs(string s) {
            int count = 0;

            for (int i = 0; i < s.size(); i++)
                if (s[i] == ',') count++;

                return count;
        }

        int countlzeros(string s) {
            int count = 0;

            for (int i = 0; i < s.size(); i++)
                if (s[i] == '0') {
                    count++;
                } else {
                    i = s.size() + 1;
                }
            return count;
        }

        string load_program(string file) {
            ifstream rdfile(file);
                while(rdfile >> instruction) {
                    filedata += instruction;
                    filedata += ",";
                }
            rdfile.close();
            return filedata;
        }

        string convert_program(string fconv) {
            int instrcount = countinstrs(fconv);
            stringstream hextoint;
            unsigned int value;
            string s = fconv;
            string delimiter = ",";
            size_t pos = 0;
            string token;
            int i = 0;
            while ((pos = s.find(delimiter)) != string::npos) {
                token = s.substr(0, pos);
                int zeroc = countlzeros(token);
                //zerocount[i] = zeroc;
                stringstream hextoint(token);
                hextoint >> hex >> value;
                //instrarr[i] = value;
                cout << value << endl;
                s.erase(0, pos + delimiter.length());
                i++;
            }
            return "";
        }

        void run_program(string file) {
            finfo = load_program(file);
            fconv = convert_program(finfo);
            //execute_program();
        }
};

int main(int argc, char* argv[]) {

    vm rd;
    rd.run_program(argv[1]);
    return 0;

}

回答1:

It's pretty simple, int[] is an incomplete type, as it lacks information about how large it is. In function call parameters, it goes synonymous with declaring a pointer instead of an array, but for defintions, as in your code, the compiler certainly needs to know how large the array is, in order to allocate storage for it.