| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1101 人关注过本帖
标题:[讨论]有问题讨论
只看楼主 加入收藏
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
 问题点数:0 回复次数:10 
[讨论]有问题讨论

我这个问题讨论想要发很久了,苦于老是发不上来所以一直没能发
源代码kai已经修改过的没有什么问题
但是请看清我的问题
#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
class MyComplex
{
private:
T real;
T image;
public:
MyComplex()
{
real = 0;
image = 0;
}
MyComplex(T r,T i)
{
real=r;
image=i;
}
T getReal(){return real;}
T getImage(){return image;}
friend MyComplex<T> operator+(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = a.real + b.real;
T i = a.image + b.image;
MyComplex<T> temp(r, i);
return temp;
}
friend MyComplex<T> operator-(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = a.real - b.real;
T i = a.image - b.image;
MyComplex<T> temp(r, i);
return temp;
}
friend MyComplex<T> operator*(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = a.real*b.real-a.image*b.image;
T i = a.real*b.image+a.image*b.real;
MyComplex<T> temp(r, i);
return temp;
}
friend MyComplex<T> operator/(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = (a.real*b.real+a.image*b.image)/(b.real*b.real+b.image*b.image);
T i = (b.real*a.image-a.real*b.image)/(b.real*b.real+b.image*b.image);
MyComplex<T> temp(r, i);
return temp;
}
friend ostream & operator<<(ostream & output, const MyComplex<T> & c)
{
if(c.image > 0)
output<<c.real<<"+"<<c.image<<"i";
else if(c.image == 0)
output<<c.real;
else if(c.image < 0)
output<<c.image<<c.image<<"i";

return output;
}
};
int main()
{
MyComplex <double> a(12.3, -12.5);
MyComplex <double> b(1.0, 2.0);
MyComplex <double> c1 = a + b;
MyComplex <double> c2 = a - b;
MyComplex <double> c3 = a * b;
MyComplex <double> c4 = a / b;

cout<<a<<endl;
cout<<b<<endl;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout<<c4<<endl;
system("pause");
return 0;
}

我的问题是
1。要将其分为三个部分分别实现。头文件,类的实现,测试
也就是complex.h complex.cpp main.cpp
知道我的意思吧
2。就是 如何将其加入一个mfc的类向导使我当前的类能够使用这个类

搜索更多相关主题的帖子: 源代码 MyComplex real image include 
2006-03-05 09:29
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 

mfc的事俺不知道


九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-03-05 16:15
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 

http://www.vckbase.com/document/viewdoc/?id=683

看看这个能不能帮你,或者你直接去google


九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-03-05 16:23
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
抱歉,什么叫类向导,是class wizard吗?

http://myajax95./
2006-03-05 16:26
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
我又试了一下,你可以定义一个头文件 和一个Main, 但是那个头文件的实现Cpp 确是不行的。下面是代码,请注意,该代码可以在DEV中通过编译和运行,但是在VC中不能通过编译,如果要在VC中通过编译请注意代码中的注释:

1)头文件 MyComplex.h

#ifndef MYCOMPLEX_H_
#define MYCOMPLEX_H_
#include <iostream> // 如果需要在VC中通过编译请将其改为 #include <iostream.h>
using namespace std; // 并且将这一行省去

template<class T>
class MyComplex
{
private:
T real;
T image;
public:
MyComplex()
{
real = 0;
image = 0;
}
MyComplex(T r,T i)
{
real=r;
image=i;
}
T getReal(){return real;}
T getImage(){return image;}
friend MyComplex<T> operator+(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = a.real + b.real;
T i = a.image + b.image;
MyComplex<T> temp(r, i);
return temp;
}
friend MyComplex<T> operator-(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = a.real - b.real;
T i = a.image - b.image;
MyComplex<T> temp(r, i);
return temp;
}
friend MyComplex<T> operator*(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = a.real*b.real-a.image*b.image;
T i = a.real*b.image+a.image*b.real;
MyComplex<T> temp(r, i);
return temp;
}
friend MyComplex<T> operator/(const MyComplex<T> & a, const MyComplex<T> & b)
{
T r = (a.real*b.real+a.image*b.image)/(b.real*b.real+b.image*b.image);
T i = (b.real*a.image-a.real*b.image)/(b.real*b.real+b.image*b.image);
MyComplex<T> temp(r, i);
return temp;
}
friend ostream & operator<<(ostream & output, const MyComplex<T> & c)
{
if(c.image > 0)
output<<c.real<<\"+\"<<c.image<<\"i\";
else if(c.image == 0)
output<<c.real;
else if(c.image < 0)
output<<c.image<<c.image<<\"i\";

return output;
}
};
#endif


2)Main.cpp

#include <cstdlib>
#include \"MyComplex.h\"

int main()
{
MyComplex <double> a(12.3, -12.5);
MyComplex <double> b(1.0, 2.0);
MyComplex <double> c1 = a + b;
MyComplex <double> c2 = a - b;
MyComplex <double> c3 = a * b;
MyComplex <double> c4 = a / b;

cout<<a<<endl;
cout<<b<<endl;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout<<c4<<endl;

system(\"pause\");
return 0;
}



自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-05 18:18
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 

这么说的话是不是说得类的定义与实现是不是一定要放在一个文件里呢?
那我的问题就是如何创建自己的编译库 mentioned by knocker;
那么那种iostream.h是怎么实现的呢?
有这么一个问题,我们的老师一直提倡我们将定义与实现分开最后居然办不到,我想这不太对
我想知道为什么?


http://kongfuziandlife. http://codeanddesign.
2006-03-05 19:33
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
zinking,

因为你的类中用到了Template,所以函数的定义必须写在header file 中,没有别的办法。

你的老师说的也是对的,将申明放在头文件中,将定义放在独立的cpp 文件中, 这样做的目的是为了分别编译,对检查错误有很好的帮助。

据我目前所知就这些,你可以统统写在一个cpp 文件中(那个main 也包括其中),但是这样做不太好, 还有就是将定义写在一个头文件中, 在那些用到该头文件的cpp 文件中 include 就可以了。

我这里所指的定义就是你所说的定义实现,其实一般来讲,我们用申明这个概念来表示函数的Prototype, 用定义这个概念来表示 具体化的函数,也就是你所指的函数的实现。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-05 20:23
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
zinking,
如果你感兴趣,你可以看看Dev的那些头文件.
我们来看看 iostream.h 这个头文件, 在我的电脑里它放在这个目录下, 如果你是默认安装的话,也应该在这个目录下.

C:\Dev-Cpp\include\c++\3.4.2\backward


程序代码:

用写字板打开, 内容如下:
// Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

#ifndef _BACKWARD_IOSTREAM_H
#define _BACKWARD_IOSTREAM_H 1

#include \"backward_warning.h\"
#include <iostream> // 请注意这里, 这个文件放在哪里了呢?
// 它在 C:\Dev-Cpp\include\c++\3.4.2

using std::iostream;
using std::ostream;
using std::istream;
using std::ios;
using std::streambuf;

using std::cout;
using std::cin;
using std::cerr;
using std::clog;
#ifdef _GLIBCXX_USE_WCHAR_T
using std::wcout;
using std::wcin;
using std::wcerr;
using std::wclog;
#endif

using std::ws;
using std::endl;
using std::ends;
using std::flush;

#endif

// Local Variables:
// mode:C++
// End:



现在请你在目录 C:\Dev-Cpp\include\c++\3.4.2 下找到 iostream 这个文件, 用写字板打开它
内容如下:
程序代码:

// Standard iostream objects -*- C++ -*-

// Copyright (C) 1997, 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

//
// ISO C++ 14882: 27.3 Standard iostream objects
//

/** @file iostream
* This is a Standard C++ Library header. You should @c #include this header
* in your programs, rather than any of the \"st[dl]_*.h\" implementation files.
*/

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <ostream> //请注意这里, 让我们来看看 ostream 里面写了些什么
#include <istream> //请注意这里 让我们来看看 istream 里面写了些什么

namespace std
{
/**
* @name Standard Stream Objects
*
* The &lt;iostream&gt; header declares the eight <em>standard stream
* objects</em>. For other declarations, see
* http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#10 and the
* @link s27_2_iosfwd I/O forward declarations @endlink
*
* They are required by default to cooperate with the global C library's
* @c FILE streams, and to be available during program startup and
* termination. For more information, see the HOWTO linked to above.
*/
//@{
extern istream cin; ///< Linked to standard input
extern ostream cout; ///< Linked to standard output
extern ostream cerr; ///< Linked to standard error (unbuffered)
extern ostream clog; ///< Linked to standard error (buffered)

#ifdef _GLIBCXX_USE_WCHAR_T
extern wistream wcin; ///< Linked to standard input
extern wostream wcout; ///< Linked to standard output
extern wostream wcerr; ///< Linked to standard error (unbuffered)
extern wostream wclog; ///< Linked to standard error (buffered)
#endif
//@}

// For construction of filebuffers for cout, cin, cerr, clog et. al.
static ios_base::Init __ioinit;
} // namespace std

#endif /* _GLIBCXX_IOSTREAM */



同样的在目录 C:\Dev-Cpp\include\c++\3.4.2 下面你可以找到 istream 和 ostream 这两个文件,用写字板打开它们,你看到了,所有的定义都写在这两个文件里面,同样也用到了Template,而且那些函数被定义为inline 函数了。 这和我们做的是一样的。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-05 21:02
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
再说明一下,如果你的class 没有用到Template,那么提倡 将class 的 申明写在一个头文件中, 将其具体定义写在一个cpp 文件中,然后将具体的执行程序,也就是那个main() 写在一个单独的cpp 文件中。
但是如果你的某个class 涉及到template,
那么你可以为这个class 单独写一个头文件,并且将其具体定义也写在这个头文件中, 任何需要用到该class 的文件,都必须 include 这个头文件。

还有一种办法,那就是,如果这个class 只是在main 里面被使用,那么你也可以 将这个class 的申明和定义都写在那个包含main() 的cpp 文件中。不过不赞成采用这种办法。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-05 21:17
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
thanks to kai
for detailed instructions

http://kongfuziandlife. http://codeanddesign.
2006-03-06 10:47
快速回复:[讨论]有问题讨论
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.097937 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved