代码之家  ›  专栏  ›  技术社区  ›  Ramazan Chasygov

使用Atom设置C++项目

  •  0
  • Ramazan Chasygov  · 技术社区  · 7 年前

    我正在尝试使用Atom设置cpp项目。我的项目(下图)。我的代码(下面的代码片段)。我得到错误 undefined reference to hashCustom::Hash(std::__cxx11::basic_string, std::allocator >) collect2: error: ld returned 1 exit status . 我从教程中做了所有的事情,有一点不同,那个教程使用Visual Studio,但它不支持Linux。如何消除错误?

    hash.h

    #include <iostream>
    #include <string>
    using namespace std;
    
    #ifndef HASH_H
    #define HASH_H
    
    class hashCustom {
      public:
      int Hash(string key);
    };
    
    #endif
    

    hash.cpp

    #include <iostream>
    #include <string>
    #include "../Header Files/hash.h"
    using namespace std;
    
    int hashCustom::Hash(string key) {
      int index;
      index = key.length();
      return index;
    }
    

    main.cpp

    #include <iostream>
    #include <string>
    #include "../Header Files/hash.h"
    using namespace std;
    
    int main() {
    
      hashCustom hashObj;
      cout << hashObj.Hash("Number") << endl;
      return 0;
    }
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   Bob__    7 年前

    从您发布的错误消息来看,链接器似乎无法解析该符号。您应该编译这两个 main.cpp hash.cpp 使用类似以下命令(使用gcc):

    $ g++ -o test main.cpp hash.cpp
    

    工作示例: https://wandbox.org/permlink/PIK8kF4eINcLjEYc

    否则,会出现相同的错误: https://wandbox.org/permlink/K8nmaUmJZhlS8Wml