C++头文件中的数组

发布于 2024-11-04 22:10:01 字数 1188 浏览 2 评论 0原文

我正在做一项训练练习,但开始时遇到困难。我必须为数组数据结构编写一个程序。提供了一个测试工具,我需要实现数据结构的代码。我不确定如何在头文件中定义数组,

下面是测试工具 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-11-11 22:10:01

我很确定您应该创建 ArrayIntStorage.h 标头并实现匹配的 ArrayIntStorage.cpp。

基于“//使用 std 排序数据结构”注释,您应该在适当的 stl 容器上使用并创建一个包装器,例如 std::vector。

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

除了上面介绍的接口之外,还需要实现>>和<<。

更新。

阅读您的问题 C++ 将变量从 .cpp 传递到头文件 你似乎对这一切感到很困惑...

首先,添加对>>的支持和<<运算符:

您可以通过在 .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::vector 之类的东西。 >> 的目的和<<您需要实现的功能是从该容器添加和检索 int。

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

一旦完成,您就不需要其他问题的答案中所说的任何“外部”疯狂。
看你的主要功能。如果创建您的类的对象和 fin1 流。然后它调用>>您已实施的运算符。所有这些都是通过局部变量完成的。

这就是“使用 main.cpp 中该变量的值”的方式。您可以使用该变量作为参数来调用类的成员函数。

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

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?

脸赞 2024-11-11 22:10:01

头文件 ArrayIntStorage 应如下所示:-

函数签名可能会根据用法而改变:-

// 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

}

The header file ArrayIntStorage should be like below :-

The function signature may change according to the usage :-

// 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

}
硪扪都還晓 2024-11-11 22:10:01

马克,

http://www.cplusplus.com/doc/tutorial/(我提到过在我的评论中)让我们失望了。他们不会将头文件本身作为一个主题来讨论......但是 http://www.learncpp.com/cpp-tutorial/19-header-files/ 确实如此。

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

因此,您可以在 .h 文件中定义类的功能...然后在 .cpp 文件中定义它的工作方式。

这有道理吗?

干杯。基思.

Marc,

http://www.cplusplus.com/doc/tutorial/ (which I mentioned in my comment) has let us down. They don't dicuss header-files as a topic in it's own right... however http://www.learncpp.com/cpp-tutorial/19-header-files/ does.

Basically a header file defines "the interface" of your class. It's put in a seperate file so that users of your class can include JUST this "interface definition" in there code... Like #include "ArrayIntStorage.h" for example. This allows the compiler to determine if they've got something wrong, like a miss-spelled method name, or passing a parameter of the wrong type... you know, all those compiler errors you've seen... yeah?

So you define WHAT your class does in the .h file... and then in the .cpp file you define HOW it does it.

Does that make sense?

Cheers. Keith.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文