C++尝试将命名空间放在单独文件中时出现未定义符号。

huangapple go评论89阅读模式
英文:

C++ Undefined symbols when I try to put namespace in a separate file

问题

I have a problem with putting namespaces in separate files. Everything works for me when I write everything in one file, but when I try to create cpp+hpp files, that's where it all starts. Maybe I'm doing something wrong in the header file or linking the files incorrectly? I really don't know. I will be very grateful for your help.

Here is my error. On 86x_64 it also throws it out, but without mentioning the architecture:

  1. Undefined symbols for architecture arm64:
  2. "Huffman::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Huffman::EncodingMap const&)", referenced from:
  3. _main in main-4994c4.o
  4. ld: symbol(s) not found for architecture arm64
  1. // huffman.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include <queue>
  5. #include <unordered_map>
  6. #include <memory>
  7. namespace Huffman
  8. {
  9. // ... (code omitted for brevity)
  10. };
  1. // huffman.hpp
  2. #ifndef HUFFMAN_H
  3. #define HUFFMAN_H
  4. #include <iostream>
  5. #include <string>
  6. #include <queue>
  7. #include <unordered_map>
  8. #include <memory>
  9. namespace Huffman
  10. {
  11. // ... (code omitted for brevity)
  12. };
  13. #endif
  1. // main.cpp
  2. #include <iostream>
  3. #include "./huffman.hpp"
  4. int main()
  5. {
  6. auto [encoded, map] = Huffman::encode("hello world");
  7. std::cout << encoded << std::endl;
  8. std::cout << map << std::endl;
  9. return 0;
  10. }

I tried to build it with cmake, but I also built it myself for testing, and the error is really independent of my build system:

  1. g++ ./src/main.cpp ./src/huffman.cpp -std=c++17

Is there anything specific you'd like to know or discuss regarding this code?

英文:

I have a problem with putting namespaces in separate files. Everything works for me when I write everything in one file, but when I try to create cpp+hpp files, that's where it all starts. Maybe I'm doing something wrong in the headerfile or linking the files incorrectly? I really don't know. I will be very grateful for your help.

Here is my error. On 86x_64 it also throws it out, but without mentioning the architecture

  1. Undefined symbols for architecture arm64:
  2. &quot;Huffman::operator&lt;&lt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, Huffman::EncodingMap const&amp;)&quot;, referenced from:
  3. _main in main-4994c4.o
  4. ld: symbol(s) not found for architecture arm64
  1. // huffman.cpp
  2. #include &lt;iostream&gt;
  3. #include &lt;string&gt;
  4. #include &lt;queue&gt;
  5. #include &lt;unordered_map&gt;
  6. #include &lt;memory&gt;
  7. namespace Huffman
  8. {
  9. struct TreeNode
  10. {
  11. private:
  12. struct Compare
  13. {
  14. bool operator()(std::shared_ptr&lt;TreeNode&gt; left, std::shared_ptr&lt;TreeNode&gt; right)
  15. {
  16. return left-&gt;frequency &gt; right-&gt;frequency;
  17. }
  18. };
  19. public:
  20. char symbol;
  21. unsigned frequency;
  22. std::shared_ptr&lt;TreeNode&gt; left;
  23. std::shared_ptr&lt;TreeNode&gt; right;
  24. TreeNode(char symbol, unsigned frequency)
  25. : symbol(symbol), frequency(frequency) {}
  26. static std::shared_ptr&lt;TreeNode&gt; buildTree(const std::string &amp;text)
  27. {
  28. std::unordered_map&lt;char, unsigned&gt; frequencyTable;
  29. for (char c : text)
  30. {
  31. ++frequencyTable[c];
  32. }
  33. std::priority_queue&lt;std::shared_ptr&lt;TreeNode&gt;, std::vector&lt;std::shared_ptr&lt;TreeNode&gt;&gt;, Compare&gt; queue;
  34. for (const auto &amp;[symbol, frequency] : frequencyTable)
  35. {
  36. queue.push(std::make_shared&lt;TreeNode&gt;(symbol, frequency));
  37. }
  38. while (queue.size() &gt; 1)
  39. {
  40. auto left = queue.top();
  41. queue.pop();
  42. auto right = queue.top();
  43. queue.pop();
  44. auto newNode = std::make_shared&lt;TreeNode&gt;(&#39;$&#39;, left-&gt;frequency + right-&gt;frequency);
  45. newNode-&gt;left = left;
  46. newNode-&gt;right = right;
  47. queue.push(newNode);
  48. }
  49. return queue.top();
  50. }
  51. };
  52. class EncodingMap
  53. {
  54. private:
  55. std::unordered_map&lt;char, std::string&gt; map;
  56. void build(std::shared_ptr&lt;Huffman::TreeNode&gt; node, std::string code)
  57. {
  58. if (node.get() == nullptr)
  59. return;
  60. if (node-&gt;left == nullptr &amp;&amp; node-&gt;right == nullptr)
  61. {
  62. map[node-&gt;symbol] = code;
  63. return;
  64. }
  65. build(node-&gt;left, code + &quot;0&quot;);
  66. build(node-&gt;right, code + &quot;1&quot;);
  67. }
  68. public:
  69. EncodingMap(const std::string &amp;text)
  70. {
  71. auto root = TreeNode::buildTree(text);
  72. build(root, &quot;&quot;);
  73. }
  74. friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const EncodingMap &amp;map)
  75. {
  76. for (auto &amp;&amp;[key, value] : map.map)
  77. {
  78. std::cout &lt;&lt; key &lt;&lt; &quot;=&quot; &lt;&lt; value &lt;&lt; std::endl;
  79. }
  80. return os;
  81. }
  82. const std::string &amp;operator[](char symbol)
  83. {
  84. return map[symbol];
  85. }
  86. };
  87. std::pair&lt;std::string, EncodingMap&gt; encode(const std::string &amp;text)
  88. {
  89. EncodingMap map = EncodingMap(text);
  90. std::string encodedText;
  91. for (char c : text)
  92. {
  93. encodedText += map[c];
  94. }
  95. return std::make_pair(encodedText, map);
  96. }
  97. };
  1. // huffman.hpp
  2. #ifndef HUFFMAN_H
  3. #define HUFFMAN_H
  4. #include &lt;iostream&gt;
  5. #include &lt;string&gt;
  6. #include &lt;queue&gt;
  7. #include &lt;unordered_map&gt;
  8. #include &lt;memory&gt;
  9. namespace Huffman
  10. {
  11. struct TreeNode
  12. {
  13. public:
  14. char symbol;
  15. unsigned frequency;
  16. std::shared_ptr&lt;TreeNode&gt; left;
  17. std::shared_ptr&lt;TreeNode&gt; right;
  18. TreeNode(char symbol, unsigned frequency)
  19. : symbol(symbol), frequency(frequency) {}
  20. static std::shared_ptr&lt;TreeNode&gt; buildTree(const std::string &amp;text);
  21. };
  22. class EncodingMap
  23. {
  24. public:
  25. EncodingMap(const std::string &amp;text);
  26. friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const EncodingMap &amp;map);
  27. const std::string &amp;operator[](char symbol);
  28. };
  29. std::pair&lt;std::string, EncodingMap&gt; encode(const std::string &amp;text);
  30. };
  31. #endif
  1. // main.cpp
  2. #include &lt;iostream&gt;
  3. #include &quot;./huffman.hpp&quot;
  4. int main()
  5. {
  6. auto [encoded, map] = Huffman::encode(&quot;hello world&quot;);
  7. std::cout &lt;&lt; encoded &lt;&lt; std::endl;
  8. std::cout &lt;&lt; map &lt;&lt; std::endl;
  9. return 0;
  10. }

I tried to build it with cmake, but I also built it myself for testing, and the error is really independent of my build system.

  1. g++ ./src/main.cpp ./src/huffman.cpp -std=c++17

答案1

得分: 2

这就是你应该将类拆分为头文件和源文件的方式。

  1. // 头文件,x.h
  2. #ifndef X_H
  3. #define X_H
  4. class X
  5. {
  6. void f();
  7. };
  8. #endif
  9. // 源文件,x.cpp
  10. #include &quot;x.h&quot;
  11. void X::f()
  12. {
  13. // 此处编写代码
  14. }
英文:

This is how you are supposed to split classes into header file and source files

  1. // header file, x.h
  2. #ifndef X_H
  3. #define X_H
  4. class X
  5. {
  6. void f();
  7. };
  8. #endif
  9. // source file, x.cpp
  10. #include &quot;x.h&quot;
  11. void X::f()
  12. {
  13. // code here
  14. }

huangapple
  • 本文由 发表于 2023年5月17日 17:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定