C++ 错误:不存在从 "math::Vec3<float>" 到 "float" 的合适转换函数

c++ error:no suitable conversion function from "math::Vec3<float>" to "float" exists

本文关键字:float 转换 gt 函数 lt 不存在 错误 math Vec3 C++      更新时间:2023-10-16
  • 我收到错误 C2440:"=":无法从"math::Vec3"转换为"float",但我不明白为什么。我认为缓冲区是 Vec3 类型v 对象也是 Vec3 类型
  • Vec3
  • 类用作通用三维向量,因此它定义了几个可用于Vec3和 S 数据的数值运算符。

图片.h

#ifndef _IMAGE
#define _IMAGE
#include "Array.h"
#include "Serializable.h"
#include "Vec3.h"
using namespace math;
class Image : public Array<Vec3<float>>, public Serializable
{
public:
enum channel_t { RED = 0, GREEN, BLUE };          
protected:
Vec3<float> *buffer;    
public:
Vec3<float> getPixel(unsigned int x, unsigned int y) const;
/*code*/
};
#endif

图片.cpp

#include "Image.h"
using namespace math;
Vec3<float> Image::getPixel(unsigned int x, unsigned int y) const  {
math::Vec3<float> v;
int s = 0;
if ((x <= Image::getWidth()) && (y <= Image::getHeight()) && (x >= 0) && (y >= 0))
{
for (unsigned int i = 1; i == getWidth(); i++)
{
for (unsigned int j = 1; i == getHeight(); j++)
{
if (i == x - 1 && j == y - 1)
goto label;
else
s = s + 3;
}
}
label:
v[Image::RED] = buffer[s + RED];       <-------------------HERE 
v[Image::GREEN] = buffer[s + GREEN];   <-------------------I GET AN
v[Image::BLUE] = buffer[s + BLUE];     <-------------------ERROR
}
return v;
}//end of getPixel

Vec3.h

#ifndef _Vec3_ 
#define _Vec3_
namespace math
{
template <typename S>
class Vec3
{
public:
// data members
//! The first coordinate of the vector
union { S x, r; }; 
//! The second coordinate of the vector
union { S y, g; }; 
//! The third coordinate of the vector
union { S z, b; }; 
S & operator [] (size_t index)
{
return *((S*)this + index);
}
Vec3<S> operator + (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x + right.x;
left.y = y + right.y;
left.z = z + right.z;
return left;
}
Vec3<S> operator - (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x - right.x;
left.y = y - right.y;
left.z = z - right.z;
return left;
}
Vec3<S> operator * (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x * right.x;
left.y = y * right.y;
left.z = z * right.z;
return left;
}
Vec3<S> operator * (S right)
{
Vec3<S> left;
left.x = x * right;
left.y = y * right;
left.z = z * right;
return left;
}
Vec3<S> operator / (S right)
{
Vec3<S> left;
left.x = x / right;
left.y = y / right;
left.z = z / right;
return left;
}
Vec3<S> operator / (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x / right.x;
left.y = y / right.y;
left.z = z / right.z;
return left;
}
Vec3<S> & operator += (const Vec3<S> & right)
{
x += right.x;
y += right.y;
z += right.z;
return *this;
}
Vec3<S> & operator -= (const Vec3<S> & right)
{
x -= right.x;
y -= right.y;
z -= right.z;
return *this;
}
Vec3<S> & operator /= (const Vec3<S> & right)
{
x /= right.x;
y /= right.y;
z /= right.z;
return *this;
}
Vec3<S> & operator *= (const Vec3<S> & right)
{
x *= right.x;
y *= right.y;
z *= right.z;
return *this;
}
Vec3<S> & operator *= (S right)
{
x *= right;
y *= right;
z *= right;
return *this;
}
Vec3<S> & operator /= (S right)
{
x /= right;
y /= right;
z /= right;
return *this;
}
Vec3<S>(S x, S y, S z) : x(x), y(y), z(z) {}
Vec3<S>(S val) : x(val), y(val), z(val) {}
Vec3<S>() : x(), y(), z() {}
Vec3<S>(const Vec3<S> & right) : x(right.x), y(right.y), z(right.z) {}
Vec3<S> & operator = (const Vec3<S> & right)
{
x = right.x;
y = right.y;
z = right.z;
return *this;
}
bool operator == (const Vec3<S> & right) const
{
return x == right.x && y == right.y && z == right.z;
}
bool operator != (const Vec3<S> & right) const
{
return x != right.x || y != right.y || z != right.z;
}
};
template<typename S>
Vec3<S> operator * (S a, Vec3<S> v)
{
return v*a;
}

template<typename S>
Vec3<S> operator * (int a, Vec3<S> v)
{
return v*S(a);
}
template<typename S>
Vec3<S> operator * (Vec3<S> v, int a)
{
return v*S(a);
}
template<typename S>
Vec3<S> operator / (Vec3<S> v, int a)
{
return v/S(a);
}
} // namespace math
#endif

v 和 buffer 有不同的类型:

Vec3<float> *buffer;    
math::Vec3<float> v;

缓冲区是指向 Vec3 的指针,当你写 "buffer[s + RED];"时,你试图获取指针的 s + RED 元素。但是在 v 向量和 v[Image::RED] 中是红色通道(它是浮动的)。

尝试使用这个:

v[Image::RED] = buffer[s][RED];

buffer不是math::Vec3类型,而是指向此类对象的指针,该对象也可以解释为此类对象的数组。buffer[s + RED]是此数组的一个元素,因此它是math::Vec3类型,而v[Image::RED]float类型。

你可能想写

v[Image::RED] = buffer[s][Image::RED];

buffer是指向Vec3<float>指针,所以buffer[k]是一个Vec3<float>
v[x]float,不是Vec3<float>

索引表明您打算将像素buffer平面"表示为floats,在这种情况下,您可以使用

std::vector<float> buffer;

(或者char* buffer;,但这会带来一大堆内存管理问题。

您可以使用算术而不是搜索循环:

Vec3<float> Image::getPixel(unsigned int x, unsigned int y) const  {
math::Vec3<float> v;
if (x < getWidth() && y < getHeight() && x >= 0 && y >= 0)
{
int s = 3 * y * getWidth() + 3 * x;
v[RED] = buffer[s + RED];
v[GREEN] = buffer[s + GREEN];
v[BLUE] = buffer[s + BLUE];
}
return v;
}

(这使用从零开始的索引;您的代码似乎与像素是从零开始还是从 1 开始存在冲突。

v

的类型是math::Vec3<float>v[0]floatbuffer是指向Vec3<float>的指针,通过为其编制索引,您可以使用operator[]指针,即数组访问。所以通过buffer[i]你会得到一个Vec3<float>,它不能转换为简单的float