[英]C++ undefined symbols for architecture x86_64
我知道同一問題有很多問題,但是我沒有發現任何與我的問題相近的問題。
我將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)
我的主要文件如下:
#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 void Decrypt::printEncryptedString
。 如果從函數定義中刪除inline
關鍵字,則應進行編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.