【发布时间】:2017-05-06 21:08:12
【问题描述】:
- 我收到错误 C2440: '=' : cannot convert from 'math::Vec3' to 'float' 但我不明白为什么。我认为 buffer 是 Vec3 类型 并且 v 对象也是 Vec3 类型。
- Vec3 类用作通用 3 维向量,因此它定义了几个可用于 Vec3 和 S 数据的数值运算符。
Image.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
Image.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
【问题讨论】:
-
FWIW 这绝对是“优先聚合而不是继承”的情况。没有理由
Image应该从Array派生,而是应该有一个Array作为成员。 -
为什么你的
buffer是一个指针,而不是一个实例? 我认为缓冲区是 Vec3 类型,你为什么会这样认为?它的类型是指向 Vec3 的指针,因此,buffer[s + RED]语句会生成一个Vec3<float>类型的实例,当然,它不能转换为简单的浮点数。跨度> -
这不是问题,但是以下划线开头后跟大写字母 (
_IMAGE) 的名称和包含两个连续下划线的名称保留供实现使用。不要在你的代码中使用它们。