我有一个有很多函数模板的项目,我在Visual 2017中写了这些模板,而且它总是工作得很好。现在我必须在VS2019中构建它,因为我需要在另一个用VS2019编写的项目中包含库,而这个东西不会构建。
有一个函数模板似乎存在问题,尽管它实际上并不抱怨该函数本身。当我在代码中调用它时,编译器只会说“标识符未找到”。但是,在名称空间中,就连InteliSense也能看到它,并毫无怨言地链接到它。只是编译器不会。
下面是有问题的代码:
// declaration
namespace Oparse
{
// lots of other functions, many of them templates
template <typename T> OpModel<T> *_ModelPtr(T *receiver) { return new OpModel<T>(receiver); };
}
// Invocation
namespace Oparse
{
template <class T, class U>
class OpModelFactory
: public OpNestable
{
public:
OpModelFactory<T, U>(vector<U*> &receiver) : OpNestable(OP_MODELFACTORY), receiver(receiver) {};
// other stuff
void Serialize(string key, stringstream &stream, unsigned int indents)
{
for (unsigned int i = 0; i < receiver.size(); ++i)
{
// check if the instances are really of the type of this OpModel, otherwise there may be duplicates between polymorphic factories populating the same receiver.
T *currentModel = dynamic_cast<T*>(receiver[i]);
if (currentModel != NULL)
{
OpModel<T> *parser = _ModelPtr<T>(currentModel); // <-- identifier not found
parser->Serialize(key, stream, indents);
delete parser;
}
}
};
private:
vector<U*> &receiver;
}
}
如果我评论这个调用,项目就会构建,尽管在这个地方声明了更多的函数模板。我不知道该怎么做才能让链接器找到它。是否有任何Visual向导可以给我一个提示?我必须坦白地承认,我已经多年没有使用IDE了,这是我第一次在Visual 2019年.
这是错误的完整输出。还有第二条信息,但我发现它毫无帮助:
1>D:\Orbiter_installs\Orbiter2016\Orbitersdk\Oparse\include\OpModel.h(138,27): error C3861: '_ModelPtr': identifier not found
1>D:\Orbiter_installs\Orbiter2016\Orbitersdk\Oparse\include\OpModel.h(152): message : see reference to class template instantiation 'Oparse::OpModelFactory<T,U>' being compiled
没有,没有更多的信息。我也看到过类似的信息,通常是“使用. $further信息”,但这是我所得到的全部信息。
发布于 2021-07-26 19:51:43
存在、循环依赖问题。
在Oparse.h
中,您首先包括在实现Serialize
时需要_ModelPtr
的OpModel.h
,但是_ModelPtr
只在后面的标头中定义。
您需要转发声明模板方法。
在OpModel.h
中,将以下内容写成:
namespace Oparse
{
template<typename T> class OpModel;
template <typename T> OpModel<T>* _ModelPtr(T* receiver);
// Remaining of OpModel.h...
typedef map<string, pair<Oparse::OpValue*, vector<Oparse::OpValidator*>>> OpModelDef;
typedef vector<pair<Oparse::OpValue*, vector<Oparse::OpValidator*>>> OpValues;
...
https://stackoverflow.com/questions/68534725
复制相似问题