用于体系结构x86_64的C++未定义符号

C++ undefined symbols for architecture x86_64

本文关键字:C++ 未定义 符号 体系结构 x86 用于      更新时间:2023-10-16

我知道同一个问题有很多问题,但我没有发现任何接近我的问题。

我正在为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关键字,则应该进行编译。