简体   繁体   English

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

[英]C++ undefined symbols for architecture x86_64

I know there are many questions with the same issue but I did not find anything close to my problem. 我知道同一问题有很多问题,但是我没有发现任何与我的问题相近的问题。

I am using Xcode for a C++ project and I get the following errors: 我将Xcode用于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)

My main file is the folowing: 我的主要文件如下:

#include <iostream>
#include "Decrypt.hpp"

int main(int argc, const char * argv[]) {
    Decrypt decryption = Decrypt();
    decryption.printEncryptedString();
    std::cout << "Hello, World!\n";
    return 0;
}

My header: 我的标题:

#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();

};

and the .cpp file: 和.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';
    }

};

I do not understand what causes the errors. 我不明白是什么原因导致了错误。 Can someone help me please? 有人能帮助我吗?

You are allowed to define the Decrypt class multiple times in your project (otherwise inclusion of header files would be a problem!), but each definition must be exactly the same. 您可以在项目中多次定义Decrypt类(否则会包含头文件会是一个问题!), 但是每个定义必须完全相同。

You have two definitions that are different from each other. 您有两个彼此不同的定义。 One has inline definitions of its member functions; 一个对其成员函数有内联定义; the other does not. 另一个没有。

This has undefined behaviour and, here, that is apparently manifesting as your compiler ignoring the definition in the .cpp , which it'll "see" second. 这具有未定义的行为 ,在这里,这显然表现为您的编译器忽略了.cpp的定义,第二个它将“看到”。 That second definition contains your member function definitions, so they're not making it into the build. 第二个定义包含您的成员函数定义,因此它们没有将其纳入构建。

In your .cpp file, define your member functions individually like this: 在您的.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';
}

Just like that. 就这样 Not inside a second class definition. 不在第二class定义之内。

The chapter on member functions in your C++ book will explain all this to you, and more. C ++书籍中有关成员函数的章节将向您以及所有更多内容进行解释。

Also note that your header guard in Decrypt.hpp has been "closed" in the middle of the file rather than at the end, which cannot be intentional? 还要注意,您在Decrypt.hpp中的标头保护已在文件中间而不是在结尾处“关闭”,这不是故意的吗?

Your definition of Decrypt::printEncryptedString() is different than your declaration. 您对Decrypt::printEncryptedString()定义与声明不同。 In your declaration, it is named void Decrypt::printEncryptedString and your definition defines the function inline void Decrypt::printEncryptedString . 在您的声明中,它被命名为void Decrypt::printEncryptedString并且您的定义定义了inline void Decrypt::printEncryptedString函数inline void Decrypt::printEncryptedString If you remove the inline keyword from your function definition, it should compile. 如果从函数定义中删除inline关键字,则应进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM