求教看一下程序,如何了。
求教看一下程序,怎么了。。。急急!!
//定义一个学生类,2个学生3门成绩,计算没人的总成绩,每一门平均分,定义协会类,当然继承学生类,输出2个成绩,
不过就是[我想头文件包含在主函数里,把类放在。h中,再把它包含在。cpp中]但是出现错误,,
_thiscall A::A(int,int,int)" (??0A@@QAE@HHH@Z) already defined in cv.obj
zy.obj : error LNK2005: "public: int __thiscall A::Total(void)" (?Total@A@@QAEHXZ) already defined in cv.obj
zy.obj : error LNK2005: "public: int __thiscall A::Average(void)" (?Average@A@@QAEHXZ) already defined in cv.obj
这是。cpp程序。
#include <iostream.h>
#include "cv.h"
void main()
{
B stu1(80,88,90,80,59);
B stu2(50,60,70,70,67);
cout << "第1个学生的总成绩和平均成绩是:" << endl;
stu1.print();
cout << "第2个学生的总成绩和平均成绩是:" << endl;
stu2.print();
}
这是。cv.h文件
#include <iostream.h>
const int m=3;
class A
{
public:
A(int s1,int s2,int s3);
int Total () ;
int Average ( );
void print();
private:
int a[m];
};
A::A(int s1,int s2,int s3)
{
a[0]=s1;
a[1]=s2;
a[2]=s3;
}
int A:: Total()
{
int sum=0;
for(int i=0; i<m; i++)
sum+=a[i];
return sum;
}
int A::Average()
{
int i,ave;
ave=0;
for( i=0; i<m; i++)
ave+=a[i];
return ave/m;
}
void A:: print()
{
int sum,ave;
sum=Total();
ave=Average();
cout << "总分:"<< sum << endl;
cout << "课程的平均成绩" << ave <<endl;
}
class B:public A
{
public:
B(int s1,int s2,int s3,int s4,int s5);
void print();
private:
int b,wx,lh;
};
B::B(int s1,int s2,int s3,int s4,int s5):A(s1,s2,s3),b(s4,s5) //调用基类的构造函数
{wx=s4;lh=s5;}
void B:: print()
{
A::print();
cout<<"协会成绩:"<<","<<wx<<","<<lh<<endl;
}
------解决方案--------------------
在头文件里添加
//定义一个学生类,2个学生3门成绩,计算没人的总成绩,每一门平均分,定义协会类,当然继承学生类,输出2个成绩,
不过就是[我想头文件包含在主函数里,把类放在。h中,再把它包含在。cpp中]但是出现错误,,
_thiscall A::A(int,int,int)" (??0A@@QAE@HHH@Z) already defined in cv.obj
zy.obj : error LNK2005: "public: int __thiscall A::Total(void)" (?Total@A@@QAEHXZ) already defined in cv.obj
zy.obj : error LNK2005: "public: int __thiscall A::Average(void)" (?Average@A@@QAEHXZ) already defined in cv.obj
这是。cpp程序。
#include <iostream.h>
#include "cv.h"
void main()
{
B stu1(80,88,90,80,59);
B stu2(50,60,70,70,67);
cout << "第1个学生的总成绩和平均成绩是:" << endl;
stu1.print();
cout << "第2个学生的总成绩和平均成绩是:" << endl;
stu2.print();
}
这是。cv.h文件
#include <iostream.h>
const int m=3;
class A
{
public:
A(int s1,int s2,int s3);
int Total () ;
int Average ( );
void print();
private:
int a[m];
};
A::A(int s1,int s2,int s3)
{
a[0]=s1;
a[1]=s2;
a[2]=s3;
}
int A:: Total()
{
int sum=0;
for(int i=0; i<m; i++)
sum+=a[i];
return sum;
}
int A::Average()
{
int i,ave;
ave=0;
for( i=0; i<m; i++)
ave+=a[i];
return ave/m;
}
void A:: print()
{
int sum,ave;
sum=Total();
ave=Average();
cout << "总分:"<< sum << endl;
cout << "课程的平均成绩" << ave <<endl;
}
class B:public A
{
public:
B(int s1,int s2,int s3,int s4,int s5);
void print();
private:
int b,wx,lh;
};
B::B(int s1,int s2,int s3,int s4,int s5):A(s1,s2,s3),b(s4,s5) //调用基类的构造函数
{wx=s4;lh=s5;}
void B:: print()
{
A::print();
cout<<"协会成绩:"<<","<<wx<<","<<lh<<endl;
}
------解决方案--------------------
在头文件里添加
- C/C++ code
#ifndef _XXXXXX____XXXXXX____XXX_X_X #define _XXXXXX____XXXXXX____XXX_X_X //把你的代码添加进来 #endif
------解决方案--------------------
按你的程序,在VC6.0上没错啊