C ++数组中的头文件 [英] C++ array in header file

查看:208
本文介绍了C ++数组中的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个训练演习和我有它起步的麻烦。我必须写一个数组的程序。所提供的测试工具,我需要实现code的数据结构。我不知道如何定义在头文件中的数组

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

 的#include<&的fstream GT;
#包括LT&;&iostream的GT;
使用命名空间std;
// ****
//你需要创建这个类
// ****
#包括ArrayIntStorage.h
INT主(INT ARGC,字符** argv的){
// ***********
//非分页阅读和放大器;然后排序使用std
// ***********
ifstream的FIN1(ACW2_data.txt);
的ofstream OUT1(1-arrayUnsortedRead.txt);
//的ofstream OUT2(2-arrayUnsortedRead-thenSTDSort.txt);如果(!fin1.is_open())
{
    COUT<< FAIL<< ENDL;
    返回1;
}ArrayIntStorage arrayStorage1;
arrayStorage1.setReadSort(假); //不读排序//读取INT值代入数据结构
FIN1>> arrayStorage1;//数据结构输出INT值到文件
OUT1<< arrayStorage1;//使用std排序的数据结构
arrayStorage1.sortStd();//数据结构输出INT值到文件
OUT2<< arrayStorage1;fin1.close();
out1.close();
out2.close();

在头文件的任何信息,以及如何使用它们这一主文件将是非常美联社preciated

留言Merci beaucoup


解决方案

我是pretty确保您应该创建ArrayIntStorage.h头和落实配套ArrayIntStorage.cpp。

基于//排序的数据结构使用std

发表评论你预计使用,并在适当的STL容器创建一个包装,类似的std ::向量。

根据对//没有那种写着评论,你应该在默认情况下,每次插入后进行排序向量(除非,当然,有人对您的包装要求setReadSort(假))。

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

更新。

阅读你 C ++从质疑到的.cpp头文件传递变量你似乎被这一切是相当迷茫......

第一件事,第一,增加支持>>和<其中p运营商:

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

 朋友的std :: ostream的&放大器;运营商的LT;≤(的std :: ostream的和放大器;出来,常量ArrayIntStorage和放大器;一个);朋友的std :: ifstream的&安培;运营商的GT;>(性病:: ifstream的&放大器;,&ArrayIntStorage放大器;);

您再明确自己在.cpp文件中实现:

 的std :: ostream的&放大器;运营商的LT;≤(的std :: ostream的和放大器;出来,常量ArrayIntStorage&放大器;一)
{返回的; }的std :: ifstream的&安培;运营商的GT;>(性病:: ifstream的&放大器;,&ArrayIntStorage放大器;)
{返回; }

显然,你需要添加一些适当的code那里,这只是让它编译。
如果仍然不能编译,请检查您是否已经包含在您的.h文件中的流头部:

 的#include<&的fstream GT;
#包括LT&;&iostream的GT;

现在一些基本信息:

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

由于ArrayIntStorage是一个类,一旦你建立了接口,你需要(在.h文件公众成员函数),你应该只看.H和.cpp充实的实现。

一旦做到这一点,你不需要任何的外部疯狂的答案,你的其他问题时说的。
看看你的主要功能。如果你创建的类的对象和FIN​​1流。然后,它调用操作符>>你已经实现。所有这一切都与局部变量完成。

这是你如何使用这个变量的值从的main.cpp。你叫你的类的成员函数与变量作为参数。<​​/ P>

最后,如果你把所有这些问题理解的头文件和链接错误,你确定你已经开始用正确的训练演习?

I am doing a training exercise and am having trouble getting started on it. I have to write a program for an array data structure. There is a test harness provided and I need to implement the code for the data structure. I am not sure how to define an array in a header file

bellow is the test harness main.cpp which i can not edit

#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();

any information on header files and how to use them with this main file would be much appreciated

Merci beaucoup

解决方案

I'm pretty sure YOU are supposed to create the ArrayIntStorage.h header AND implement the matching ArrayIntStorage.cpp.

Based on the "// sort data structure using std" comment you are expected to use and create a wrapper over an appropriate stl container, something like std::vector.

Based on the "// do not read sort" comment, you should, by default, sort the vector after each insert (unless, of course, someone calls setReadSort(false) on your wrapper).

In addition to the interface described above, you still need to implement >> and <<.

UPDATE.

Reading you question at C++ pass variable from .cpp to header file you seem to be quite confused by all this...

First thing first, adding support for >> and << operators:

You do this by declaring these operators in your .h file:

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

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

You then define their implementation in the .cpp file:

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

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

Obviously, you need to add some proper code there, this is just to make it compile. If it still doesn't compile, check if you have included the stream headers in your .h file:

#include <fstream>
#include <iostream>

Now for some general info:

Your array storage should be based on something like std::vector. The purpose of the >> and << function you need to implement is to add and retrieve int's from that container.

Since the ArrayIntStorage is a class, once you've established the interface you need ( the public member functions in the .h file) you should only look at the .h and .cpp to flesh out the implementation.

Once that is done, you don't need any of the "extern" madness the answers to your other question said. Look at your main function. If creates an object of your class and the fin1 stream. It then calls the >> operator you've implemented. All of this is done with local variables.

This is how you "use the value of this variable from main.cpp". You call a member function of your class with that variable as a parameter.

And finally, if you have all these problems with understanding header files and link errors, are you sure you've started with the proper training exercise?

这篇关于C ++数组中的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆