variable undeclared, first use in this function!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>

using namespace std;

struct state {
       int M_count;
       int C_count;
       int boat;
       struct state* parent;
       int Gscore;
       };
       
       
struct openlist {
       struct state obj;
       struct openlist* next;
       };
       
struct closedlist {

       struct state obj;
       struct closedlist* next;       
       
       };

struct openlist* tail_open = NULL;
struct closedlist* tail_closed = NULL;

struct state move_mc(struct state st){
       
       struct state temp = st;
       if (temp.boat==0){
       temp.boat=1;
       temp.C_count = temp.C_count -1; 
       temp.M_count = temp.M_count -1;  
       temp.*parent = st;
       temp.Gscore++;                
       }
       if (temp.boat==1){
       temp.boat=0;
       temp.C_count = temp.C_count +1; 
       temp.M_count = temp.M_count +1;  
       temp.*parent = st;
       temp.Gscore++;                
       }
       return temp;
       }

This is a peice of my code. The error says:
  
In function `state move_mc(state)':
`parent' undeclared (first use this function)  


please help!
there is no need of keyword struct in the line 29
there is no need of keyword struct in the line 29


That's right but not the problem.

(Putting the "struct" before some struct in C++ is optional - unlike C. It forward-declares the struct "in place". You can declare a stuct as many times as you want. "state" is already defined in line 5, so it's a useless forward declaration.)


The problem at hand here is the typo in line 36 and 43. temp.*parent should most probably be *temp.parent. You want to assign "st" to the place where "temp.parent" points to, right?


(For an interpretation of the error message: There is an operator.*() in C++. It's calling an member function pointer. Your compiler thinks when reading "temp.*parent" that you are going to call a member function pointer to the function stored in the local variable "parent" using "temp" as a this-object. But you don't have any "parent" in current scope, hence the strange looking error.)
Last edited on
(Putting the "struct" before some struct in C++ is optional - unlike C. It forward-declares the struct "in place". You can declare a stuct as many times as you want. "state" is already defined in line 5, so it's a useless forward declaration.)


Thanks for letting me know about the struct imi .
Topic archived. No new replies allowed.