gpt4 book ai didi

c++ - 未定义的内部链接和 undefined symbol 错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:24:20 26 4
gpt4 key购买 nike

我尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com