C++ C++;联动误差。我做错了什么?
这是我第一次把一个程序分成一个头文件和两个.cpp文件。但我认为我得到了一个链接错误。下面是目录的外观。 (这是我的图片链接,我没有足够的代表在问题中发布图片) main.cpp是我的主要源文件,其中包含所有调用函数和其他重要内容。在functions.cpp中我有我的所有函数,在coordin.h文件中我有函数原型、结构和常量。一切都很好没有打字错误什么都没有我已经检查过了。但我得到了一个未定义的函数错误引用。 我还包括了coordin.h文件。 您认为functions.cpp文件需要转到其他地方吗?我的意思是编译器没有查看该文件吗 代码如下: main.cpp 函数.cppC++ C++;联动误差。我做错了什么?,c++,function,header,C++,Function,Header,这是我第一次把一个程序分成一个头文件和两个.cpp文件。但我认为我得到了一个链接错误。下面是目录的外观。 (这是我的图片链接,我没有足够的代表在问题中发布图片) main.cpp是我的主要源文件,其中包含所有调用函数和其他重要内容。在functions.cpp中我有我的所有函数,在coordin.h文件中我有函数原型、结构和常量。一切都很好没有打字错误什么都没有我已经检查过了。但我得到了一个未定义的函数错误引用。 我还包括了coordin.h文件。 您认为functions.cpp文件需要转到
/*
函数.cpp
包含所有函数声明
*/
#包括
#包括“协调人h”
#包括
使用名称空间std;
//将直角坐标转换为极坐标
极性矩形到极性(矩形xypos){
极性回答;
答案:距离=
sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
answer.angle=atan2(xypos.y,xypos.x);
返回答案;
}
//显示极坐标,将角度转换为度
void show_polar(polar dapos){
coutCode::Blocks是否确实编译了函数.cpp
文件?右键单击项目树中的函数.cpp
,选择“属性”,并确保复选框“构建”、“发布”设置-否则,Code::Block将在生成过程中忽略此文件,并且不会编译它。您在何处包含coordin.h?错误到底是什么?您可以向我们展示您的代码吗?您可能有一个函数,您在coordin.h
中提供了原型,但在functions.cpp
中没有实现。请确保在functions.cpp
中实现了您得到错误的确切函数(具有相同的参数和所有参数)。也许这个答案会有所帮助:似曾相识…以前见过这个,但我找不到问题
#include <iostream>
#include "coordin.h"
using namespace std;
int main()
{
rect rplace ;
polar pplace;
rect test = {45.89,25.4};
pplace = rect_to_polar(test);
cout<<pplace.angle<<endl;
return 0;
}
#ifndef COORDIN_H_INCLUDED
#define COORDIN_H_INCLUDED
/* Constants */
const double RAD_TO_DEG = 57.29577951;
struct polar{
double distance; //distance from the origin
double angle; //the angle from the origin
};
struct rect {
double x; //horizontal distance form the origin
double y; //vertical distance from the origin
};
/* Function prototypes */
polar rect_to_polar(rect xypos);
void show_polar(polar dapos);
#endif // COORDIN_H_INCLUDED
/*
functions.cpp
contains all the function declarations
*/
#include <iostream>
#include "coordin.h"
#include <cmath>
using namespace std;
//convert rectangular to polar coordinates
polar rect_to_polar(rect xypos){
polar answer;
answer.distance =
sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}
//show polar coordinate, converting angle to degrees
void show_polar(polar dapos){
cout<<"Distance : " << dapos.distance <<endl;
cout<<"Angle : " << dapos.angle * RAD_TO_DEG <<" degrees."<<endl;
}