Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于C++;样板 我在编写C++模板时遇到了问题,让我们检查代码: abc.h class ABC { public: template <class T> T getSomething(const std::string& name, ABC* owner = 0); ABC* getSomething(const std::string& name); }; abc.cpp #include "abc.h" template <class T> T ABC::getSomething(const std::string& name, ABC* owner) { return dynamic_cast<T>(owner ? owner->getSomething(name) : getSomething(name)); } ABC* ABC::getSomething(const std::string& name) { return NULL; } abc.h ABC班 { 公众: 样板 T getSomething(const std::string&name,ABC*owner=0); ABC*getSomething(const std::string和name); }; abc.cpp #包括“abc.h” 样板 T ABC::getSomething(const std::string&name,ABC*owner) { 返回动态_cast(owner?owner->getSomething(name):getSomething(name)); } ABC*ABC::getSomething(const std::string和name) { 返回NULL; }_C++_Templates - Fatal编程技术网 getSomething(name):getSomething(name)); } ABC*ABC::getSomething(const std::string和name) { 返回NULL; },c++,templates,C++,Templates" /> getSomething(name):getSomething(name)); } ABC*ABC::getSomething(const std::string和name) { 返回NULL; },c++,templates,C++,Templates" />

关于C++;样板 我在编写C++模板时遇到了问题,让我们检查代码: abc.h class ABC { public: template <class T> T getSomething(const std::string& name, ABC* owner = 0); ABC* getSomething(const std::string& name); }; abc.cpp #include "abc.h" template <class T> T ABC::getSomething(const std::string& name, ABC* owner) { return dynamic_cast<T>(owner ? owner->getSomething(name) : getSomething(name)); } ABC* ABC::getSomething(const std::string& name) { return NULL; } abc.h ABC班 { 公众: 样板 T getSomething(const std::string&name,ABC*owner=0); ABC*getSomething(const std::string和name); }; abc.cpp #包括“abc.h” 样板 T ABC::getSomething(const std::string&name,ABC*owner) { 返回动态_cast(owner?owner->getSomething(name):getSomething(name)); } ABC*ABC::getSomething(const std::string和name) { 返回NULL; }

关于C++;样板 我在编写C++模板时遇到了问题,让我们检查代码: abc.h class ABC { public: template <class T> T getSomething(const std::string& name, ABC* owner = 0); ABC* getSomething(const std::string& name); }; abc.cpp #include "abc.h" template <class T> T ABC::getSomething(const std::string& name, ABC* owner) { return dynamic_cast<T>(owner ? owner->getSomething(name) : getSomething(name)); } ABC* ABC::getSomething(const std::string& name) { return NULL; } abc.h ABC班 { 公众: 样板 T getSomething(const std::string&name,ABC*owner=0); ABC*getSomething(const std::string和name); }; abc.cpp #包括“abc.h” 样板 T ABC::getSomething(const std::string&name,ABC*owner) { 返回动态_cast(owner?owner->getSomething(name):getSomething(name)); } ABC*ABC::getSomething(const std::string和name) { 返回NULL; },c++,templates,C++,Templates,我的主要功能是: int main() { ABC abc; ABC* test = abc.getSomething<ABC*>("hello", NULL); } intmain() { ABC; ABC*test=ABC.getSomething(“hello”,NULL); } 当我把我的main放在这个abc.cpp中并编译它时,没有问题,一切正常 但是当我使用这个abc.cpp(后来我将它编译成abc.o)然后将我的主函数放在不同的文件

我的主要功能是:

int main()
{
        ABC abc;
        ABC* test = abc.getSomething<ABC*>("hello", NULL);
}
intmain()
{
ABC;
ABC*test=ABC.getSomething(“hello”,NULL);
}
当我把我的main放在这个abc.cpp中并编译它时,没有问题,一切正常

但是当我使用这个abc.cpp(后来我将它编译成abc.o)然后将我的主函数放在不同的文件(比如def.cpp)中时,问题就来了

我犯了一个很奇怪的错误,错误是:

/tmp/ccn1H4Bg.o: In function `main':
def.cpp:(.text+0x4a): undefined reference to `ABC* ABC::getSomething<ABC*>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ABC*)'
collect2: ld returned 1 exit status
/tmp/ccn1H4Bg.o:在函数'main'中:
def.cpp:(.text+0x4a):对“ABC*ABC::getSomething(std::basic_string const&,ABC*)”的未定义引用
collect2:ld返回了1个退出状态
你们能帮帮我我做错了什么吗


谢谢大家

这是因为编译器不支持单独编译的模板。将abc.cpp中的模板代码移动到abc.h可以解决此问题。像这样:

//abc.h
class ABC
{
public:
        template <class T>
        T getSomething(const std::string& name, ABC* owner = 0);
        ABC* getSomething(const std::string& name);
};

template <class T>
T ABC::getSomething(const std::string& name, ABC* owner)
{
        return dynamic_cast<T>(owner ? owner->getSomething(name) : getSomething(name));
}

//abc.cpp
ABC* ABC::getSomething(const std::string& name)
{
        return NULL;
}

//def.cpp
int main()
{
        ABC abc;
        ABC* test = abc.getSomething<ABC*>("hello", NULL);
}
//abc.h
ABC班
{
公众:
样板
T getSomething(const std::string&name,ABC*owner=0);
ABC*getSomething(const std::string和name);
};
样板
T ABC::getSomething(const std::string&name,ABC*owner)
{
返回动态_cast(owner?owner->getSomething(name):getSomething(name));
}
//abc.cpp
ABC*ABC::getSomething(const std::string和name)
{
返回NULL;
}
//def.cpp
int main()
{
ABC;
ABC*test=ABC.getSomething(“hello”,NULL);
}