仰望—星空 2023-06-05 13:25 采纳率: 93.3%
浏览 13
已结题

C++软光栅报错,无法访问成员

我想定义一个四维向量,在最后对运算符+的实现出现了问题,代码如下:

#pragma once
#include <stdexcept>
#include <intrin.h>
#include "Preprocessor.h"


class Vector4
{
public:
    Vector4(float vx, float vy, float vz, float vw);
    Vector4();
    Vector4(float vx, float vy, float vz);

    __forceinline explicit Vector4(__m128 m) :Vector4(m.m128_f32[0], m.m128_f32[1], m.m128_f32[2], m.m128_f32[3]) {}

    Vector4 operator+(const Vector4& right)const;
    Vector4 operator-(const Vector4& right)const;
    Vector4 operator*(float right)const;
    Vector4 operator/(float right)const;

    float SquareMagnitude();
    float Magnitude();
    Vector4& Normalize();

    float static Dot(const Vector4& left, const Vector4& right);
    float  Dot(const Vector4& right);
    Vector4 static Cross(const Vector4& left, const Vector4& right);
    Vector4 static Lerp(const Vector4& left, const Vector4& right, float t);
    void standardization()
    {
        if (w == 0)
        {
            //std::cout << "error w==0" << std::endl;
            return;
        }
        x /= w;
        y /= w;
        z /= w;
        w = 1;
        
    }
    void Print();

    __m128 getM128()
    {
        return _mm_set_ps(x, y, z, w);
    }

    __forceinline float& operator[](int index);
    

private:
    float x, y, z, w;
};


Vector4::Vector4(float vx, float vy, float vz, float vw) :x(vx), y(vy), z(vz), w(vw) {}
Vector4::Vector4() : Vector4(0, 0, 0, 1) {}
Vector4::Vector4(float vx, float vy, float vz) :Vector4(vx, vy, vz, 1) {}



// 报错,无法访问this中的getM128函数,right更是什么成员都访问不到
Vector4 Vector4::operator+(const Vector4& right)const
{
    return Vector4(_mm_add_ps(this.getM128(), right.getM128()));
}


报错:

img

  • 写回答

3条回答 默认 最新

  • 仰望—星空 2023-06-05 14:04
    关注

    img


    解决了,不应该加上this指针,或者将函数声明为__m128 getM128()const;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月13日
  • 已采纳回答 6月5日
  • 创建了问题 6月5日