오류 메시지
C++ 함수에서 예외를 발생시키기 위해 stop() 함수를 호출할 수 있습니다. 함수 add_positive_numbers()의 골격을 제공해 드렸습니다. 이 함수에서 예외를 발생시키도록 완성하세요.
이 연습은 강의의 일부입니다
Rcpp로 R 코드 최적화하기
연습 안내
x가 음수이면 "x is negative" 메시지로 예외를 발생시키세요.y가 음수이면 "y is negative" 메시지로 예외를 발생시키세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
cppFunction('
// adds x and y, but only if they are positive
int add_positive_numbers(int x, int y) {
// if x is negative, stop
if(___) stop("x is negative") ;
// if y is negative, stop
if(___) ___("y is negative") ;
return x + y ;
}
')
# Call the function with positive numbers
add_positive_numbers(2, 3)
# Call the function with a negative number
add_positive_numbers(-2, 3)