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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:24:41+00:00 2026-06-17T16:24:41+00:00

I have a vector class look like this: class Vector3 { public: Vector3(){m_x =

  • 0

I have a vector class look like this:

class Vector3
{

 public:
   Vector3(){m_x = m_y = m_z = 0.0f;}
   Vector3(const float & i_x, const float & i_y, const float & i_z):
     m_x(i_x), 
     m_y(i_y),
     m_z(i_z)
     {}

   Vector3 operator+(const Vector3 & i_other);
private:
   float m_x;
   float m_y;
   float m_z;
};

Vector3::Vector3 Vector3::operator+(const Vector3 & i_other)
{
   float tx = m_x + i_other.m_x;
   float ty = m_y + i_other.m_y;
   float tz = m_z + i_other.m_z;
   return Vector3(tx, ty, tz);
}

Obviously, the Vector3::operator+ definition synax is wrong because the return type is Vector3::Vector3, not Vector3. Vector3::Vector3 means there is a namespace Vector3, and inside the name space there is a class Vector3. But I only have a class Vector3, no namespace here.

My question is, in Ubuntu 12.04, the syntax above can not be compiled (Ubuntu’s g++ compiler is [gcc version 4.6.3]). However, in Mac, g++ can compile the code(Mac’s g++ compiler is [gcc version 4.2.1]). Also, I test this syntax in a Red Hat linux machine, it also works (g++ version is [gcc version 4.4.6])

So, is it different version of gcc have different compile principle? Or, my g++ in Ubuntu broke?

  • 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-17T16:24:43+00:00Added an answer on June 17, 2026 at 4:24 pm

    The older compiler is incorrect. Little surprise there.

    It is probably parsing Vector3::Vector3 as an injected-type-name. Inside the scope of class Vector3 { }, the identifier Vector3 refers to the class, not the constructor (except when you’re declaring the constructor, of course). And at first glance, you might think it means the same thing in a return type, because §3.4/3 (I’m using the C++11 standard here) says

    The injected-class-name of a class (Clause 9) is also considered to be a member of that class for the purposes of name hiding and lookup.

    Digging deeper, in §3.4.3.1/2,

    In a lookup in which the constructor is an acceptable lookup result and the nested-name-specifier nominates
    a class C:

    — if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name
    of C (Clause 9)

    the name is instead considered to name the constructor of class C.

    The context of starting a declaration with an injected-class-name happens to be the same as in defining a constructor outside class {} scope, a la

    class Vector3 { … };
    
    Vector3::Vector3(){m_x = m_y = m_z = 0.0f;}
    

    The older GCC noticed that the declaration wasn’t a constructor, then took a fallback path that did work. However that fallback was illegal because C++ specifies that in a context where the constructor could be the result of the lookup, it is the only valid lookup result.

    In all probability, some user took the time to file a bug, and a GCC developer took the time to diagnose this, fix it, and write a testcase. Multiply across the number of trivialities in a complex language like C++, and you start to appreciate the effort put into your compiler.

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

Sidebar

Related Questions

Let's say we have a class that looks like this: class A { public:
Let's say I have objects which look very roughly like this: class object {
I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
I have class A: public B { ...} vector<A*> v; I want to do
I have something like this: enum EFood{ eMeat, eFruit }; class Food{ }; class
Given I have class IChunk { public: const char* getRawData() = 0; ... };
I have a vector class and I defined the __mul__ method to multiply a
I have a vector of pointers to class objects. These class objects invoke new
I have a vector of pointers to a class. I need to call their
I have a vector called players and a class called Player. And what I'm

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.