如何将函数返回的2个值分成2个不同的变量C++

The*_*ian 1 c++ function

返回值的函数是this

float calcVelocity(float xacceleration, float yacceleration,sf::Clock clock, float originalDistance){
    sf::Time time = clock.getElapsedTime(); //get current time and store in variable called time
    float xvelocity = xacceleration*time.asSeconds();
    float yvelocity = yacceleration*time.asSeconds();
    while (!(originalDistance + calcDisplacement(yacceleration, clock, originalDistance) <= 0)) {
        time = clock.getElapsedTime(); //get current time and store in variable called time
        xvelocity = xacceleration*time.asSeconds();//Calculates velocity from acceleration and time
        yvelocity = yacceleration*time.asSeconds();
        cout << xvelocity<<endl;//print velocity
        cout << yvelocity << endl;
        system("cls");//clear console
    }
    return xvelocity;
    return yvelocity;
}
Run Code Online (Sandbox Code Playgroud)

然后我希望它们在while循环完成后打印为finalXvelocity = blah和finalYvelocity = blah.在主代码中,当我调用函数并输出结果时,它会将两个值一起打印.例如,finalXvelocity = blahblah.

我想我可以将返回的值分离到主代码中,然后使用那些打印它们,但我不知道该怎么做.

谢谢

Bat*_*eba 5

使用struct:

struct velocity
{
    float x_component; /*ToDo - do you really need a float*/
    float y_component;
};
Run Code Online (Sandbox Code Playgroud)

这将是最具扩展性的选择.您可以扩展以提供构造函数和其他细节,例如计算速度.也许a class更自然,private默认情况下数据成员.