nilla hong _
nilla hong _

Reputation: 3

vs2017 nonstandard syntax. Use ' & ' to create a member pointer

I get this error

USCENEComponent = GetComponentRotation : nonstandard syntax. Use & to create a member pointer.

when compiling this code:

if (!Player_isDead) {
    FVector fwd = GetActorForwardVector();
    FVector muzzle = GetMesh()->GetBoneLocation("pinky_01_r");
    muzzle += fwd * 155;

    AProjectile* bullet = GetWorld()->SpawnActor<AProjectile>(BPProjectile, muzzle, RootComponent->GetComponentRotation); // <<===== error
    if (bullet) {
        //bullet->Firer = this;
        bullet->ProxSphere->AddImpulse(fwd*BulletLaunchImpulse);
    }
    else {
        GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Yellow, "no bullet actor could be spawnd. is the bullet overlapping something?");
    }
}

What should I add? I don't know offhand. Please let me know in detail. ㅠㅠ

Upvotes: 0

Views: 80

Answers (2)

Pete Becker
Pete Becker

Reputation: 76438

USCENEComponent = GetComponentRotation should be USCENEComponent = GetComponentRotation() (or some variant with an appropriate argument list). GetComponentRoation() is a function call; in C++ you can’t call a function without the parentheses, even if it takes no arguments.

The warning is a bit misleading. It’s really aimed at creating a pointer-to-member-function. Like this:

struct C {
    void f();
};

void (C::*pmf)() = &C::f;

That bit on the right-hand-side creates a pointer to the member function f of the struct C.

The business about “non-standard syntax” is a Microsoftism. Any reasonable compiler would tell you that you need parentheses to call a function. The reason that Microsoft gives you this warning is that back in the olden days, Microsoft decided that it made sense to be able to create a pointer to member function without mentioning the class name if the context provided it for you and, while you were being lazy, without the &. So this was legal with Microsoft’s compiler:

struct C {
    void f();
    void g() { void (C::*pmf)() = f; }
};

They even floated a paper at one of the standards meetings suggesting that the standard should allow this syntax. That didn’t go anywhere.

So, having (erroneously) allowed that syntax, they opted for backward compatibility by giving a misleading warning for code that uses their invalid syntax. Formally, that’s okay: the only requirement that the standard imposes for non-conforming code is that the compiler “issue a diagnostic”, and every compiler I know of considers a warning to be a diagnostic.

Upvotes: 1

MSalters
MSalters

Reputation: 179981

GetComponentRotation is a method. What do you want to do with the method? Call it, or take its address? We don't know, nor does the compiler. It guesses you wanted the address. My guess would be that you wanted to call GetComponentRotation. In that case, add the argument list, even if it's empty: RootComponent->GetComponentRotation( ).

Upvotes: 1

Related Questions