template-classes

Ambiguous multiple inheritance of template classes

若如初见. 提交于 2021-02-08 12:32:14
问题 I've got a real situation which can be summarized in the following example: template< typename ListenerType > struct Notifier { void add_listener( ListenerType& ){} }; struct TimeListener{ }; struct SpaceListener{ }; struct A : public Notifier< TimeListener > , public Notifier< SpaceListener > { }; struct B : TimeListener{ }; int main() { A a; B b; a.add_listener( b ); // why is ambiguous? return 0; } Why is not obvious to the compiler that B is a TimeListener , and therefore the only

Compiler error `assigning incompatible type within if statement` [duplicate]

空扰寡人 提交于 2021-01-03 06:30:42
问题 This question already has answers here : using std::is_same, why my function still can't work for 2 types (4 answers) Closed 3 months ago . The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here Sample code /** * @brief setValue * set value to property * @param val * value to set to

Compiler error `assigning incompatible type within if statement` [duplicate]

牧云@^-^@ 提交于 2021-01-03 06:28:45
问题 This question already has answers here : using std::is_same, why my function still can't work for 2 types (4 answers) Closed 3 months ago . The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here Sample code /** * @brief setValue * set value to property * @param val * value to set to

Compiler error `assigning incompatible type within if statement` [duplicate]

江枫思渺然 提交于 2021-01-03 06:28:15
问题 This question already has answers here : using std::is_same, why my function still can't work for 2 types (4 answers) Closed 3 months ago . The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here Sample code /** * @brief setValue * set value to property * @param val * value to set to

function as template parameter : if(T receive 2 param)T(a,b); else T(a);

对着背影说爱祢 提交于 2019-12-30 10:48:12
问题 How to make template class Collection<K,T> receive a function T - that can either has signature T(K) or T(K,int) - as template argument, then conditionally compile base on the signature of the function? Here is the existing code that can receive 1 signature : Collection<K,HashFunction(K)> . template<typename AA> using HashFunction= HashStruct& (*)(AA ); /** This class is currently used in so many places in codebase. */ template<class K,HashFunction<K> T> class Collection{ void testCase(){ K k

How to have a const variable in a for loop for the generation of template classes?

萝らか妹 提交于 2019-12-21 03:32:44
问题 I have a code like template <size_t N> class A { template <size_t N> someFunctions() {}; }; Now I want to create instances of the class and call the functions in it in a for loop for a set of many values like // in main() int main() { for (int i = 1; i <= 100; i++) { const int N = i; // dont know how to do this A<N> a; a.functionCalls(); } } How to do this? Hoping for a method to do this. 回答1: This would require something called a template for which is the expected form expansion statements

non template function in a template class [closed]

不想你离开。 提交于 2019-12-14 03:28:21
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I was wondering if there is a way to place a non template function within a template class. Simply put I don't want to compiler to repeat the function for each type since the function is only manipulating pointers hence there is no type. Is this possible? so if you have code like this template

c++11 inheriting template constructors

强颜欢笑 提交于 2019-12-10 03:14:25
问题 I find the syntax of the constructor inheritance slightly odd. The example below works well, but I do not understand why I need to specify using sysTrajectory::sysTrajectory and not using sysTrajectory<Real>::sysTrajectory<Real> when inheriting from a class template? The latter gives the following error: expected ‘;’ before ‘<’ token using sysTrajectory<Real>::sysTrajectory<Real>; . class sysRealTrajectory: public sysTrajectory<Real> { public: /** * Default constructor */ inline

Dynamic Allocation in Template Class Constructor

三世轮回 提交于 2019-12-08 04:32:28
问题 I am working on a stack class and have two constructors. One of interest is this one. template <typename T> stack<T>::stack( const int n) { capacity = n ; size = 0 ; arr = new T [capacity] ; } I am calling it inside main like this. stack<int> s1(3) ; The program compiles fine but i get this run time error. 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::~stack<int>(void)" (??1?$stack@H@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019:

Template class type-specific functions

假如想象 提交于 2019-12-07 16:44:27
问题 Ok, so i have this template class, which is kinda like one-way list. template <typename T> List and it have this inside function print public: void Print(); which, as you can guess, prints the list contents from begining to end; However, as template can take classes as T, one can imagine, that i would need different implementations of Print() for that very cases. For example, I have another class Point class Point{ private: int x, y; public: int getX(); int getY(); } so i want Print