Structure variable forward declaration, prototype declaration error

Asked 2 years ago, Updated 2 years ago, 133 views

I wanted to use a queue with a Problem structure variable as a type, so I declared a variable, but there was an error, so I'm uploading a question.

For example, if you have a structureProblem{};

I want to use a variable called queueQ; but I used an undefined format.

Display error C2027.

What's interesting is that there's an error when you do a queue, but there's no error

Below is the source code.

using namespace std;

structure Problem;//structure type forward declaration

queue<problem>Q;//where error occurs

If I write English in vector<problem>Bank;//<>, I can't see it on the screen, so I wrote it in Korean.

struct Problem {

intarr[5] = {3,3,0,0,0};//Initial state;

 int brr[5];

 Problem() {//Generator
          for (int i = 0; i < 5; i++) {   brr[i] = arr[i];     }
         Problem a = *this;    Bank.push_back(a);
       }

         voidoriginal() {//keep the original
         for (int i = 0; i < 5; i++) { arr[i] = brr[i];}            
          }
     void clear() {//move to pre-migration state
          for (int i = 0; i < 5; i++) {   brr[i] = arr[i];  }
         }

};

int main() { Problem mc;

cout << mc.arr[0] << mc.arr[1] << mc.arr[2] << mc.arr[3] << mc.arr[4] << endl; return 0; }

struct prototype queue vector forward-declaration

2022-09-21 15:28

1 Answers

The reason for the C2027 error is because Queue<Problem>Q; is not defined when the variable is generated.

A forward declaration only tells you that there is a type of this name, but it does not tell you the definition of that type.

Therefore, the two variables queue<Problem> Q; and vector<Problem> Bank; must be defined in Problem and .

However, if Q and Bank are lowered below the Problem<:/code> definition, Problem::Problem() will depend on Bank

Therefore, the definitions of Q and Bank should be lowered, and the definitions of Problem::Problem() should also be lowered.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Problem
{
    intarr[5] = {3, 3, 0, 0, 0}; //Initial state;
    int brr[5];

    Problem();

    void original()
    { //Keep original
        for (int i = 0; i < 5; i++)
        {
            arr[i] = brr[i];
        }
    }

    void clear()
    { //Move to pre-migration state
        for (int i = 0; i < 5; i++)
        {
            brr[i] = arr[i];
        }
    }
};

queue<Problem> Q;
vector<Problem> Bank;

Problem::Problem()
{ //Generator
    for (int i = 0; i < 5; i++)
    {
        brr[i] = arr[i];
    }
    Problem a = *this;
    Bank.push_back(a);
}

int main()
{
    Problem mc;
    cout << mc.arr[0] << mc.arr[1] << mc.arr[2] << mc.arr[3] << mc.arr[4] << endl;
    return 0;
}

If you want to leave two variables Q and Bank on top, replace the two containers std::vector and std:queue with a non-procode template.


2022-09-21 15:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.