1

I have "This declaration has no storage class or type specifier" error in Visual Studio than I try to initialize my array with functions. I'm declare an array in header:

typedef void (*MultimethodFunc)(ofstream& out);

extern MultimethodFunc multimethodFunc[][10];

In .cpp file I have this:

MultimethodFunc multimethodFunc[10][10];

And finally I try to initialize it in another .cpp:

void TrainTrainOut(ofstream& out) {
    out << "\nTrain and Train" << endl;
}

void MMTrainTrainOut(ofstream& out) {
    return TrainTrainOut(out);
}

multimethodFunc[1][1] = MMTrainTrainOut;
multimethodFunc[1][2] = MMTrainAirplaneOut;
multimethodFunc[2][1] = MMAirplaneTrainOut;
multimethodFunc[2][2] = MMAirplaneAirplaneOut;

Another functions just similar to TrainTrainOut and MMTrainTrainOut with different outs and matching returns. The error is placed in last strings there I try to assign functions to arrray. What I'm doing wrong in this case? Thank you!

4
  • 1
    Those aren't initializations those are assignments. MultimethodFunc muntimethodFunc[10][10] = { /* values here */ }; that would be an initialization. Commented Jan 8, 2020 at 4:55
  • @Wyck, thanks, changed the title of this question. Commented Jan 8, 2020 at 4:59
  • You can't put an assignment at file scope. Put the assignments in a function and call the function. Commented Jan 8, 2020 at 4:59
  • @Wyck, how can I do that? Just make another function and place assignments in it? Commented Jan 8, 2020 at 5:03

1 Answer 1

2

You can't put an assignment at file scope.

The following program gives the error:

this declaration has no storage class or type specifier

#include <fstream>

using namespace std;

typedef void (*MultimethodFunc)(ofstream& out);

void TrainTrainOut(ofstream& out) {
    out << "\nTrain and Train" << endl;
}

void MMTrainTrainOut(ofstream& out) {
    return TrainTrainOut(out);
}

MultimethodFunc multimethodFunc[10][10];

multimethodFunc[1][1] = MMTrainTrainOut;  /* ERROR HERE */
multimethodFunc[1][2] = MMTrainTrainOut;
multimethodFunc[2][1] = MMTrainTrainOut;
multimethodFunc[2][2] = MMTrainTrainOut;

int main() {

}

The following program compiles. It fixes the problem by moving the assignments into an initialization function and calling the initialization function explicitly somewhere (e.g.: main)

#include <fstream>

using namespace std;

typedef void (*MultimethodFunc)(ofstream& out);

void TrainTrainOut(ofstream& out) {
    out << "\nTrain and Train" << endl;
}

void MMTrainTrainOut(ofstream& out) {
    return TrainTrainOut(out);
}

MultimethodFunc multimethodFunc[10][10];


void initializeThings() {
    multimethodFunc[1][1] = MMTrainTrainOut;
    multimethodFunc[1][2] = MMTrainTrainOut;
    multimethodFunc[2][1] = MMTrainTrainOut;
    multimethodFunc[2][2] = MMTrainTrainOut;
}

int main() {
    initializeThings();
}
Sign up to request clarification or add additional context in comments.

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.