错误 C2593:“运算符”含糊不清
如果我有以下文件,我会收到此错误(VC9 中的 c2593)。 如果我取消注释 main.cpp 中的原型,错误就会消失。我需要保持相同的功能,同时将类保留在 main.cpp 之外。我怎样才能做到这一点?
谢谢。
main.cpp:
#include "number.h"
//const Number operator + (const Number & lhs, const Number & rhs);
int main(void)
{
Number n1(2); // n1 = 2
Number n2(9,3); // n2 = 3
Number n3 = n1+n2; // n3 = 5
}
number.h:
struct Number
{
int num;
Number(int n=0,int d=1) {num = n/d;}
operator int() {return num;}
operator double() {return num*1.0;}
};
number.cpp:
#include "Number.h"
const Number operator + (const Number & lhs, const Number & rhs)
{
Number tmp;
tmp.num = lhs.num + rhs.num;
return tmp;
}
If I have the following files, I get this error (c2593 in VC9).
If I un-comment the prototype in main.cpp, the error disappears. I need to maintain the same functionality while keeping the class out of main.cpp. How can I do that?
Thanks.
main.cpp:
#include "number.h"
//const Number operator + (const Number & lhs, const Number & rhs);
int main(void)
{
Number n1(2); // n1 = 2
Number n2(9,3); // n2 = 3
Number n3 = n1+n2; // n3 = 5
}
number.h:
struct Number
{
int num;
Number(int n=0,int d=1) {num = n/d;}
operator int() {return num;}
operator double() {return num*1.0;}
};
number.cpp:
#include "Number.h"
const Number operator + (const Number & lhs, const Number & rhs)
{
Number tmp;
tmp.num = lhs.num + rhs.num;
return tmp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试将原型放入 Number 头文件中:
number.h:
number.cpp:
main.cpp:
Try putting the prototype in the Number header file:
number.h:
number.cpp:
main.cpp:
您永远不会在 number.h 中声明运算符 +,您只会在 number.cpp 中定义它 - 因此,当您在 main.cpp 中包含 number.h 时,它不会知道去哪里找到运算符+。
您必须将运算符 + 的声明放在类外部的 number.h 中,然后在 number.cpp 中定义它
You never declare operator + in number.h, you only define it in number.cpp - therefore, when you include number.h in main.cpp, it doesn't know where to go to find operator +.
You must put the declaration of operator + in number.h, outside of the class, then define it in number.cpp
该注释行应该放在 number.h 中
编辑:在 number.h 中,但作为一个自由函数。
That commented line should go in number.h
EDIT: in number.h but as a free function.
在您带着非常相似的问题返回给我们之前,您的代码还需要注意另一件事:最好删除
operator int
和operator double
函数。它们会让你非常头痛。让我们举几个例子:在您原来的情况下,您的添加是不明确的,因为有
operator+(double, double)
和operator+(int, int)
内置运算符考虑了一下,他们同样好。其他人解决了这个问题。但在开始并遇到这些其他问题之前,最好删除转换函数并插入显式函数,例如asDouble
或类似的函数。One other thing to note with your code before you go back to us with a very similar question: Better remove the
operator int
andoperator double
functions. They will cause you major headache. Let's make a few examples:In the original situation you had, your addition was ambiguous, because there were
operator+(double, double)
andoperator+(int, int)
builtin operators considered and they were equally well. Others solved that problem. But before you start and run into these other problems, better remove the conversion functions and insert explicit functions likeasDouble
or something similar.除了其他人在头文件中声明operator+的答案之外,我建议您在结构中也包含operator+=。
这样调用 x += y; 是高效的。而不是 x = x + y;
Apart from other's answers to declare operator+ in the header file, I suggest you to have operator+= in your struct as well.
This way it is efficient to call x += y; instead of x = x + y;
就我个人而言,我更喜欢在类中声明运算符:
然后:
Personally I like it better to declare the operators inside the class:
and then: