Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 866491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:48:53+00:00 2026-05-15T09:48:53+00:00

I’m trying to create a template class, and when I define a non-member template

  • 0

I’m trying to create a template class, and when I define a non-member template function, I get the “No matching function for call to randvec()” error.

I have a template class defined as:

template <class T>
class Vector {
T x, y, z;

public:
//constructors
Vector();
Vector(const T& x, const T& y, const T& z);
Vector(const Vector& u);

//accessors
T getx() const;
T gety() const;
T getz() const;

//mutators
void setx(const T& x);
void sety(const T& y);
void setz(const T& z);

//operations
void operator-();
Vector plus(const Vector& v);
Vector minus(const Vector& v);
Vector cross(const Vector& v);
T dot(const Vector& v);
void times(const T& s);
T length() const;
//Vector<T>& randvec();

//operators
Vector& operator=(const Vector& rhs);
friend std::ostream& operator<< <T>(std::ostream&, const Vector<T>&);
};

and the function in question, which I’ve defined after all those functions above, is:

//random Vector
template <class T>
Vector<double>& randvec()
{
const int min=-10, max=10;
Vector<double>* r = new Vector<double>;
int randx, randy, randz, temp;

const int bucket_size = RAND_MAX/(max-min +1);

temp = rand();    //voodoo hackery

do randx = (rand()/bucket_size)+min;
while (randx < min || randx > max);
r->setx(randx);

do randy = (rand()/bucket_size)+min;
while (randy < min || randy > max);
r->sety(randy);

do randz = (rand()/bucket_size)+min;
while (randz < min || randz > max);
r->setz(randz);

return *r;
 }

Yet, every time I call it in my main function using a line like:

    Vector<double> a(randvec());

I get that error. However, if I remove the template and define it using ‘double’ instead of ‘T’, the call to randvec() works perfectly. Why doesn’t it recognize randvec()?

P.S. Don’t mind the bit labeled voodoo hackery – this is just a cheap hack so that I can get around another problem I encountered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:48:54+00:00Added an answer on May 15, 2026 at 9:48 am

    You defined your randvec as a function template, yet no function parameters depend on template parameter T (in fact randvec has no function parameters at all). This immediately means that it is impossible for the compiler to deduce the template argument T from the call to randvec(). You have to specify the template argument T for each call to randvec explicitly, as in

    randvec<some_type>()
    

    or, in your context

    Vector<double> a( randvec<some_type>() );
    

    This actually rises the question of why you decided to define your randvec as a function template, when nothing in randvec actually depends on the template parameter T. Why?

    P.S. Returning a dynamically allocated object by reference… It can be made to work correctly, but it is still a very questionable practice. I don’t see your entire code, but I’d guess that your

    Vector<double> a( randvec<some_type>() );
    

    leaks Vector<double> object allocated and returned by randvec.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.