使用 Atom 设置 C++ 项目
Set Up C++ project with Atom
我正在尝试使用 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;
}
从您发布的错误消息来看,链接器似乎无法解析该符号。您应该将 main.cpp
和 hash.cpp
一起编译,使用类似(使用 gcc)的命令:
$ g++ -o test main.cpp hash.cpp
工作示例:https://wandbox.org/permlink/PIK8kF4eINcLjEYc
否则,你会得到同样的错误:https://wandbox.org/permlink/K8nmaUmJZhlS8Wml
我正在尝试使用 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;
}
从您发布的错误消息来看,链接器似乎无法解析该符号。您应该将 main.cpp
和 hash.cpp
一起编译,使用类似(使用 gcc)的命令:
$ g++ -o test main.cpp hash.cpp
工作示例:https://wandbox.org/permlink/PIK8kF4eINcLjEYc
否则,你会得到同样的错误:https://wandbox.org/permlink/K8nmaUmJZhlS8Wml