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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:54:33+00:00 2026-06-14T12:54:33+00:00

I’m trying to familiarize myself with operators in C++. I figured I would do

  • 0

I’m trying to familiarize myself with operators in C++. I figured I would do so with a simple case of vector addition. Unfortunately, I seem to have run into some issues. My class definition is as below:

#ifndef _MVEC_H_
#define _MVEC_H_

#include "Error.h" //I define things like throw(message) here, it works and is not the issue

class MVec {
        private:
                double vec[3];
        public:
                MVec();
                MVec(double &);
                MVec(double *);
                MVec(MVec &);
                MVec & operator=(MVec &);
                inline double & operator[](const int i);
                inline const double & operator[](const int i) const;
                MVec operator+(const MVec &) const;
                ~MVec();
};

MVec::MVec() {}

MVec::MVec(double &a) {
        for(int i = 0; i < 3; i++)
                vec[i] = a;
}

MVec::MVec(double *a) {
        for(int i = 0; i < 3; i++)
                vec[i] = *a++;
}

MVec::MVec(MVec &rhs) {
        for(int i = 0; i < 3; i++)
                vec[i] = rhs[i];
}

MVec & MVec::operator=(MVec &rhs) {
        if(this != &rhs)
                for(int i = 0; i < 3; i++)
                        vec[i] = rhs[i];

        return *this;
}

inline double & MVec::operator[](const int i) {
        #ifdef _CHECKBOUNDS_
        if(i < 0 || i >= 3)
                throw("Subscript out of bounds");
        #endif

        return vec[i];
}

inline const double & MVec::operator[](const int i) const {
        #ifdef _CHECKBOUNDS_
        if(i < 0 || i >= 3)
                throw("Subscript out of bounds");
        #endif

        return vec[i];
}

MVec MVec::operator+(const MVec &vec1) const {
        MVec ans;

        for(int i = 0; i < 3; i++)
                ans[i] = vec[i] + vec1[i];

        return ans;
}

MVec::~MVec() {
        delete[] vec;
}

#endif

The [] operator appears to work as intended. Unfortunately, the vector addition operator does not. Specifically, when I run the code:

#include "Error.h"
#include "MVec.h"
#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[]) {
        MVec a, b, c;
        a[0] = 1; a[1] = 2; a[2] = 3;
        b[0] = 5.9906; b[1] = 72.1139; b[2] = 83.1324;

        //c = a + b;

        std::cout << (a + b)[0] << std::endl;
        std::cout << (a + b)[1] << std::endl;
        std::cout << (a + b)[2] << std::endl;

        exit(0);
}

When I uncomment the line c = a + b; I get a compiler error:

no match for ‘operator=’ in ‘c = MVec::operator+(const MVec&)
const(((const MVec&)((const MVec*)(& b))))’

When I comment it out, I get a glibc detected error after the first std::cout. Presumably, I am doing something wrong with the temporary variable I am creating in the operator+ function. Unfortunately, I am not (quite) smart enough to figure out what. Any and all insight into this would be very helpful.

  • 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-14T12:54:34+00:00Added an answer on June 14, 2026 at 12:54 pm

    You need to take a const reference to MVec in your copy constructor in order to be able to use it with temporaries:

    MVec(const MVec &);
    

    The same applies to the assignment operator, and the constructor taking a double:

    MVec(const double &); // or no reference, MVec(double);
    MVec& operator=(const MVec& rhs);
    

    You should also remove the delete [] vec from the destructor, because vec is not dynamically allocated. This is the likely cause of the glibc error.

    Now, in order for expressions such as

    SomeVec = 1.0 + SomeOtherVec;
    

    you need to declare the operator+ as a non-member fumction:

    MVec operator+(const MVec& lhs, const MVec& lhs); 
    

    This will allow for implicit conversions both on the LHS and on the RHS. In general it is a good idea to have these types of operators as non-member functions, to guarantee symmetry between LHS and RHS operands.

    On the other hand, it probably makes more sense not to allow the implicit conversions from double at all. You can achieve this by making the relevant constructor explicit:

    explicit MVec(double);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.