我想将.h和.cpp分开用于模板类。 这是我在做的事情:
我直接写了.h和.cpp就像没有模板一样。 因此它会创建一个例外,例如Link 2019 Template异常 有一些解决方案来处理这个如何定义模板类头并在另一个cpp中实现它 。 我选择解决方案3。 根据解决方案,我在#endif里面添加了包含* .cpp的头文件。(Still * .cpp包括* .h) (下面代码代表这一步)它给出了根据研究,摆脱这个错误的方法是(循环依赖)从* .cpp删除#include * .h但是这次模板已经定义错误。
无法识别的模板声明/定义错误
发生。 我的问题是,如果我将* .cpp包含在* .h文件中。 我们如何按预期建造项目? 或者这个解决方案是绝对的?
// TestTemp.h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp(); void SetValue(T obj_i); T Getalue(); private: T m_Obj; }; #include "TestTemp.cpp" #endif // TestTemp.cpp #include "TestTemp.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue(T obj_i) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj; } #include "TestTemp.h" int main() { TestTemp<int> a; a.Getalue(); return 0; }I want to separate .h and .cpp for template class. Here is the what I was doing:
I wrote directly .h and .cpp like without template. So it creates an exception like Link 2019 Template exception There are some solution to handle this How to define template class header and implement it in another cpp. I choose solution 3. According to the solution I added include *.cpp just before #endif inside header.(Still *.cpp includes *.h)(Below code represents this step) It givesAccording to research the way of get rid of this error is(circular dependency) remove #include *.h from *.cpp but this timetemplate has already been defined error.
unrecognizable template declaration/definition error
Occured. My question is if I include *.cpp to *.h file. How can we build project as expected? Or this solution is obsolute?
// TestTemp.h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp(); void SetValue(T obj_i); T Getalue(); private: T m_Obj; }; #include "TestTemp.cpp" #endif // TestTemp.cpp #include "TestTemp.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue(T obj_i) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj; } #include "TestTemp.h" int main() { TestTemp<int> a; a.Getalue(); return 0; }最满意答案
与普通类的成员函数不同,模板类的成员函数不能单独编译并链接到可执行文件中。 模板的成员必须在编译器可用的位置可见。 这就是所有荒谬的内容,包括那篇可怕文章中的内容。
最简单的方法是将定义直接放入模板定义中:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f() { /* whatever */ } }; #endif这样做的缺点是较大的类变得不可读(参见Java)。 因此,下一步是将定义移到模板外部,但将它们保留在标题中:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; template <class Ty> void test<Ty>::f() { /* whatever */ } #endif许多人认为这仍然太杂乱,并希望将定义放在一个单独的文件中。 这也没关系,但是每当使用原始标头时,您必须确保包含该单独文件:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; #include "test.imp" #endif这是文件“test.imp”:
#ifndef TEST_IMP #define TEST_IMP template <class Ty> void test<Ty>::f() { /* whatever */ } #endif请注意,“test.imp”实际上是一个头文件,因此它通过#include "test.imp"的#include "test.imp"指令进入test.h 。 它不能单独编译,因此不应以.cpp扩展名命名,这最多会产生误导。
Unlike member functions of ordinary classes, member functions of template classes cannot be compiled separately and linked into the executable. The members of a template must be visible to the compiler at the point where they're used. That's what all that nonsensical include stuff in that horrible article is about.
The simplest way to do this is to put the definitions directly into the template definition:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f() { /* whatever */ } }; #endifThis has the drawback that larger classes become unreadable (cf. Java). So the next step is to move the definitions outside the template, but keep them in the header:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; template <class Ty> void test<Ty>::f() { /* whatever */ } #endifMany people feel that that's still too cluttered, and want to put the definitions into a separate file. That's okay, too, but you have to make sure that that separate file gets included whenever the original header is used:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; #include "test.imp" #endifThis is the file "test.imp":
#ifndef TEST_IMP #define TEST_IMP template <class Ty> void test<Ty>::f() { /* whatever */ } #endifNote that "test.imp" is really a header file, so it gets into your code through the #include "test.imp" directive in test.h. It cannot be compiled separately, so should not be named with a .cpp extension, which would, at best, be misleading.
C ++模板头cpp分离,包含* .cpp到* .h的解决方案不再起作用了(C++ template header cpp separation, solution including *.cpp into *.h is not working anymore)我想将.h和.cpp分开用于模板类。 这是我在做的事情:
我直接写了.h和.cpp就像没有模板一样。 因此它会创建一个例外,例如Link 2019 Template异常 有一些解决方案来处理这个如何定义模板类头并在另一个cpp中实现它 。 我选择解决方案3。 根据解决方案,我在#endif里面添加了包含* .cpp的头文件。(Still * .cpp包括* .h) (下面代码代表这一步)它给出了根据研究,摆脱这个错误的方法是(循环依赖)从* .cpp删除#include * .h但是这次模板已经定义错误。
无法识别的模板声明/定义错误
发生。 我的问题是,如果我将* .cpp包含在* .h文件中。 我们如何按预期建造项目? 或者这个解决方案是绝对的?
// TestTemp.h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp(); void SetValue(T obj_i); T Getalue(); private: T m_Obj; }; #include "TestTemp.cpp" #endif // TestTemp.cpp #include "TestTemp.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue(T obj_i) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj; } #include "TestTemp.h" int main() { TestTemp<int> a; a.Getalue(); return 0; }I want to separate .h and .cpp for template class. Here is the what I was doing:
I wrote directly .h and .cpp like without template. So it creates an exception like Link 2019 Template exception There are some solution to handle this How to define template class header and implement it in another cpp. I choose solution 3. According to the solution I added include *.cpp just before #endif inside header.(Still *.cpp includes *.h)(Below code represents this step) It givesAccording to research the way of get rid of this error is(circular dependency) remove #include *.h from *.cpp but this timetemplate has already been defined error.
unrecognizable template declaration/definition error
Occured. My question is if I include *.cpp to *.h file. How can we build project as expected? Or this solution is obsolute?
// TestTemp.h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp(); void SetValue(T obj_i); T Getalue(); private: T m_Obj; }; #include "TestTemp.cpp" #endif // TestTemp.cpp #include "TestTemp.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue(T obj_i) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj; } #include "TestTemp.h" int main() { TestTemp<int> a; a.Getalue(); return 0; }最满意答案
与普通类的成员函数不同,模板类的成员函数不能单独编译并链接到可执行文件中。 模板的成员必须在编译器可用的位置可见。 这就是所有荒谬的内容,包括那篇可怕文章中的内容。
最简单的方法是将定义直接放入模板定义中:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f() { /* whatever */ } }; #endif这样做的缺点是较大的类变得不可读(参见Java)。 因此,下一步是将定义移到模板外部,但将它们保留在标题中:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; template <class Ty> void test<Ty>::f() { /* whatever */ } #endif许多人认为这仍然太杂乱,并希望将定义放在一个单独的文件中。 这也没关系,但是每当使用原始标头时,您必须确保包含该单独文件:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; #include "test.imp" #endif这是文件“test.imp”:
#ifndef TEST_IMP #define TEST_IMP template <class Ty> void test<Ty>::f() { /* whatever */ } #endif请注意,“test.imp”实际上是一个头文件,因此它通过#include "test.imp"的#include "test.imp"指令进入test.h 。 它不能单独编译,因此不应以.cpp扩展名命名,这最多会产生误导。
Unlike member functions of ordinary classes, member functions of template classes cannot be compiled separately and linked into the executable. The members of a template must be visible to the compiler at the point where they're used. That's what all that nonsensical include stuff in that horrible article is about.
The simplest way to do this is to put the definitions directly into the template definition:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f() { /* whatever */ } }; #endifThis has the drawback that larger classes become unreadable (cf. Java). So the next step is to move the definitions outside the template, but keep them in the header:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; template <class Ty> void test<Ty>::f() { /* whatever */ } #endifMany people feel that that's still too cluttered, and want to put the definitions into a separate file. That's okay, too, but you have to make sure that that separate file gets included whenever the original header is used:
#ifndef TEST_H #define TEST_H template <class Ty> class test { public: void f(); }; #include "test.imp" #endifThis is the file "test.imp":
#ifndef TEST_IMP #define TEST_IMP template <class Ty> void test<Ty>::f() { /* whatever */ } #endifNote that "test.imp" is really a header file, so it gets into your code through the #include "test.imp" directive in test.h. It cannot be compiled separately, so should not be named with a .cpp extension, which would, at best, be misleading.
发布评论