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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:52:51+00:00 2026-06-15T05:52:51+00:00

I tried to implement the operator[] for a container of mine. But I am

  • 0

I tried to implement the operator[] for a container of mine. But I am really new to c++, and it seems I have an error in my implementation.

I declared them like this:

float& operator[](const int &idx);
const float& operator[](const int &idx) const;

which should be fine, it’s pretty much copy/paste from tutorials. Now, Quaternion.cpp looks like this:

float& Quaternion::operator[](const int &idx)
{
    if(idx == 0)
    {
        return x;
    }
    if(idx == 1)
    {
        return y;
    }
    if(idx == 2)
    {
        return z;
    }
    if(idx == 3)
    {
        return w;
    }
    std::cerr << "Your Quaternion is only accessible at positions {0, 1, 2, 3}!" 
              << std::endl;
    return x;
}

const float& Quaternion::operator[](const int &idx)
{
    if(idx == 0)
    {
        return const x;
    }
    if(idx == 1)
    {
        return const y;
    }
    if(idx == 2)
    {
        return const z;
    }
    if(idx == 3)
    {
        return const w;
    }
    std::cerr << "Your Quaternion is only accessible at positions {0, 1, 2, 3}!" 
         << std::endl;
    return x;
}

I get the error for the signature “const float& Quaternion::operator[](const int &idx)”.

Another thing which happened before, was that I couldn’t return 0 in case the boundaries got exceeded. Maybe I will, once this issue is resolved, but it gave me an error message before. I just returned x then, which makes me really unhappy.

  • 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-15T05:52:52+00:00Added an answer on June 15, 2026 at 5:52 am

    You left out the trailing const modifier from the second (const) operator implementation:

    const float& Quaternion::operator[](const int &idx) const
    {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to implement css sticky footer on my page but it doesn't
I have tried to implement own operator and used in map<>, code looks as
I tried to implement this: namespace Test { void* operator new(size_t s) { return
I would like to have a class implement operator() several different ways based on
I tried to implement the Hover effect for images just like Google Images in
I tried to implement __concat__ , but it didn't work >>> class lHolder(): ...
I want to have something like this below: template <class T> struct Container{ public:
I have an abstract functor class that overloads operator() and derived objects that implement
I have been trying to implement the solution referenced at: Error when trying to
I tried to implement a simple yes/no input with java.util.Scanner. My code looks like

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.