在vs2010中C++包含文件的使用
我建立了三个文件:1.headerfile:#include<iostream>usingnamespacestd;classComplex{public:friendo...
我建立了三个文件:
1.
headerfile:
#include<iostream>
using namespace std;
class Complex
{
public:
friend ostream& operator<<(ostream& output,Complex& c1);
friend istream& operator>>(istream& input,Complex& c1);
friend Complex operator+(Complex &c2,Complex &c1);
private:
int real;
int imag;
};
2.
function.cpp:
#include<iostream>
using namespace std;
#include"headerfile.h"
Complex operator+(Complex &c2,Complex &c1)
{
Complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return c3;
}
ostream& operator<<(ostream& output,Complex& c1)
{
cout<<"("<<c1.real<<","<<c1.imag<<")"<<endl;
return output;
}
istream& operator>>(istream& input,Complex& c1)
{
cin>>c1.real>>c1.imag;
return input;
}
3.
main():
#include<iostream>
using namespace std;
#include"headerfile.h"
#include"function.cpp"
int main()
{
Complex c1,c2,c3;
cin>>c1;
cin>>c2;
c3=c1+c2;
cout<<c3;
return 0;
}
但是编译出现了一大堆错误,把这三个文件合在一起就可以运行,为什么这样不行啊我是完全按照书上的标准写的啊 展开
1.
headerfile:
#include<iostream>
using namespace std;
class Complex
{
public:
friend ostream& operator<<(ostream& output,Complex& c1);
friend istream& operator>>(istream& input,Complex& c1);
friend Complex operator+(Complex &c2,Complex &c1);
private:
int real;
int imag;
};
2.
function.cpp:
#include<iostream>
using namespace std;
#include"headerfile.h"
Complex operator+(Complex &c2,Complex &c1)
{
Complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return c3;
}
ostream& operator<<(ostream& output,Complex& c1)
{
cout<<"("<<c1.real<<","<<c1.imag<<")"<<endl;
return output;
}
istream& operator>>(istream& input,Complex& c1)
{
cin>>c1.real>>c1.imag;
return input;
}
3.
main():
#include<iostream>
using namespace std;
#include"headerfile.h"
#include"function.cpp"
int main()
{
Complex c1,c2,c3;
cin>>c1;
cin>>c2;
c3=c1+c2;
cout<<c3;
return 0;
}
但是编译出现了一大堆错误,把这三个文件合在一起就可以运行,为什么这样不行啊我是完全按照书上的标准写的啊 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询