首先,我已经在Stackoverflow上阅读了有关此问题的相关内容,但我仍然无法解决它.我尽可能简化了我的代码.
我只有一个带.h和.cpp文件的自定义类,但是在尝试从main.cpp创建此类的实例时出错.
main.cpp中
#include "Customer.h" using namespace std; int main() { Customer a("string"); return 0; }
Customer.h
using namespace std; class Customer { public: Customer(string input); };
为customer.cpp
#include "Customer.h" using namespace std; Customer::Customer(string input) { }
我得到的错误信息如下?
gcc *.cpp -o k Undefined symbols for architecture x86_64: "std::__1::basic_string, std::__1::allocator >::__init(char const*, unsigned long)", referenced from: _main in main-40340f.o "std::__1::basic_string , std::__1::allocator >::basic_string(std::__1::basic_string , std::__1::allocator > const&)", referenced from: _main in main-40340f.o "std::__1::basic_string , std::__1::allocator >::~basic_string()", referenced from: _main in main-40340f.o "std::terminate()", referenced from: ___clang_call_terminate in main-40340f.o "___cxa_begin_catch", referenced from: ___clang_call_terminate in main-40340f.o "___gxx_personality_v0", referenced from: _main in main-40340f.o Dwarf Exception Unwind Info (__eh_frame) in main-40340f.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) clang: error: linker command failed with exit code 1 (use -v to see invocation)
我运行Mac OS X 10.9和Sublime Text 3. gcc -v给出以下内容:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.1.0 Thread model: posix
当我编写空构造函数而不是这些时,它编译没有问题.
什么可能导致这个问题?