简体   繁体   中英

Supposed “syntax error” in my code,“missing ';' before '.'”

I'm relatively new to this level of coding and am really hitting my wall on this error:

[Line 65] error C2143: syntax error : missing ';' before '.'

Obviously this would usually mean a syntax error in my code, but I can't find it flippin anywhere. Any assistance (or a second set of eyes to see my error would be appreciated).

Here is the code snippet in question:

class RacingCar {

public: 
            Wheel* wheels[4];

            RacingCar()
            { 
                wheels[0] = new Wheel;
                wheels[1] = new Wheel;
                wheels[2] = new Wheel;
                wheels[3] = new Wheel;
            }

            RacingCar( RacingCar& refOldCar)
            {
//              new Wheel(refOldCar.wheels[0]->pressure);
                wheels[0] = new Wheel;
                wheels[1] = new Wheel;
                wheels[2] = new Wheel;
                wheels[3] = new Wheel;

                int a = refOldCar.speedCopy();
                **RacingCar.setSpeed(10);**
                RacingCar.Brake(50);
                RacingCar.Print();
                RacingCar.speed = refOldCar.speed;
            }

Thanks a ton for any help

This:

RacingCar.setSpeed(10);

should be:

setSpeed(10);

which is shorthand for:

this->setSpeed(10);
RacingCar.setSpeed(10);

there is no such syntax. Write:

RacingCar::setSpeed(10);

or

setSpeed(10);

or

this->setSpeed(10);

Note that they all may have different meaning, although will probably work the same in your context.

您正在使用类和构造函数(与类相同)的名称-例如RacingCar而不是refOldCar,此名称或直接访问成员。

Since the speed is unlikely to be a class-level property (affecting all cars), you probably meant to write:

this->setSpeed (10); // or, for the less anal, just "setSpeed(10);" :-)

This more correctly makes it an attempt to set the object-level property. A class-level property would reference RacingCar::SomethingOrOther (double-colon rather than dot) which is why it's thinking you need to close off the current statement before that . character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM