更新 2:找到导致此问题的前向声明错误
更新:使用 Visual Studio 2010 库存编译器编译,启用了 c++0x。
我有一个具有奇怪行为的成员函数的类。
当我在 cpp 文件中定义方法时,链接器会因“未解析的外部符号”错误而崩溃。当我将定义移动到标题时,它编译得很好。
1)这不是模板方法 2)cpp 文件肯定正在编译 3)我很难过
有什么想法吗?
头文件
// THIS IS THE ERROR HERE: I forward declare TPA as a class, when it is actually a struct
class TollingPathAttributes; // forward declare the TollingPathAttributes struct as a class (doh!)
class TollingPathAttributesOutput
{
public:
TollingPathAttributesOutput(HighwayCommand* command);
~TollingPathAttributesOutput();
void foo();
void AddPathAttributes(boost::shared_ptr<TollingPathAttributes> attributes);
protected:
string m_outputFilename;
vector<shared_ptr<TollingPathAttributes>> m_data;
queuing_mutex m_dataMutex;
};
.cpp 文件
void TollingPathAttributesOutput::foo() {}
void TollingPathAttributesOutput::AddPathAttributes(boost::shared_ptr<TollingPathAttributes> attributes)
{
queuing_mutex::scoped_lock lock(m_dataMutex);
m_data.push_back(attributes);
}
调用调用
m_output->foo(); // compiles and links no problem
boost::shared_ptr<TollingPathAttributes> attr =
boost::make_shared<TollingPathAttributes>(origin, dest, UNTOLLED_OPTION, vector<int>(), 0.0, 0.0);
m_output->AddPathAttributes(attr); // compiles fine, but only links correctly if I move the definition to the header
链接错误
1>OtCustomZenith_logic.lib(TollingPathAttributesRecorder.obj) : error LNK2019: unresolved external symbol "public: void __thiscall TollingPathAttributesOutput::AddPathAttributes(class boost::shared_ptr<class TollingPathAttributes>)" (?AddPathAttributes@TollingPathAttributesOutput@@QAEXV?$shared_ptr@VTollingPathAttributes@@@boost@@@Z) referenced in function "public: virtual void __thiscall TollingPathAttributesRecorder::Record(class TolledPath &,class boost::shared_ptr<class Path>,int)" (?Record@TollingPathAttributesRecorder@@UAEXAAVTolledPath@@V?$shared_ptr@VPath@@@boost@@H@Z)
1>..\..\..\OT\OtCustomZenith_test.exe : fatal error LNK1120: 1 unresolved externals