some issues with functions... or DEV

I am trying to define the member functions of some classes and i am getting some strange error messages

a function-definition is not allowed here before '{' token
expected `,' or `;' before '{' token
expected `}' at end of input

but none of these fit with cpp syntax

The offending function (first two errors)
char factor::dfactor(char x)
'{' is on next line

and my main function (last error)
1
2
3
4
5
6
7
8
9
int main()
{
    bool asd=false;
    while (!asd)
    {
          initialize();
    }
    return 0;
}


if anyone knows what is going on i would like to know, the first two errors only occur on that function, not the others, which to me look syntactically identical.
Last edited on
You have to post the code that is causing the compile error. main() does not help.
it is longish but...

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class term
{
    Token b;
    factor e;
    int value=e.dfactor(look);
    char tok=b.match(look);
    public:
    char dterm();
    char dterm(char x);
    
};
char term::dterm()
{
    tok=b.match(look);
    if(tok=='*' || tok=='/')
    {
         if(tok=='*'){return value*=e.dfactor(look);}
         else if(tok=='/'){return value/=e.dfactor(look);}
         return value;
    }
}

char term::dterm(char x)
{
    tok=b.match(x);
    if(tok=='*' || tok=='/')
    {
         if(tok=='*'){return value*=e.dfactor(x);}
         else if(tok=='/'){return value/=e.dfactor(x);}
         return value;
    }
}
////end term/////////////
/////////////////////////
////start of expression//
class expression
{
    Token a;
    term d;
    static int value;
    static char tok;
    public:
    char dexpression();
    char dexpression(char x);
};

char expression::dexpression()
{
    if (a.isUnary(look)){value=0;}
    else{value=d.term(look);}
    if(a.isUnary(look))
    {
         tok=match(look);
         if(tok=='+'){return value+=d.dterm(look);}
         else if(tok=='-'){return value-=d.dterm(look);}
         return value;
    }
}

char expression::dexpression(char x)
{
    if (a.isUnary(x)){value=0;}
    else{value=d.term(x);}
    if(a.isUnary(x))
    {
         tok=match(x);
         if(tok=='+'){return value+=d.dterm(x);}
         else if(tok=='-'){return value-=d.dterm(x);}
         return value;
    }
////end expression///////
/////////////////////////

//parsefunctions.hpp is needed for some of these classes so it is is included above
//this but the initialize function needs the expression class so it is located here
int initialize()
{
     inittable();
     getline(cin,lookstr);
     expression start;
     for(unsigned int i;lookstr[i];i++)
     {
          look=lookstr[i];
          if(!isspace(look)){cout << start.dexpresion(look); continue;}
          else {continue;}
     }
     return 0;
}

}


here is the complete main function, yes it is this short

1
2
3
4
5
6
7
8
9
10
11
using namespace std; 

int main()
{
    bool asd=false;
    while (!asd)
    {
          initialize();
    }
    return 0;
}
Last edited on
Lines 5 and 6 are not legal syntax.

None of your class member functions return a value in all cases, so you should be getting warnings like "control reaches end of non-void function".

Fix lines 5 and 6 and see what happens.
Try using constructor for class term to initialize value & tok variables.
Those both helped but now cin, cout, and getline don't work for some reason.
Line 81 i is uninitialized.
How so, I didn't change it from one time to another and I don't see how my class changes would affect the for loop.
for(unsigned int i;lookstr[i];i++)

what is the initial value of i?

wow... i don't know how i missed that, but no those three things are still not working, besides getline and cin are before tho loop
Last edited on
How do you know they are not working?

cout lookstr before the for loop and see what its value is.
because i get the compile error... 'cout' undeclared
If you are including iostream, then it is std::cout.
Oh nvm i forgot to add using namespace std; at the beginning. This is one stupid mistake after another... Now all i get are linker errors.
Last edited on
Topic archived. No new replies allowed.