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

  • SEARCH
  • Home
  • 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 8127863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:40:53+00:00 2026-06-06T07:40:53+00:00

The following program, compiled with g++ 4.6, yields the error request for member ‘y’

  • 0

The following program, compiled with g++ 4.6, yields the error

request for member ‘y’ in ‘a2’, which is of non-class type ‘A<B>(B)’

at its last line:

#include <iostream>

template <class T> class A
{
public:
  T y;
  A(T x):y(x){}
};

class B
{
public:
  int u;
  B(int v):u(v){}
};

int main()
{
  int v = 10;
  B b1(v);

  //works
  A<B> a1(b1);

  //does not work (the error is when a2 is used)
  A<B> a2(B(v));

  //works
  //A<B> a2((B(v)));

  std::cout << a1.y.u << " " << a2.y.u << std::endl;    
}

As can be seen from the working variant included in the code, adding parentheses around the arguments of the constructor of A solves the problem.

I have seen some related errors caused by a the interpretation of a constructor invocation as a function declaration, like when creating an object with no argument to its constructor, but with braces:

myclass myobj();

but it seems to me that

A<B> a2(B(v));

cannot be interpreted as a function declaration.

Someone can explain to me what is happening?

  • 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-06-06T07:40:55+00:00Added an answer on June 6, 2026 at 7:40 am

    It’s a case of most vexing parse where the compiler interprets A<B> a2(B(v)) as the declaration of a function. Such that:

    A<B> is the return type
    a2 is the function name
    B is the type of the argument
    v is the argument name

    So, when you are doing

    std::cout << a1.y.u << " " << a2.y.u << std::endl;
    

    The compiler does not think of a2.y.u as a class, that’s why you are getting the non-class type error.

    Also, since double parenthesis aren’t allowed in a function declaration, the version A<B> a2((B(v))); works because the compiler doesn’t interprets it as a function declaration anymore, but as a variable declaration.

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

Sidebar

Related Questions

I successfully compiled the following program simple.cc: #include <gtkmm.h> int main (int argc, char
Consider the following program, which is obviously buggy: #include <cstdio> double test(int n) {
I compiled the following program with gcc -fprofile-arcs -ftest-coverage test.c: int main() { int
The following C++ program compiles and runs as expected: #include <stdio.h> int main(int argc,
I'm trying to compile the following program: #include<functional> #include<iostream> int main(int argc, char* argv[],
I have the following program which compiles fine and with no errors or warnings:
I have the following code that cannot be compiled: #include <iostream> #include <set> #include
I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t
The following program, was compiled and tested, it sometimes return the result, and sometimes
I have a program following: class INT { public: INT(int ii = 0) :

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.