我尝试在Xcode上编译一个简单程序,并收到以下消息:

function<anonymous namespace>::Initialize' has internal linkage but is not defined

function<anonymous namespace>::HandleBadInput' has internal linkage but is not defined

Undefined symbols for architecture x86_64:
  "(anonymous namespace)::Initialize()", referenced from:
      _main in main.o
  "(anonymous namespace)::HandleBadInput()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


头文件如下所示:

#ifndef WJKErrorHandling
#define WJKErrorHandling

namespace WJKErrorHandling{

    void Initialize(void);
    int HandleBadInput(void);

}

#endif // defined(WJKErrorHandling)


实现文件如下所示:

#include <iostream>
#include "WJKErrorHandling.h"

namespace WJKErrorHandling{

    void Initialize(void){

        std::cin.exceptions(std::cin.failbit);
    }

    int HandleBadInput(void){

        std::cerr << "Input Error: wrong type?\n";
        std::cin.clear();

        char BadInput[5];
        std::cin >> BadInput;

        return 1;
    }

}


并且main.cpp看起来像这样:

#include <iostream>
#include "WJKErrorHandling.h"


void Prompt (void){

    //Prompts the user to begin entering numbers

    std::cout << "Begin entering numbers: \n";
}

float GetNumber (void){

    std::cout << "Number: \n";
    float Number;
    std::cin >> Number;
    return Number;
    }

std::string GetString (void){

    std::cout << "String: \n";
    std::string String;
    std::cin >> String;
    return String;

}

int main()
{
    Prompt();
    WJKErrorHandling::Initialize();

    int ReturnCode = 0;

    try{

        float Number = GetNumber();
        std::cout << Number;
        std::string String = GetString();
        std::cout << String;

        std::cout << "SUCCESS!!!!\n";

    }
    catch(...){

        ReturnCode = WJKErrorHandling::HandleBadInput();
    }


    return ReturnCode;
}


到目前为止,我一直在尝试寻找答案,但是我还不了解自己找到的任何帖子。我是C ++的新手,所以将不胜感激!

最佳答案

您的#define Guard导致名称查找问题。

更改为以下样式应可解决此问题:

#ifndef WJK_ERROR_HANDLING_H
#define WJK_ERROR_HANDLING_H

关于c++ - 未定义的内部链接和 undefined symbol 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13536856/

10-12 20:32