0

I want to pass a vector of type that is only known during compile time to a function which will pass it to other functions (to push elements depending on the type of vector).

How should i do it to ensure that the compiler does not throw the error:

test.cpp: In instantiation of 'void funcPushInt(std::vector) [with C = classB]': test.cpp:28:20: required from 'void checkType(std::vector) [with A = classB]' test.cpp:53:16: required from here test.cpp:38:3: error: no matching function for call to 'std::vector::push_back(int&)' vec.push_back(i); ^

#include <typeinfo>
#include <vector>
using namespace std;

class classB
{
    private:
        int i;
    public:
        B()
        {
            i = 0;
        }
}

template <class A>
void checkType(vector<A>);
template <class C>
void funcPushInt(vector<C>);
template <class B>
void funcPushClassB(vector<B>);

template <class A>
void checkType(vector<A> vec)
{
  // check type of vec
  if(typeid(vec) == typeid(vector<int>)) 
    funcPushInt(vec);
  else if(typeid(vec) == typeid(vector<classB>))
    funcPushClassB(vec);
}

template <class C>
void funcPushInt(vector<C> vec)
{
  // push int
  int i = 1;
  vec.push_back(i);
}

template <class B>
void funcPushClassB(vector<B> vec)
{
  // error as it can't push float to vector<int>
  classB objB;
  vec.push_back(objB);
}

int main()
{
    // empty vec of classB type
  vector<classB> vec;
  checkType(vec);
}
5
  • 3
    I'm more concerned about all the template functions with no declared return types and by-value parameters upon which local changes mean nothing to the caller upon return Commented Feb 10, 2015 at 5:51
  • 1
    Classic XY problem. By the way, use value_type. Commented Feb 10, 2015 at 5:53
  • What are you really trying to do? Commented Feb 10, 2015 at 5:55
  • Basically: 1) check type of vector 2) depending on type, create object of that type and push it in Commented Feb 10, 2015 at 6:07
  • Create object how? Are you assuming it is default constructible? Commented Feb 10, 2015 at 13:14

1 Answer 1

2

Your functions don't have a return type. Assuming that is an error of omission, you might as well have two functions.

template <class A>
void checkType(vector<A> vec)
{
    funcPushFloat(vec);
}

void checkType(vector<int> vec)
{
    funcPushInt(vec);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks or the reply, but I want a single checkType function instead of using multiple functions. And yes, i left out the return types
Is there a technical reason why you want a single function?
Not really.. Just for convenience's sake and not having to repeat a bunch of code. I guess i'll have to do it that way then. Thanks R Sahu!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.