C++ C++;架构x86_64的未定义符号

C++ C++;架构x86_64的未定义符号,c++,xcode,C++,Xcode,我知道有很多问题与同一个问题,但我没有发现任何接近我的问题 我使用C++代码作为一个C++项目,我得到以下错误: Undefined symbols for architecture x86_64: "Decrypt::printEncryptedString()", referenced from: _main in main.o "Decrypt::Decrypt()", referenced from: _main in main.o ld: symbol(

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

我使用C++代码作为一个C++项目,我得到以下错误:

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;
}
#包括
#包括“Decrypt.hpp”
int main(int argc,const char*argv[]{
解密=解密();
解密。printEncryptedString();

std::cout您对
Decrypt::printEncryptedString()的定义
与您的声明不同。在您的声明中,它被命名为
void Decrypt::printEncryptedString
,您的定义定义了函数
inline void Decrypt::printEncryptedString
。如果您从函数定义中删除
inline
关键字,它应该被编译。

您可以定义在项目中多次解密
类(否则包含头文件将是一个问题!),但每个定义必须完全相同

您有两个相互不同的定义。一个有其成员函数的内联定义,另一个没有

这有未定义的行为,在这里,这显然表现为编译器忽略了
.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';
}
Decrypt::Decrypt()
{
int i;
无符号字符ch;
文件*fpIn;
fpIn=fopen(“ctext.txt”,“r”);
i=0;
而(fscanf(fpIn、%c、&ch)!=EOF){
/*避免加密换行符*/
/*在Vigenere密码的“真实世界”实现中,
明文中的每个ASCII字符都将被加密。
但是,我不想在这里加密换行符,因为
这使得恢复明文稍微有点困难*/
/*…我的目标不是创建“生产质量”代码=)*/
如果(ch!='\n'){
i++;
加密+=ch;
}
}
fclose(fpIn);
}
void Decrypt::printEncryptedString()
{

std::你能确定是否也编译了带有解密实现的源文件吗?顺便说一句,endif需要位于头文件的末尾。这不是方法。你在cpp文件中声明了一个全新的类
Decrypt
。你应该
包含
头文件,然后只实现成员函数。哦,
\eNEDF <代码>对于包保护应该在文件的末尾,而不是在中间。@博森感谢……对于愚蠢的问题:)它已经编译了,并且恐怕它看起来像<代码>内联是一个错误,因为它已经从代码中删除了。无论如何,函数在SeCo中无论如何都是隐含的<代码>内联< /代码>。nd定义所以,虽然我没有检查,但我认为这是合法的。比现在更重要的是,来考虑一下。忽略所有UB的事实,当然……问题也存在,没有内联。解决方案,已经由我问题中的评论给出。我喜欢你的答案anyway@Rorschach:是的,我知道:我的建议与薄熙来评论中给出的答案相同。但是,答案不应作为评论给出,而应作为答案给出。这一条为您提供了更多的细节,更不用说解释实际发生的事情了。
#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::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';
}