编译问题,大家帮帮忙。

lingzantia 2005-05-11 11:48:25
编译出现了3个错误,都是fatal error C1001: INTERNAL COMPILER ERROR,且都指向three_d.h 的
friend three_d operator+(const three_d &obj_1, const three_d &obj_2);
该如何解决,赐教!

下面是我的代码,分为3个.cpp和2个.h文件:

//main.cpp
#include <iostream>
using namespace std;

#include "test.h"
#include "three_d.h"

void main(void)
{
test menu;
}



//three_d.h
#ifndef THREE_D_H
#define THREE_D_H

class three_d
{
private:
int x, y, z;
void init();
public:
three_d();
three_d(int a,int b,int c);
~three_d();
friend three_d operator+(const three_d &obj_1, const three_d &obj_2);
friend three_d operator--(three_d &obj, int);
friend ostream &operator<<(ostream &out, const three_d &obj);
friend istream &operator>>(istream &in, three_d &obj);.
three_d operator++();
three_d operator-=(const three_d &obj);
};

#endif


//three_d.cpp
#include <iostream>
using namespace std;
#include "three_d.h"

void three_d::init()
{
cout << "Input the three_d : ";
cin >> x >> y >> z;
}

three_d::three_d()
{
init();
}

three_d::three_d(int a,int b,int c) : x(a), y(b), z(c)
{}

three_d::~three_d(){}

three_d three_d::operator ++()
{
++x;
++y;
++z;
return *this;
}

three_d three_d::operator -=(const three_d &obj)
{
x -= obj.x;
y -= obj.y;
z -= obj.z;
return *this;
}

three_d operator+(const three_d &obj_1, const three_d &obj_2)
{
return three_d(obj_1.x + obj_2.x, obj_1.y + obj_2.y, obj_1.z + obj_2.z);
}

three_d operator--(three_d &obj, int)
{
x--;
y--;
z--;
return *this;
}

ostream &operator<<(ostream &out, const three_d &obj)
{
out << "The three_d is : ";
out << odj.x;
out << ", ";
out << obj.y;
out << ", ";
out << obj.z;
out << endl;

return out;
}

istream &operator>>(istream &in, three_d &obj)
{
in >> obj.x;
in >> obj.y;
in >> obj.z;

return in;
}


//test.h
#ifndef TEST_H
#define TEST_H

class test
{
public:
test();
~test();
void menu();
};

#endif


//test.cpp
#include <iostream>
using namespace std;
#include "test.h"
#include "three_d.h"

test::test(){}

test::~test(){}

void test::menu()
{
char ch;

cout << "选项\n";
cout << "================================================\n";
cout << "1、两对象求和 2、对象对自身求差\n";
cout << "3、前自增 4、后自减\n";
cout << "5、退出\n";
cout << "================================================\n\n";

do
{
cout << "选择 : ";
cin >> ch;

switch(ch)
{
case '1' : three_d obj_1, obj_2;
cout << obj_1 + obj_2 << endl;
break;
case '2' : three_d obj_1, obj_2;
cout << obj_1 -= obj_2 << endl;
break;
case '3' : three_d obj;
cout << ++obj << endl;
break;
case '4' : three_d obj;
cout << obj-- << endl;
break;
case '5' : return;
default : cout << "错误的选择!\n";
}

cout << "继续操作?[是(Y or y) / 不(any other key)]";
}while ( (cin.get() == 'Y') || (cin.get() == 'y') );
}
...全文
102 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
eric8231 2005-05-12
  • 打赏
  • 举报
回复
这个定义也不对呀:
three_d operator--(three_d &obj, int)
{
x--;
y--;
z--;
return *this;
}

operator--()不是成员函数,怎么能返回*this呢? 是笔误吗?
zhangfjj 2005-05-12
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3588/3588388.xml?temp=6.501406E-02
zhangfjj 2005-05-12
  • 打赏
  • 举报
回复
老问题了,据说是VC6编译器的问题,你可以试一下其他编译器,如dev-c++,或者在类的声明前加
//three_d.h
#ifndef THREE_D_H
#define THREE_D_H
class three_d;//加前置声明
//再对友元重载的运算符函数进行声明,应该可以了,你可以查一查以前坛子里的回复
three_d operator+(const three_d &obj_1, const three_d &obj_2);
three_d operator--(three_d &obj, int);
ostream &operator<<(ostream &out, const three_d &obj);
istream &operator>>(istream &in, three_d &obj);.

class three_d
{
private:
int x, y, z;
void init();
public:
three_d();
three_d(int a,int b,int c);
~three_d();
friend three_d operator+(const three_d &obj_1, const three_d &obj_2);
friend three_d operator--(three_d &obj, int);
friend ostream &operator<<(ostream &out, const three_d &obj);
friend istream &operator>>(istream &in, three_d &obj);.
three_d operator++();
three_d operator-=(const three_d &obj);
};
lingzantia 2005-05-12
  • 打赏
  • 举报
回复
to zhangfjj(小张(正在学习Linux。。。))
我的类的声明和定义是分开放的,该如何处理?
lingzantia 2005-05-12
  • 打赏
  • 举报
回复
to eric8231(1328cire)
受教!
lingzantia 2005-05-12
  • 打赏
  • 举报
回复
加了
class three_d;//加前置声明
//再对友元重载的运算符函数进行声明
又来了一大堆的错误,
换CB6.0试了下,也是一堆,郁闷了

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧