英文:
argument of type * is incompatible with parameter of type "const float *"
问题
我是你的中文翻译,以下是你要的翻译内容:
我对C++相对新手。我已经进行了广泛的搜索,但无法使其工作。
我定义了一个自定义类型叫做`vec_t`:
```cpp
class FloatType {
private:
float value;
public:
FloatType() : value(0.0f) {}
FloatType(float v) : value(v) {}
//operator float() { return value; }
//operator float() { return value; }
//explicit operator const float* () { return &value; }
//operator float* () const { return &value; }
//operator const float* () { return &value; }
//operator const float() { return (const float)value; }
//operator const float*() { return &value; }
};
typedef FloatType vec_t;
我还创建了一个以const float *
作为唯一参数的函数:
void Create(const float* vColor = NULL);
void Create(const float* vColor) {
//...
}
现在,当像这样调用函数时:
vec_t a = { 2.5f };
vec_t* b = &a;
Create(b);
Visual Studio Community 2019 (v142) 报错:
> 类型为 "vec_t *" 的参数与类型为 "const float *" 的参数不兼容
现在,立即将其转换为const float *
可以解决问题:
Create((const float *)b);
但是,我的目标是拥有一个隐式转换操作符,将我的类型隐式转换为const float *
,但无论我如何努力,似乎都无法做对。
所有的operator
注释都是我的尝试,它们都具有有效的语法,但它们无法解决问题。
我不明白的是什么?
我希望能够从FloatType
类本身处理这个问题,就我所知,隐式转换操作符是实现这一目标的方法。
请不要引导我使用其他方法,这纯粹是为了练习和锻炼。
英文:
I'm fairly new to C++. I've searched extensively but I couldn't make it work.
I've defined a custom type called vec_t
:
class FloatType {
private:
float value;
public:
FloatType() : value(0.0f) {}
FloatType(float v) : value(v) {}
//operator float() { return value; }
//operator float() { return value; }
//explicit operator const float* () { return &value; }
//operator float* () const { return &value; }
//operator const float* () { return &value; }
//operator const float() { return (const float)value; }
//operator const float*() { return &value; }
};
typedef FloatType vec_t;
I've also created a function which takes a const float *
as its only argument:
void Create(const float* vColor = NULL);
void Create(const float* vColor) {
//...
}
Now, when calling the function like so:
vec_t a = { 2.5f };
vec_t* b = &a;
Create(b);
Visual Studio Community 2019 (v142) is complaining:
> argument of type "vec_t *" is incompatible with parameter of type "const float *"
Now, casting it to const float *
on the spot gets it done:
Create((const float *)b);
But, my goal is to have an implicit casting operator to implicitly convert from my type to const float *
, but no matter my efforts, I can not seem to have it right.
All operator
comments were my attempts and they all have valid syntax, but they don't make the problem go away.
What am I not understanding?
I want to be able to handle this from within the FloatType
class itself, and to my knowledge, an implicit casting operator is the way to do so.
Don't guide me to another approach, this is purely for practice and exercise.
答案1
得分: 1
vec_t
类型不是一个指针,所以在调用 Create()
时不应该使用指针作为参数:
class FloatType
{
private:
float value;
public:
...
operator const float* () const { return &value; }
// 这里需要加上 const ^^^^^
};
typedef FloatType vec_t;
vec_t a = { 2.5f };
Create(a);
由于某种原因,你引入了指针 b
,这是你的代码不工作的主要原因。
另外,你的转换运算符上应该有额外的 const
,就像我上面指示的那样。
英文:
Your vec_t
type is not a pointer, so you should not be using a pointer as the argument when calling Create()
:
class FloatType
{
private:
float value;
public:
...
operator const float* () const { return &value; }
// const here ^^^^^
};
typedef FloatType vec_t;
vec_t a = { 2.5f };
Create(a);
For some reason, you introduced the pointer b
, that's the main reason your code doesn't work.
Also, you should have an extra const
on your conversion operator, as I've indicated above.
答案2
得分: 1
问题在于,虽然vec_t
是一个可以定义转换运算符的类类型,但vec_t *
是一个基本类型(指针),不适用于用户定义的转换。
一些可能的解决方案:
- 不要传递指向
vec_t
的指针,而是传递它们的值。如果必要,使用std::move
来避免可能昂贵的拷贝。这可以简化为只是Create(a)
。约翰的答案在更多细节上解释了这一点。 - 对指针进行解引用,例如
Create(*b)
,以允许编译器找到用户定义的转换。 - 在
vec_t
中添加一个float * data()
成员函数,就像std::vector
一样,并调用它来访问底层数据:Create(b->data())
。 - 不要使用自己的向量类型,而是使用
std::array<float, N>
来表示静态大小的向量,使用std::vector<float>
来表示动态大小的向量。**这可能是最好的解决方案。**要获取原始数据的指针,使用vec.data()
。(再次利用这些类型的值语义来获得内存安全性。移动/复制它们,不要使用原始指针。) - 如果你想要做一些真正丑陋的事情,你不应该这样做:
Create(b->operator float *())
。
英文:
The problem is that while vec_t
is a class type that can have conversion operators defined, vec_t *
is a primitive type (a pointer), and no user-defined conversions apply to it.
Some possible solutions:
- Don't pass around pointers to
vec_t
, pass them by value instead. If necessary, usestd::move
to avoid potentially expensive copies. This boils down to justCreate(a)
. john's answer explains this in more detail. - Dereference the pointer, as in
Create(*b)
, to allow the compiler to find your user-defined conversions. - Add a
float * data()
member function tovec_t
, just likestd::vector
, and call that to get at the underlying data:Create(b->data())
- Don't use your own vector type and just use
std::array<float, N>
for statically-sized vectors andstd::vector<float>
for dynamically sized ones. This is likely the best solution. To get a pointer to the raw data, usevec.data()
. (Again, take advantage of these types' value semantics to get memory safety. Move / copy them, don't use raw pointers.) - If you want something really ugly that you shouldn't do:
Create(b->operator float *())
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论