//shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include "math.h"
const double pi = 3.14159265358;
double perimeter_of_rectangle(double a,double b);
struct rectangle
{
double a, b;
};
double perimeter_of_rectangle(double a,double b)
{
return 2*(a+b);
};
#endif // SHAPE_H_INCLUDED
//main.cpp
#include
#include "shape.h"
int main()
{
using namespace std;
struct rectangle r =
{
2, 3
};
cout << "Perimeter of rectangle " << perimeter_of_rectangle(r.a, r.b) << endl;
return 0;
}
扩展资料
头文件一般由四部分内容组成:
(1)头文件开头处的版权和版本声明;
(2)预处理块;
(3)inline函数的定义;
(4)函数和类结构声明等。
在头文件中,用 ifndef/define/endif结构产生预处理块,用 #include 格式来引用库的头文件。
#ifndef GRAPHICS_H//作用:防止graphics.h被重复引用
#define GRAPHICS_H
#include<....>//引用标准库的头文件...
#include"..."//引用非标准库的头文件
...void Function1(...);//全局函数声明
...inline();//inline函数的定义
...classBox//作用:类结构声明
{...
};#endif
参考资料:百度百科 头文件
不知道你上面的代码是不是从编辑器拷出来的,如果是,那你的程序可就惨了。
我先说下我的操作过程。
1 new Win32 Console Application
2 new C/C++ Header File
所有的路径都按默认,只是写了文件名(shape.h )
没有 “3 新建C++ Source File c:\1104(保存路径) 1104(文件名)”,因为我刚始创建的不是一个空的工程,系统会自动给我生成一个 1104.cpp。
我想你的错误应该就在这里,你创建的应该是一个空工程。下面是当你创建空工程时的具体做法:
你在vc里要有一定要有一句#include “stdafx.h”(用vc的都知道)。
所以你就要在你工程的目录里 添加两个文件 一个是 stdafx.h,另一个是stdafx.cpp这样就好了,如果有必要你还要在你的工程里加入这两个文件。
在右边的FileView(文件视图)里的Source File 上右击 选择“添加文件到目录”在弹出的对话框里选stdafx.cpp,同样在Header File 上右击 选择“添加文件到目录”在弹出的对话框里选stdafx.h。OK
比较麻烦,所以一般不要创建空工程。
下面分析代码:
//***************
//* shape.h *
//***************
#include "math.h" //首先,没有头文件卫士 >?<
const double pi=3.14159265358;
struct cirle
{
double r;
};
struct square
{
double a;
};
struct rectangle
{
double a,b;
};
struct triangle
{
double a,b,c,alpha,beta,gamma;
};
double rerimeter_of_circle(double r)//是perimeter_of_circle 吧?!
{
return 2*pi*r;
};///////////////////////////// ErrorA.不明白函数体后面为什么要加分号(;)
double area_of_circle(double r)
{
return pi*r*r;
}; ////////////////////////////////////同ErrorA
double perimer_of_square(double a)//看函数名拼写正确吗?是不是 perimeter_of_tsquare??
{
return 4*a;
}; ////////////////////////////////////同ErrorA
double area_of_square(double a)
{
return a*a;
}; ////////////////////////////////////同ErrorA
double perimeter_of_rectangle(double a,double b)
{
return 2*(a+b);
}; ////////////////////////////////////同ErrorA
double area_of_rectangle(double a,double b)
{
return a*b;
}; ////////////////////////////////////同ErrorA
double perimeter_of_triangle(double a,double b,double c)
{
return a+b+c;
}; ////////////////////////////////////同ErrorA
double area_of_triangle(double a,double b,double gamma)
{
return sin(gamma/180*pi)*a*b/2;
}; ////////////////////////////////////同ErrorA
下面是主函数:
//*************
//* main.cpp *
//*************
#include "iostream.h" //是否忘了#include “stdafx.h”
#include "shape.h"
int main()
{
circle c={2};
square s={1};
rectangle r={2,3};
triangle t={3,4,5,36.86989,53.13011,90};
cout <<"Perimeter of circle " <
}
按照提示改正后应该就可以正常运行了。
总提示找不到头文件(ndk-build error: string: No such file or directory) :
首先检查有没有Application.mk文件,如果没有在所在目录下面新建个Application.mk文件,在里面添加APP_STL := gnustl_static就可以找到标准库了。
//shape.h
#ifndef
SHAPE_H_INCLUDED
#define
SHAPE_H_INCLUDED
#include
"math.h"
const
double
pi
=
3.14159265358;
double
perimeter_of_rectangle(double
a,double
b);
struct
rectangle
{
double
a,
b;
};
double
perimeter_of_rectangle(double
a,double
b)
{
return
2*(a+b);
};
#endif
//
SHAPE_H_INCLUDED
//main.cpp
#include
#include
"shape.h"
int
main()
{
using
namespace
std;
struct
rectangle
r
=
{
2,
3
};
cout
<<
"Perimeter
of
rectangle
"
<<
perimeter_of_rectangle(r.a,
r.b)
<<
endl;
return
0;
}
扩展资料
头文件一般由四部分内容组成:
(1)头文件开头处的版权和版本声明;
(2)预处理块;
(3)inline函数的定义;
(4)函数和类结构声明等。
在头文件中,用
ifndef/define/endif结构产生预处理块,用
#include
格式来引用库的头文件。
#ifndef GRAPHICS_H//作用:防止graphics.h被重复引用
#define GRAPHICS_H
#include<....>//引用标准库的头文件...
#include"..."//引用非标准库的头文件
...void Function1(...);//全局函数声明
...inline();//inline函数的定义
...classBox//作用:类结构声明
{...
};#endif
参考资料:百度百科
头文件
函数不要在头文件中定义,而且你的函数都加了;符号,在源文件中对结构变量(结构体或类)中的函数实现一般定义在main()函数之外。