C++ array in header file
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-开发者_Go百科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?
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
}
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.
精彩评论