error C2143: syntax error : missing ';' before '->'

Feb 8, 2013 at 11:14pm
code deleted
Last edited on Feb 10, 2013 at 5:42am
Feb 8, 2013 at 11:15pm
Errors:

GeometryHomework3.cpp(192) : error C2143: syntax error : missing ';' before '->'
GeometryHomework3.cpp(193) : error C2181: illegal else without matching if
GeometryHomework3.cpp(194) : error C2143: syntax error : missing ';' before '->'
GeometryHomework3.cpp(195) : error C2181: illegal else without matching if
GeometryHomework3.cpp(196) : error C2143: syntax error : missing ';' before '->'
GeometryHomework3.cpp(197) : error C2181: illegal else without matching if
GeometryHomework3.cpp(198) : error C2143: syntax error : missing ';' before '->'
GeometryHomework3.cpp(199) : error C2181: illegal else without matching if
GeometryHomework3.cpp(200) : error C2143: syntax error : missing ';' before '->'
GeometryHomework3.cpp(201) : error C2181: illegal else without matching if
GeometryHomework3.cpp(202) : error C2143: syntax error : missing ';' before '->'
GeometryHomework3.cpp(224) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(229) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(230) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(235) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(241) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(247) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(248) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(254) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(255) : error C2143: syntax error : missing ';' before '.'
GeometryHomework3.cpp(256) : error C2143: syntax error : missing ';' before '.'
Feb 8, 2013 at 11:17pm
I have no idea how to fix it, can anyone help?
Feb 8, 2013 at 11:32pm
On line 192, 194, 196, 198, 200 and 202 you're trying to call a method of a class name. Setting aside the topic of static functions, to use the arrow operator you must have a pointer to an instance of the class. In this case it would be, for example., 's' defined on line 129.
Last edited on Feb 8, 2013 at 11:34pm
Feb 8, 2013 at 11:39pm
I think, for example, Square* s = new Square(token); is supposed to be in the scope of if.

So the problem will be the way I called the print function or the way I claim print function?
Last edited on Feb 8, 2013 at 11:39pm
Feb 8, 2013 at 11:48pm
What do you mean by claim?

Anyway, to call a member function you do
1
2
Square* s = new Square(token);
s->print(shapes[j]);

I think, for example, Square* s = new Square(token); is supposed to be in the scope of if.

Correct. 's' won't actually be accessible from line 192 because it's outside the scope where it was declared. Since you assign 's' to 'shapes[i]' I suppose you can cast that to a Square* and use it, but it seems useless to do all this work.
Feb 8, 2013 at 11:59pm
What the print function call was, before i put it into class, it's like:

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
void printSquare(const Square*);

void printSquare(const Square* a)
{
  double area;
  area = a->side * a->side;
  double perimeter;
  perimeter = a->side * 4;

  cout.unsetf(ios::fixed|ios::showpoint);
  cout << setprecision(6);

  cout<< "SQUARE" <<" side=" <<a->side;
  cout.setf(ios::fixed|ios::showpoint);
  cout<<setprecision(2) << " area="<<area<<" perimeter="<<perimeter<<endl;
  cout<<endl;
}

int main()
{
...

  for(int j = 0; j < i ;j++ )
    {
      if (shapeID[j]==square)
        printSquare((Square*)shapes[j]);
...
    }
}


That's what you mean? Having a Square* to cast it.
Feb 9, 2013 at 12:10am
Anyway, do u have any idea how to fix it?
Feb 9, 2013 at 12:13am
I noticed that print() functions are declared as not taking parameters. My mistake, sorry. In this case a call would look like s->print();

---------------

That's almost what I meant. You would do that if the function was global. With a pointer to an instance it should be ((Square*)shapes[j])->print();
Last edited on Feb 9, 2013 at 12:27am
Feb 9, 2013 at 12:28am
If I do (Square*)shapes[j]->print();
There is the error:
left of '->print' must point to class/struct/union/generic type
type is 'void *'

for line 192, 194, 196, 198, 200 and 202
Feb 9, 2013 at 12:34am
I tried compiling your code. I was able to modify it to compile without errors, but I didn't check if it worked. You need to:
Fix the call syntax from line 192 to line 202 by either making the pointers you create accessible in that scope or casting the void pointers in the shapes array to the correct type and use those. Also the functions don't want any argument
Fix the classes constructors. You put the class name before the variable and that is wrong.

left of '->print' must point to class/struct/union/generic type
type is 'void *'
Yeah, I wrote that wrong, sorry (I am still a beginner after all). I edited the previous post
Last edited on Feb 9, 2013 at 12:36am
Feb 9, 2013 at 12:45am
So
casting the void pointers in the shapes array to the correct type
you mean change function call to printSquare((Square*)shapes[j]);?
Feb 9, 2013 at 12:52am
i think what I should do is casting the void pointers in the shapes array to the correct typelike you said, but how to do that?
Feb 9, 2013 at 12:55am
I mean changing it to ((Square*)shapes[j])->print();. You first post doesn't have any printSquare() function. But if you prefer to get rid of the print() methods and use global functions then there's no problem.

--------------

I just looked at the implementation of the print() methods. You create an uninitialized pointer and use that to make calculations. This will make your progream crash.
Feb 9, 2013 at 1:03am
WOOOOOO, Great! It's working!!

Ya, You create an uninitialized pointer and use that to make calculations. This will make your program crash., you are right, there is warning for it in the compiler, I delete all those uninitialized pointer, also delete all the a->, they shouldn't be there. Because I couldnt make program work b4, I added them there....

Thank you so much for your helping! I really appreciated! :D
Topic archived. No new replies allowed.