2011-04-30 146 views
1

阵列我在做一个训练演习时遇到它起步的麻烦。我必须编写一个数组数据结构的程序。提供了一个测试工具,我需要实现数据结构的代码。我不知道如何在头文件中定义的数组C++中头文件

波纹管是测试工具的main.cpp,我不能在头文件编辑

#include <fstream> 
#include <iostream> 
using namespace std; 


// ***************************** 
// you need to create this class 
// ***************************** 
#include "ArrayIntStorage.h" 


int main(int argc, char **argv) { 
// *********************************** 
// non-sort read & then sort using std 
// *********************************** 
ifstream fin1("ACW2_data.txt"); 
ofstream out1("1-arrayUnsortedRead.txt"); 
//ofstream out2("2-arrayUnsortedRead-thenSTDSort.txt"); 

if(!fin1.is_open()) 
{ 
    cout << "FAIL" << endl; 
    return 1; 
} 

ArrayIntStorage arrayStorage1; 
arrayStorage1.setReadSort(false); // do not read sort 

// read in int values into data structure 
fin1 >> arrayStorage1; 

// output int values in data structure to file 
out1 << arrayStorage1; 

// sort data structure using std 
arrayStorage1.sortStd(); 

// output int values in data structure to file 
out2 << arrayStorage1; 

fin1.close(); 
out1.close(); 
out2.close(); 

的任何信息,以及如何将它们与这个主要使用文件将不胜感激

的Merci beaucoup

+1

这是不现实的练习。应该提供'ArrayIntStorage'类的头文件和文档。我会大声和粗暴地抱怨,对任何懒惰,傲慢的灵魂设定这个练习。 – 2011-04-30 13:21:57

回答

2

我敢肯定,你应该创建ArrayIntStorage.h头和落实配套ArrayIntStorage.cpp。

基于“使用std //排序数据结构”发表评论,你预计将使用和在适当的STL容器创建一个包装,类似的std ::向量。

基于“//不读取排序”注释,默认情况下,应该在每次插入之后对向量进行排序(当然,除非有人在包装上调用setReadSort(false))。

除了上述接口,您仍然需要实现>>和< <。

更新。

读你C++ pass variable from .cpp to header file质疑你似乎是由这一切很困惑......

第一件事,第一,增加了对支持>>和< <运营商:

通过声明这些运营商做到这一点在您的.h文件中:

friend std::ostream& operator<<(std::ostream &out, const ArrayIntStorage &a); 

friend std::ifstream & operator>>(std::ifstream &, ArrayIntStorage &); 

即可定义其在.cpp文件执行:

std::ostream& operator<<(std::ostream &out, const ArrayIntStorage &a) 
{ return out; } 

std::ifstream & operator>>(std::ifstream &, ArrayIntStorage &) 
{ return in; } 

显然,你需要添加一些正确的代码存在,这只是让它编译。 如果仍然无法编译,请检查您是否已经包含在您的.h文件中的流头:

#include <fstream> 
#include <iostream> 

现在对于一些基本信息:

你的阵列存储,应根据像STD ::向量。的你需要实现的目的>>和< <功能是从容器中添加和检索INT的。

由于ArrayIntStorage是一个类,一旦建立了所需的接口(.h文件中的公共成员函数),您应该只查看.h和.cpp以充实实现。

一旦完成,你不需要任何“extern”疯狂的回答你的其他问题说。 看看你的主要功能。如果创建你的类的对象和fin1流。然后它调用你已经实现的>>操作符。所有这些都是用局部变量完成的。

这就是你如何“使用main.cpp中的这个变量的值”。你用这个变量作为参数调用你的类的成员函数。

最后,如果您在理解头文件和链接错误时遇到了所有这些问题,您确定已经开始进行适当的培训练习吗?

+0

我知道我必须创建头文件,但是我还没有听到有关创建匹配cpp文件的任何信息。至于你的最后一点,当我制作关于运营商的头文件时,我会收到错误。我试图用这行'std :: ostream&operator <<(ostream&out,const ArrayIntStorage&a)'和类似的一行来绕过它。这些给我“LNK”链接器错误在我的头文件 – Marc 2011-04-30 14:10:29

+0

谢谢!我现在已经做了一些其他的练习并且更多地理解它。 – Marc 2011-04-30 20:49:37

2

头文件ArrayIntStorage应该有如下: -

功能西尼亚TURE可根据使用情况改变: -

// ArrayIntStorage.h - header file 

class ArrayIntStorage 
{ 

    public : 

    ArrayIntStorage(); // this is the constructor 
         // You may skip this constructor, and use the default one 

    void setReadSort(bool bSort); 
    void sortStd() 

    // Add any public members if needed 

    private : 

    // Add any private members/ functions if needed 

} 
+0

好吧,我之前有过类似的东西,但我无法让它工作。即时通讯非常确定,现在我必须使用默认的构造函数,尽管有其他人后来我必须创建自己的构造函数。我之前没有在C++中编程太多,我只是不明白头文件是什么。什么可以和我不能做一个头文件?别人说我需要一个匹配的cpp文件 – Marc 2011-04-30 14:12:02

+0

不要忘记:除了上面介绍的接口外,还需要实现>>和<<。 IBM的[关于重载操作符的文章(仅限C++)](http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr318 .htm)对我来说看起来很不错。 http://www.cplusplus.com是非常好的(高质量)资源...并且他们有一套体面的(即使不完整的)教程。 – corlettk 2011-04-30 14:12:34

1

马克,

http://www.cplusplus.com/doc/tutorial/(我在我的评论中提到)已经让我们失望。他们并没有将头文件作为一个主题在它自己的权利...然而http://www.learncpp.com/cpp-tutorial/19-header-files/呢。

基本上,一个头文件定义了你的类的“接口”。它被放在一个单独的文件中,以便您的类的用户可以在该代码中包含JUST这个“接口定义”......例如#include "ArrayIntStorage.h"。这允许编译器确定它们是否有错误,比如拼写错误的方法名称,或者传递错误类型的参数......你知道,你看过的所有那些编译器错误......是啊?

所以你定义了你的类在.h文件中做了什么......然后在.cpp文件中定义了它是如何实现的。

这有道理吗?

干杯。基思。

+0

非常有帮助,谢谢。我没有意识到头文件只是被复制到你包含它的地方 – Marc 2011-04-30 15:47:01