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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:04:31+00:00 2026-06-09T01:04:31+00:00

Whenever I assign an already declared Matrix to somethnig else my program exits unexpectedly.

  • 0

Whenever I assign an already declared Matrix to somethnig else my program exits unexpectedly.

I really don’t know why this happens, I was under the impression that leaving assignment operators for the compiler to generate was the best thing to do unless you absolutely had to overload it, which makes me think there’s something wrong with my constructor or something?

Example:

Matrix rotation = rotationMatrix3(Y, degToRad(20.0f));
Vector3 left = rotation * direction_;

rotation = rotationMatrix3(Y, degToRad(-20.0f)); //crash
Vector3 right = rotation * direction_;

Here’s my Matrix class:

Matrix.h

enum Axis {
    X, Y, Z,
};

class Matrix {
public:
    Matrix(int cols, int rows);
    Matrix(int cols, int rows, const float values[]);
    ~Matrix();

    float& operator()(int col, int row);
    float  operator()(int col, int row) const;
    inline int cols() const { return cols_; }
    inline int rows() const { return rows_; }

private:
    int cols_, rows_;
    float* values_;
};

Vector3 operator*(const Matrix& m, const Vector3& v);

Matrix rotationMatrix3(int axis, float rads);

Matrix.cpp

Matrix::Matrix(int cols, int rows) : cols_(cols), rows_(rows), values_(NULL) {
    values_ = new float[rows_ * cols_];
}

Matrix::Matrix(int cols, int rows, const float values[]) : cols_(cols), rows_(rows), values_(NULL) {
    values_ = new float[rows_ * cols_];

    for(int c = 0; c < cols; ++c) {
        for(int r = 0; r < rows; ++r) {
            (*this)(c, r) = values[r * cols + c];
        }
    }
}

Matrix::~Matrix() {
    delete [] values_;
}

float& Matrix::operator()(int col, int row) {
    return values_[row * cols_ + col];
}

float Matrix::operator()(int col, int row) const {
    return values_[row * cols_ + col];
}

Vector3 operator*(const Matrix& m, const Vector3& v) {
    if(m.cols() != 3) {
        throw std::logic_error("Matrix must have only 3 cols");
    }

    Vector3 result;
    result.x = m(0, 0) * v.x + m(1, 0) * v.y + m(2, 0) * v.z;
    result.y = m(0, 1) * v.x + m(1, 1) * v.y + m(2, 1) * v.z;
    result.z = m(0, 2) * v.x + m(1, 2) * v.y + m(2, 2) * v.z;

    return result;
}

Matrix rotationMatrix3(int axis, float rads) {
    float c = static_cast<float>(cos(rads));
    float s = static_cast<float>(sin(rads));

    if(axis == X) {
        const float mat[] = { 1.0f, 0.0f, 0.0f,
                              0.0f, c,    -s,
                              0.0f, s,    c };
        return Matrix(3, 3, mat);
    } else if(axis == Y) {
        const float mat[] = { c,    0.0f, s,
                              0.0f, 1.0f, 0.0f,
                              -s,   0.0f, c };
        return Matrix(3, 3, mat);
    } else if(axis == Z) {
        const float mat[] = { c,    -s,   0.0f,
                              s,    c,    0.0f,
                              0.0f, 0.0f, 1.0f };
        return Matrix(3, 3, mat);
    } else {
        throw std::logic_error("Unknown axis");
    }
}
  • 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-09T01:04:35+00:00Added an answer on June 9, 2026 at 1:04 am

    I was under the impression that leaving assignment operators for the compiler to generate was the best thing to do unless you absolutely had to overload it, which makes me think there’s something wrong with my constructor or something?

    Drop that impression. Those freebies from the language are rarely the “right” thing to do. You have a raw pointer float* values_; in your class. You absolutely have to overload the copy constructor and the assignment operator here.

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

Sidebar

Related Questions

Whenever I try installing anything using gem, I get this error - murtaza@murtaza-dev:~$ sudo
Whenever the activity that initiates the scan happens, the android application force closes. It
how to assign a test file in resharper? whenever I select 'run tests' it
I am trying to assign a asp.net label using JavaScript and then whenever label
How to assign them to have tag: self.images = [NSMutableArray arrayWithObjects: @1.jpg,@2.jpg,@3.jpg,@4.jpg,@5.jpg,nil]; Whenever UIScrollView
Whenever I assign a new value to a parameter, I get a bus error.
In jQuery, if I assign class=auto_submit_form to a form, it will be submitted whenever
I am getting a parser error whenever I try to execute a program. Parse
Whenever I call GC.SupressFinalizer() in the Dispose method, should I assign null to all
I am using Wpf Toolkit DataGrid. Whenever I assign Itemssource to it, its first

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.