代码: 全选
/*我用operator << 的时候总是说Multiple markers at this line
- first defined here
- multiple definition of `operator<<(std::ostream&, Data&)'
在eclipse下写的程序.
具体代码是:
*/
//*****************************************************************************
//Data.cpp
#include<iostream>
using namespace std;
class Data
{
public:
friend ostream& operator << (ostream&,Data&);
Data()
{
num = 0;
den = 0;
}
Data(int num,int den)
{
this->num = num;
this->den = den;
}
private:
int num,den;
};
ostream& operator << (ostream& out,Data& data)
{
out<<data.num<<"/"<<data.den<<" ";
return out;
}
//*****************************************************************************
//main.cpp
#include "Data.cpp"
//写不写#include <iostream> using namespace std; 都一样报错
int main()
{
Data hello(1,3);
cout<<hello<<endl;
return 0;
}
/*
而如果把main函数和Data类放在一起又没问题拉 谁能告诉我这是怎么一回事啊,我的程序不能把main和Data类放在一起啊.急啊.
*/