我在苹果电脑上写C++代码。为什么编译时会出现此错误?:
架构日志未定义的符号:“i386::i386 string”,引用自libTest.a(Log.o)中的: Log::method(std::string) ld:未找到架构的符号clang: error: linker命令失败,退出代码为1(使用-v查看调用)
不确定我的代码是否错误,或者我必须向Xcode添加额外的标志。我当前的XCode配置是“静态库”项目的默认配置。
我的代码:
Log.h
#include <iostream>
#include <string>
using namespace std;
class Log{
public:
static void method(string arg);
private:
static string theString ;
};
Log.cpp
#include "Log.h"
#include <ostream>
void Log::method(string arg){
theString = "hola";
cout << theString << endl;
}
我从测试代码中调用'method‘,用这种方式:'Log::method("asd"):’
谢谢你的帮助。
发布于 2012-02-15 02:47:03
您必须在cpp
文件中定义静态。
Log.cpp
#include "Log.h"
#include <ostream>
string Log::theString; // <---- define static here
void Log::method(string arg){
theString = "hola";
cout << theString << endl;
}
您还应该从标头中删除using namespace std;
。在你还可以的时候养成这个习惯。这将使用std
污染全局名称空间,无论您在何处包含标头。
发布于 2012-02-15 02:47:14
您声明了static string theString;
,但尚未定义它。
包括
string Log::theString;
添加到cpp
文件中
https://stackoverflow.com/questions/9282354
复制相似问题