Error: initial value of reference to non const must be lvalue 原因以及解决方法

有如下C++函数声明

static int greatest_common_divisor(int &a, int &b);

如下调用方式报错: 

Assert::AreEqual(6, CppAlgorithms::greatest_common_divisor(18, 12), L"Expect result is 6.");

" initial value of reference to non const must be lvalue "

究其原因是 引用类型变量如果不是const, 必须赋以左值。 int a = 18, a 是左值,18是右值。下边这样调就好了。

int a = 12, b=18;

Assert::AreEqual(6, CppAlgorithms::greatest_common_divisor(a, b), L"Expect result is 6.");

 

你可能感兴趣的:(reference)