程序出错,帮忙看下

来源:百度知道 编辑:UC知道 时间:2024/05/19 17:19:53
首先是写一个Power_Index.h

class Power_Index
{
public:
float Power_Index(float x, int y)//幂指数函数,输入基数x,指数y返回值
{
if (y>0)
{
result = 1;
for (i=1;i<=y;++i)
{
result = result * x;
}
}
else if (y=0)
{
result = 1;
}
else
{
x = 1 / x;
result = 1;
for (i=-1;y<=i;--i)
{
result = result * x;
}
}

return result;
}

protected:
int i;
float result;
}

然后是写一个主程序main.cpp

#include <cstdlib>
#include <iostream>
#include <Power_Index.h>

us

首先,自己写的头文件用#include "Power_Index.h"
其次,头文件中float Power_Index(float x, int y)//幂指数函数,输入基数x,指数y返回值
函数名与类名相同,编译器会认为是构造函数,而构造函数是没有返回值的,所以把函数名改一下。
还有class{}最后少了;号
else if(y=0)改为(y==0)

#include <Power_Index.h> 改成
#include "Power_Index.h" 试试

程序里的:"if(y=0)"如果是判断y值的话,应写成:if(y==0)

#include<Power_Index.h:>有错
改为#include"Power_Index.h",他们的区别在于用对角号会到VC98下的include目录下去找,双引号则会到你的程序工作区间下去找,你用<>,那么就会到include目录下找,而该头文件是你自己定义的,include下包含的是系统自带的,所以就找不到该文件。
当然另外一种解决方法是吧你的Power_Index.h复制到vc的安装目录 vc98-》
include 目录下,照样用<>.
主要问题是你没把握他们之间的区别。
希望可以帮到你。