此问题已在此处有答案:
Friend function defined inside class not seen by compiler [duplicate](1个答案)
2天前关闭。
我有一个问题,把命名空间在单独的文件。当我把所有东西都写在一个文件中时,一切都很好用,但是当我试图创建cpp+hpp文件时,这就是一切的开始。也许我在头文件中做错了什么,或者文件链接不正确?我真的不知道我将非常感谢你的帮助。
这是我的错误。在86x_64上,它也将其抛出,但没有提及体系结构
Undefined symbols for architecture arm64:
"Huffman::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Huffman::EncodingMap const&)", referenced from:
_main in main-4994c4.o
ld: symbol(s) not found for architecture arm64
// huffman.cpp
#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
#include <memory>
namespace Huffman
{
struct TreeNode
{
private:
struct Compare
{
bool operator()(std::shared_ptr<TreeNode> left, std::shared_ptr<TreeNode> right)
{
return left->frequency > right->frequency;
}
};
public:
char symbol;
unsigned frequency;
std::shared_ptr<TreeNode> left;
std::shared_ptr<TreeNode> right;
TreeNode(char symbol, unsigned frequency)
: symbol(symbol), frequency(frequency) {}
static std::shared_ptr<TreeNode> buildTree(const std::string &text)
{
std::unordered_map<char, unsigned> frequencyTable;
for (char c : text)
{
++frequencyTable[c];
}
std::priority_queue<std::shared_ptr<TreeNode>, std::vector<std::shared_ptr<TreeNode>>, Compare> queue;
for (const auto &[symbol, frequency] : frequencyTable)
{
queue.push(std::make_shared<TreeNode>(symbol, frequency));
}
while (queue.size() > 1)
{
auto left = queue.top();
queue.pop();
auto right = queue.top();
queue.pop();
auto newNode = std::make_shared<TreeNode>('$', left->frequency + right->frequency);
newNode->left = left;
newNode->right = right;
queue.push(newNode);
}
return queue.top();
}
};
class EncodingMap
{
private:
std::unordered_map<char, std::string> map;
void build(std::shared_ptr<Huffman::TreeNode> node, std::string code)
{
if (node.get() == nullptr)
return;
if (node->left == nullptr && node->right == nullptr)
{
map[node->symbol] = code;
return;
}
build(node->left, code + "0");
build(node->right, code + "1");
}
public:
EncodingMap(const std::string &text)
{
auto root = TreeNode::buildTree(text);
build(root, "");
}
friend std::ostream &operator<<(std::ostream &os, const EncodingMap &map)
{
for (auto &&[key, value] : map.map)
{
std::cout << key << "=" << value << std::endl;
}
return os;
}
const std::string &operator[](char symbol)
{
return map[symbol];
}
};
std::pair<std::string, EncodingMap> encode(const std::string &text)
{
EncodingMap map = EncodingMap(text);
std::string encodedText;
for (char c : text)
{
encodedText += map[c];
}
return std::make_pair(encodedText, map);
}
};
// huffman.hpp
#ifndef HUFFMAN_H
#define HUFFMAN_H
#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
#include <memory>
namespace Huffman
{
struct TreeNode
{
public:
char symbol;
unsigned frequency;
std::shared_ptr<TreeNode> left;
std::shared_ptr<TreeNode> right;
TreeNode(char symbol, unsigned frequency)
: symbol(symbol), frequency(frequency) {}
static std::shared_ptr<TreeNode> buildTree(const std::string &text);
};
class EncodingMap
{
public:
EncodingMap(const std::string &text);
friend std::ostream &operator<<(std::ostream &os, const EncodingMap &map);
const std::string &operator[](char symbol);
};
std::pair<std::string, EncodingMap> encode(const std::string &text);
};
#endif
// main.cpp
#include <iostream>
#include "./huffman.hpp"
int main()
{
auto [encoded, map] = Huffman::encode("hello world");
std::cout << encoded << std::endl;
std::cout << map << std::endl;
return 0;
}
我试着用cmake构建它,但我也自己构建它进行测试,错误确实与我的构建系统无关。
g++ ./src/main.cpp ./src/huffman.cpp -std=c++17
1条答案
按热度按时间r6hnlfcb1#
这就是你应该如何将类拆分成头文件和源文件