我开始使用Visual Studio 2013 Professional在C ++中编写库,以使某些代码可重用。 所以我创建了两个新项目(help和helpTest):帮助开发库,helpTest在帮助中测试我的实现。
到目前为止,我尝试将其用作标题:
//File: strg.h
#pragma once
#include "stdafx.h"
#include<vector>
#include<sstream>
namespace help
{
class strg
{
public:
strg();
~strg();
static std::vector<std::string> explode(const std::string& s, char delim); // PHP's explode function for C++
};
}
这是头文件的实现:
// File: strg.cpp
#include "stdafx.h"
#include "strg.h"
help::strg::strg()
{
}
help::strg::~strg()
{
}
std::vector<std::string> help::strg::explode(const std::string& s, char delim){
std::vector<std::string> result; // This will be our result
std::istringstream iss(s); // Our string stream; needs #include<sstream>
for (std::string token; std::getline(iss, token, delim);){ // Loop reads line till the delimiter and put it in token; needs #include<sstream>
result.push_back(std::move(token));
}
return result;
}//*/
我试图在这个主要功能中使用它:
// helpTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" // has #include "strg.h" in it!
#include <iostream> // for cin and cout
int _tmain(int argc, _TCHAR* argv[])
{
std::string string = "Das, ist, mein, String";
std::vector<std::string> stringVector = help::strg::explode(string, ',');
for (int i = 0; i < stringVector.size(); i++)
{
std::cout << stringVector[i] << std::endl;
}//*/
// Return 0 since everything is good
return 0;
}
Visual Studio适用于所有代码(正确显示所有名称,类等)。就在我执行构建和链接过程时,我得到:
1>helpTest.obj : error LNK2019: unresolved external symbol "public: static class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __cdecl help::strg::explode(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,char)" (?explode@strg@help@@SA?AV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@D@Z) referenced in function _wmain
我做错了什么? 如果您需要更多信息,请问我!我可能无意中遗漏了一些东西!我尽我所能去帮忙!花了很多我们试图解决这个问题......