找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1300|回复: 9

[讨论]有问题讨论

[复制链接]
发表于 2009-11-2 02:42:01 | 显示全部楼层 |阅读模式 IP:江苏扬州
我这个问题讨论想要发很久了,苦于老是发不上来所以一直没能发
源代码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的类向导使我当前的类能够使用这个类
发表于 2009-11-2 02:42:02 | 显示全部楼层 IP:江苏扬州
mfc的事俺不知道
回复

使用道具 举报

发表于 2009-11-2 02:42:04 | 显示全部楼层 IP:江苏扬州
http://www.vckbase.com/document/viewdoc/?id=683

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

使用道具 举报

发表于 2009-11-2 02:42:06 | 显示全部楼层 IP:江苏扬州
抱歉,什么叫类向导,是class wizard吗?
回复

使用道具 举报

发表于 2009-11-2 02:42:08 | 显示全部楼层 IP:江苏扬州
我又试了一下,你可以定义一个头文件 和一个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;
}
回复

使用道具 举报

发表于 2009-11-2 02:42:09 | 显示全部楼层 IP:江苏扬州
这么说的话是不是说得类的定义与实现是不是一定要放在一个文件里呢?
那我的问题就是如何创建自己的编译库 mentioned by knocker;
那么那种iostream.h是怎么实现的呢?
有这么一个问题,我们的老师一直提倡我们将定义与实现分开最后居然办不到,我想这不太对
我想知道为什么?
回复

使用道具 举报

发表于 2009-11-2 02:42:10 | 显示全部楼层 IP:江苏扬州
zinking,

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

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

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

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

使用道具 举报

发表于 2009-11-2 02:42:13 | 显示全部楼层 IP:江苏扬州
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 函数了。 这和我们做的是一样的。
回复

使用道具 举报

发表于 2009-11-2 02:42:16 | 显示全部楼层 IP:江苏扬州
再说明一下,如果你的class 没有用到Template,那么提倡 将class 的 申明写在一个头文件中, 将其具体定义写在一个cpp 文件中,然后将具体的执行程序,也就是那个main() 写在一个单独的cpp 文件中。
但是如果你的某个class 涉及到template,
那么你可以为这个class 单独写一个头文件,并且将其具体定义也写在这个头文件中, 任何需要用到该class 的文件,都必须 include 这个头文件。

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

使用道具 举报

发表于 2009-11-2 02:42:18 | 显示全部楼层 IP:江苏扬州
thanks to kai
for detailed instructions
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 23:34 , Processed in 0.184441 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表