문제

호출하려고합니다 hi 또는 std::move 그것에 다음과 같은 결과를 얻습니다.

/tmp/cch3DRvH.o: In function `main':

main.cpp:(.text.startup+0x5): undefined reference to `hi(int, char**)'

collect2: error: ld returned 1 exit status
#include <type_traits>
#include <utility>
#include <iostream>
#include <typeinfo>
#include <functional>

int main(int argc, char* argv[])
{
    decltype(main) hi;
    decltype(main) hi2;
    // std::function<int(int, char**)> hi2 = std::move(hi);
    // hi(argc, argv);
    std::cout << std::boolalpha;
    std::cout << std::is_same<decltype(main), decltype(hi)>::value << std::endl;
    std::cout << std::is_function<decltype(main)>::value << std::endl;
    std::cout << std::is_function<decltype(hi)>::value << std::endl;
    std::cout << std::is_same<decltype(std::move(main)), decltype(std::move(hi))>::value << std::endl;
    std::cout << std::is_same<decltype(hi2), decltype(hi)>::value << std::endl;
    return 0;
}

산출:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:16:54: warning: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]
     std::cout << std::is_same<decltype(std::move(main)), decltype(std::move(hi))>::value << std::endl;
main.cpp:16:54: warning: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]
true
true
true
true
true

보인다 hi 거의 동일합니다 main 사용할 때와 같은 경고를 제외하고는 제외합니다 std::cout << hi. 이 프로그램을 출력하지 못하게 할 수 있습니까? undefined reference to안녕하세요 (int, char **) '`?

도움이 되었습니까?

해결책

decltype(main) 유형의 별칭입니다 int(int, char **). 당신은 함수를 선언했습니다 hi 이 유형은이 유형이지만 정의하지 않았습니다.

제거하기 위해 undefined reference 오류, 함수를 정의합니다.

int hi( int, char ** ) {
    return 0;
}

이전에 나타나는 메시지 true 프로그램이 아니라 컴파일러에서 출력됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top