用于体系结构x86_64的C++未定义符号
C++ undefined symbols for architecture x86_64
我知道同一个问题有很多问题,但我没有发现任何接近我的问题。
我正在为C++项目使用Xcode,我得到了以下错误:
Undefined symbols for architecture x86_64:
"Decrypt::printEncryptedString()", referenced from:
_main in main.o
"Decrypt::Decrypt()", 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)
我的主要文件如下:
#include <iostream>
#include "Decrypt.hpp"
int main(int argc, const char * argv[]) {
Decrypt decryption = Decrypt();
decryption.printEncryptedString();
std::cout << "Hello, World!n";
return 0;
}
我的标题:
#ifndef Decrypt_hpp
#define Decrypt_hpp
#include <stdio.h>
#endif /* Decrypt_hpp */
#include <string>
class Decrypt{
std::string toDecrypt;
std::string encrypted;
int keyLength; // key between 2 and 12
public:
Decrypt();
void printEncryptedString();
};
和.cpp文件:
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
class Decrypt{
std::string toDecrypt;
std::string encrypted;
int keyLength;
public:
Decrypt(){
int i;
unsigned char ch;
FILE *fpIn;
fpIn = fopen("ctext.txt", "r");
i=0;
while (fscanf(fpIn, "%c", &ch) != EOF) {
/* avoid encrypting newline characters */
/* In a "real-world" implementation of the Vigenere cipher,
every ASCII character in the plaintext would be encrypted.
However, I want to avoid encrypting newlines here because
it makes recovering the plaintext slightly more difficult... */
/* ...and my goal is not to create "production-quality" code =) */
if (ch!='n') {
i++;
encrypted += ch;
}
}
fclose(fpIn);
}
//void CalculateKeyLength(){}
void printEncryptedString(){
std::cout << encrypted << 'n';
}
};
我不明白是什么原因导致了这些错误。有人能帮帮我吗?
您可以在项目中多次定义Decrypt
类(否则包含头文件会有问题!),但每个定义必须完全相同。
您有两个不同的定义。一种是对其成员函数进行内联定义;另一个没有。
这有未定义的行为,在这里,这显然表现为编译器忽略了.cpp
中的定义,它将"看到"第二个定义。第二个定义包含您的成员函数定义,因此它们不会被纳入构建中。
在您的.cpp
文件中,单独定义您的成员函数,如下所示:
Decrypt::Decrypt()
{
int i;
unsigned char ch;
FILE *fpIn;
fpIn = fopen("ctext.txt", "r");
i=0;
while (fscanf(fpIn, "%c", &ch) != EOF) {
/* avoid encrypting newline characters */
/* In a "real-world" implementation of the Vigenere cipher,
every ASCII character in the plaintext would be encrypted.
However, I want to avoid encrypting newlines here because
it makes recovering the plaintext slightly more difficult... */
/* ...and my goal is not to create "production-quality" code =) */
if (ch!='n') {
i++;
encrypted += ch;
}
}
fclose(fpIn);
}
void Decrypt::printEncryptedString()
{
std::cout << encrypted << 'n';
}
就这样。不在第二个class
定义内。
C++书中关于成员函数的章节将向您解释这一切,以及更多内容。
还要注意,Decrypt.hpp
中的头保护是在文件的中间而不是结尾"关闭"的,这不是故意的吗?
您对Decrypt::printEncryptedString()
的定义与您的声明不同。在您的声明中,它被命名为void Decrypt::printEncryptedString
,您的定义定义了函数inline void Decrypt::printEncryptedString
。如果从函数定义中删除inline
关键字,则应该进行编译。
相关文章:
- 编译C++时未定义的引用
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- c++11评估顺序(未定义的行为)
- 使用mysql c++连接器的未定义引用
- 从python调用openMP共享库时,未定义opnMP函数
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号
- Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseE
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 具有外部"c"和程序集的未定义函数
- 此增量后语句是否会导致未定义的行为?
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 对 Scalar ::Scalar() 的未定义引用