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 6352585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:14:04+00:00 2026-05-24T22:14:04+00:00

#include <iostream> #include <vector> using namespace std; template <typename nameOfTheVariableTypeA, typename nameOfTheVariableTypeB> nameOfTheVariableTypeB functionX

  • 0
#include <iostream>
#include <vector>
using namespace std;

template <typename nameOfTheVariableTypeA, 
      typename nameOfTheVariableTypeB> nameOfTheVariableTypeB functionX 
                                               (nameOfTheVariableTypeA argA,
                                               (nameOfTheVariableTypeB argB)
{
    nameOfTheVariableTypeA tempArgA;
    nameOfTheVariableTypeB tempArgB;

    argA.push_back(22);

    tempArgA = argA;
    cout << "\ntempArgA: " << tempArgA[0] << "\n";

    tempArgB = argB;
    cout << "\ntempArgB: " << tempArgB << "\n";

    return tempArgB;
}

int main ()
{
    functionX (12, 12.4567);

    vector <int> f;
    functionX (f, 12.4567);

    return 0;
}

From a template book:
An attempt to instantiate a template for a type that doesn't support all the operations used within it will result in a compile-time error.

The error I received for the above code is:

  1. error: request for member ‘push_back’ in ‘argA’, which is of non-class type ‘int’

  2. error: subscripted value is neither array nor pointer

What’s the point that I am missing?

  • 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-24T22:14:06+00:00Added an answer on May 24, 2026 at 10:14 pm

    First off, there’s a superfluous parenthesis:

    template <typename nameOfTheVariableTypeA,  
          typename nameOfTheVariableTypeB> nameOfTheVariableTypeB functionX  
                                                   (nameOfTheVariableTypeA argA, 
                                                  /*(*/nameOfTheVariableTypeB argB) 
    // Note: extra parenthesis not needed here -----^ 
    {
    

    Assuming that’s just a typo, let’s take a look the first call to functionX():

    functionX(12, 12.4567);  
    

    Now, template functions require that all template parameters be specified before you can call them. But in certain cases the compiler can deduce the required type for nameOfTheVariableTypeA and nameOfTheVariableTypeB for the function call to work.

    In this case, 12 is an integer literal, so it has type int. 12.4567 is a floating-point literal, so it has type double. Thus, within functionX(), nameOfTheVariableTypeA is of type int and nameOfTheVariableTypeB is of type double.

    Now that all the template parameters have been specified (in this case, deduced), the compiler can instantiate the template function. That is, the compiler creates a function that looks like this:

    // Hypothetical function generated by the compiler
    double functionX_int_double(int argA, double argB)  
    {  
        int tempArgA;  
        double tempArgB;  
        // ...
    

    It’s as though the compiler simply substituted nameOfTheVariableTypeA and nameOfTheVariableTypeB with the deduced types. Clearly, the argument argA and variable tempArgA has type int. You get the first error because int doesn’t have a class member function called push_back(). It’s the same reason why this won’t work:

    int i = 20;
    i.push_back(22);
    

    You also get the second error because the subscript operator [] is not defined for ints. Again, it’s the same reason why this won’t work:

    int j = 21;
    cout << j[0];
    

    Note that this kind of information can be gleaned from the compiler errors themselves. Make sure you read them!

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

Sidebar

Related Questions

#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> using namespace std; int main() { double u = 0; double w
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have the following code fragment #include <iostream> #include <ostream> #include <vector> using namespace
Here is the code (also at http://pastebin.com/yw5z2hnG ): #include <iostream> #include <vector> using namespace
#include iostream #include vector class ABC { }; class VecTest { std::vector<ABC> vec; public:
Why on earth I can do this: #include <iostream> #include <algorithm> #include <vector> using
#include iostream #include vector class ABC { private: bool m_b; public: ABC() : m_b(false)

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.