const使用与否决定了程序能否运行,提示undefined reference to `show_polar(polar const*)'

再学200年 2014-06-04 09:46:42
/*
函数和结构的关系
*/
#include<iostream>
#include<cmath>
using namespace std;

struct polar
{
double distance;
double angle;
} ;

struct rect
{
double x;
double y;
};

void rect_to_polar(const rect * xypos, polar * pda);//把结果直接放到pda这个指针指向的结构中
void show_polar(const polar * dapos);//加const是因为不想修改这个结构

int main()
{
rect rplace;
polar pplace;
cout << "enter the x and y values: ";

while(cin >> rplace.x >> rplace.y)
{
//结构的地址不像数组,结构的地址是需要&地址操作符获得的
rect_to_polar(&rplace, &pplace);
show_polar(&pplace);
cout << "next ot numbers(q to quit): ";//其实任意一个非数字的字符都可以退出
}
cout << "done" << endl;


return 0;
}

//因为把结果直接放到了指针pda指向的内存中,所以不用返回polar结构了
void rect_to_polar(const rect * xypos, polar * pda)
{
pda->distance = sqrt(xypos->x * xypos->x + xypos->y * xypos->y);
pda-> angle = atan2(xypos->y, xypos->x);
}

void show_polar(polar * dapos)
{
const double Rad_to_deg = 57.29577951;
cout << "distance = " << dapos->distance;
cout << ", angle = " << dapos->angle * Rad_to_deg;
cout << " degress" << endl;
}

这一个程序我准备是在一本书上看到的,我运行的时候,如果void show_polar(const polar * dapos);这一条语句不加const的话可以正常运行,但是如果加了const的话,就提示undefined reference to `show_polar(polar const*)',应该是提示我没有使用引用,请教为什么会出现这样子的情况呢?
...全文
223 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
再学200年 2014-06-04
  • 打赏
  • 举报
回复
引用 2 楼 u010161166 的回复:
谢谢,就是在函数原型那里参数用了const,而在实现的时候没有写上cosnt,我已改正。
是const,刚才打错了。
再学200年 2014-06-04
  • 打赏
  • 举报
回复
谢谢,就是在函数原型那里参数用了const,而在实现的时候没有写上cosnt,我已改正。
罗博士 2014-06-04
  • 打赏
  • 举报
回复
因为你后面的函数实现,参数是没有const的。 所以如果在前面声明时加上const,编译器就认为你只做了声明而没有做实现。 你把前后都加上const,应该就能编译通过。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧