Can't define ++ operator in C++, what's the issue here?

后端 未结 3 836
终归单人心
终归单人心 2021-01-24 07:30

I\'m working through Bjarne Stroustrup\'s The C++ Programming Language and I\'m stuck on one of the examples. Here\'s the code, and aside from whitespace differences and comment

3条回答
  •  不思量自难忘°
    2021-01-24 07:58

    You have a couple of problems:

    First, you are attempting to implement a function (operator++) inside another function (main). C++ does not allow this (with the exception of lambdas).

    Second, you are only implementing a prefix increment, so ++light will work, but light++ will not. You should implement them both. An example of it can be found here.

提交回复
热议问题