Hints for Using C++ |
g++ reverse1.cc
// a simple program to illustrate compiling. #include#define MAX 5 // The maximum number of numbers we can input. // Function reverse reverses inarray and places the result in revarray. void reverse(int inarray[], int revarray[]){ int j; for (j=0;j<MAX;j++){ revarray[j] = inarray[MAX - j -1]; } } void main(){ int i; int inarray[MAX]; // The input array int revarray[MAX]; // The reversed array cout<<"Enter 5 integers each followed by <return>" << endl; for (i=0;i<MAX;i++){ cin >> inarray[i] ; // Get the five numbers. } reverse(inarray,revarray); // Call our reverse function. cout << "The integers you input are: " << endl; for (i=0;i<MAX;i++){ // print out inarray cout << inarray[i] << " "; } cout << endl; cout << "The integers in reverse order are: " << endl; for (i = 0;i<MAX; i++){ // print out revarray. cout << revarray[i] << " " ; } cout << endl; }
g++ -c revlib.c | Make the first object file |
---|---|
g++ -c rev2.cc | Make the second object file |
g++ rev2.o revlib.o | Now link them into an executable |
make
rev2: rev2.o revlib.o g++ -o rev2 rev2.o revlib.o rev2.o: rev2.cc g++ -c rev2.cc revlib.o: revlib.cc revlib.h g++ -c revlib.cc clean: rm rev*.o *~
// A simple program to illustrate how to link multiple files together. # include# include "revlib.h" void main(){ int i; int inarray[MAX]; // The input array int revarray[MAX]; // The reversed array. cout<<"Enter 5 integers each followed by " << endl; for (i=0;i<MAX;i++){ cin >> inarray[i] ; // Get the input from stdin } reverse(inarray,revarray); // Reverse the input array. cout << "The integers you input are: " << endl; for (i=0;i<MAX;i++){ // Print inarray cout << inarray[i] << " "; } cout << endl; cout << "The integers in reverse order are: " << endl; for (i = 0;i<MAX; i++){ // Print revarray cout << revarray[i] << " " ; } cout << endl; }
#ifndef __REVLIB_H__ #define __REVLIB_H__ #define MAX 5 void reverse(int inarray[],int revarray[]); #endif
// A simple library file to reverse an array and place it in another array. #ifndef __REVLIB_CC__ #define __REVLIB_CC__ #include "revlib.h" void reverse(int inarray[], int revarray[]){ // We assume the caller is taking care of matching the size of revarray // and inarray! int j; for (j=0;j<MAX;j++){ revarray[j] = inarray[MAX - j -1]; } } #endif
stack<float> FakeStack;to the end of the stack.C file. Once a stack<float> is built when stack.C is compiled, the member functions are available to other instantiations in main.C. Obviously this is a workaround since it doesn't really allow general purpose library code, but maybe the problem will go away, ... someday.