简体   繁体   English

C ++在重载函数中使用“基本”函数

[英]C++ using a “base” function inside an overload function

while coding I came across a problem. 在编码时遇到了一个问题。

I use projective 2D-Space (ie 2D-points (x,y) and 3D-Points (x,y,w) ). 我使用投影2D空间(即2D点(x,y)和3D点(x,y,w))。 I also have a function taking in a projectiv point (3D). 我也有接受投影点(3D)的功能。 For better reading and to avoid casting in the main I tried to write an overload for that function taking in the 2D-Point and calling the "base" function (simple example). 为了更好地阅读并避免强制转换,我尝试为该函数编写一个重载,将其放入2D点并调用“基本”函数(简单示例)。 But the compiler does not compiles the code. 但是编译器不会编译代码。 There seems to be a link error (LNK 2001). 似乎存在链接错误(LNK 2001)。

//"base" function
foo(3D_pt pt)
{
      //do something with it
}

//overloaded function
foo(2D_pt pt)
     3D_pt pt3 = 3D_pt(pt.x, pt.y, 1);
     foo(pt3);
}

Any ideas how to go about that? 任何想法如何去做?

Thanks 谢谢

edit: The error is: LNK2001: unresolved symbol - foo(2D_pt pt) 编辑:错误是:LNK2001:无法解析的符号-foo(2D_pt pt)

The only problem in the code posted are: 发布的代码中的唯一问题是:

#1: Your class names ( 2D_pt and 3D_pt ) are starting with a number while an identifier in C++ should not do this ( Cplusplus.com ). #1:您的类名( 2D_pt3D_pt )以数字开头,而C ++中的标识符则不应这样做( Cplusplus.com )。

#2: The functions foo have no return type. #2:函数foo没有返回类型。

#3: { is missing in foo(2D_pt) . #3: {foo(2D_pt)丢失。

After fixing, everything builds with no issues: ideone . 修复后,一切都构建成功: ideone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM