错误 C2593:“运算符”含糊不清

发布于 2024-08-23 11:39:52 字数 775 浏览 1 评论 0原文

如果我有以下文件,我会收到此错误(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

川水往事 2024-08-30 11:39:53

尝试将原型放入 Number 头文件中:

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;}
};

const Number operator + (const Number & lhs, const Number & rhs);

number.cpp:

#include "Number.h"

const Number operator + (const Number & lhs, const Number & rhs)
{
Number tmp;
tmp.num = lhs.num + rhs.num;
return tmp;
}

main.cpp:

#include "number.h"

int main(void)
{
    Number n1(2);       // n1 = 2
    Number n2(9,3);     // n2 = 3
    Number n3 = n1+n2;  // n3 = 5
}

Try putting the prototype in the Number header file:

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;}
};

const Number operator + (const Number & lhs, const Number & rhs);

number.cpp:

#include "Number.h"

const Number operator + (const Number & lhs, const Number & rhs)
{
Number tmp;
tmp.num = lhs.num + rhs.num;
return tmp;
}

main.cpp:

#include "number.h"

int main(void)
{
    Number n1(2);       // n1 = 2
    Number n2(9,3);     // n2 = 3
    Number n3 = n1+n2;  // n3 = 5
}
孤独患者 2024-08-30 11:39:53

您永远不会在 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

七婞 2024-08-30 11:39:53

该注释行应该放在 number.h 中

编辑:在 number.h 中,但作为一个自由函数。

That commented line should go in number.h

EDIT: in number.h but as a free function.

落墨 2024-08-30 11:39:53

在您带着非常相似的问题返回给我们之前,您的代码还需要注意另一件事:最好删除 operator intoperator double 函数。它们会让你非常头痛。让我们举几个例子:

Number a, b;

1 + b;
// ambiguous: operator+(int, int) or 
//            operator+(Number, Number) ?

// did you intend to use those for this case?
float x = a; 
// ambiguous: from int -> float or
//                 double -> float ?

在您原来的情况下,您的添加是不明确的,因为有 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 and operator double functions. They will cause you major headache. Let's make a few examples:

Number a, b;

1 + b;
// ambiguous: operator+(int, int) or 
//            operator+(Number, Number) ?

// did you intend to use those for this case?
float x = a; 
// ambiguous: from int -> float or
//                 double -> float ?

In the original situation you had, your addition was ambiguous, because there were operator+(double, double) and operator+(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 like asDouble or something similar.

去了角落 2024-08-30 11:39:53

除了其他人在头文件中声明operator+的答案之外,我建议您在结构中也包含operator+=。

struct Number
{
// your other declarations.
Number& operator+=(const Number& other)
{
   this->num += other.num;
   return *this;
}
};

const Number operator+(const Number& lhs, const Number& rhs)
{
    Number ret(lhs);
    ret += rhs;
    return ret;
}

这样调用 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.

struct Number
{
// your other declarations.
Number& operator+=(const Number& other)
{
   this->num += other.num;
   return *this;
}
};

const Number operator+(const Number& lhs, const Number& rhs)
{
    Number ret(lhs);
    ret += rhs;
    return ret;
}

This way it is efficient to call x += y; instead of x = x + y;

淡笑忘祈一世凡恋 2024-08-30 11:39:53

就我个人而言,我更喜欢在类中声明运算符:

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 operator+(const Number &arg) const;
};

然后:

Number Number::operator+(const Number &arg)
{
  ...
}

Personally I like it better to declare the operators inside the class:

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 operator+(const Number &arg) const;
};

and then:

Number Number::operator+(const Number &arg)
{
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文