crq 0 Newbie Poster

>but i am supposed to use ostream overloading in a non-member function.
Friend functions are not member functions. They are non-member functions that have access to the private members of the class.

>so the friend wasn't accepted by my compiler
Well, I have no idea what's wrong with your code because you use a stupid non-standard header file that declares equally stupid things. Since I don't have the header, I can't compile your code.

i am using the skeleton that my instructor provided. i am not very excited about the way he has laid this stuff out either. i am just trying to stay within the rules that he has set for me. but i won't bother asking anymore questions on here. you seem to enjoy the saying nasty things to me ... but i guess you only do that to us "stupid" people.

thanks
crq

crq 0 Newbie Poster
// class.h
#include <iostream>

class C {
  // Stuff
  friend std::ostream& operator<<(ostream& out, const C& c);
  friend std::istream& operator>>(istream& in, C& c);
};

i tried this in my Array.h template class file. but i am supposed to use ostream overloading in a non-member function. so the friend wasn't accepted by my compiler. that's why i am having trouble figuring this out. template/non-member functions/and ostream overloading is a bit out of my league at this point.

thanks
crq

sorry, i don't know how i put that little wierd face in the code ...

crq

crq 0 Newbie Poster
// class.h
#include <iostream>

class C {
  // Stuff
  friend std::ostream& operator<<(ostream& out, const C& c);
  friend std::istream& operator>>(istream& in, C& c);
};

i tried this in my Array.h template class file. but i am supposed to use ostream overloading in a non-member function. so the friend wasn't accepted by my compiler. that's why i am having trouble figuring this out. template/non-member functions/and ostream overloading is a bit out of my league at this point.

thanks
crq

crq 0 Newbie Poster

thanks for looking at this ...

this is Test2.C

#include <assert.h>
#include <iostream>
#include "Array.h"

#include "/unc/hedlund/121/lib/cpluslib.h"

using namespace std;

int
main()
{




        cout<< "*******Declaration of 3 IntArray objects/size() checking******* \n" <<endl;

        cout<< "IntArray a declared with no parameters" <<endl;

        Array<int> a;
        cout << "   " <<endl;

return 0;
}

and the Array.h is the same except for putting the

using namespace std;

after the includes and putting the () after the T in the init method.

and this is what the compiler says ...

hmmm... syntax error. that is usually a goof error. are the numbers at the side line numbers? anyone know how to turn on the line numbers in pico?

In file included from Test2.C:4:
Array.h:367: syntax error before `>' token
Array.h:371: `b' was not declared in this scope
Array.h:371: syntax error before `;' token
Array.h:371: syntax error before `++' token
Array.h:384: too many template parameter lists in declaration of `int
includes(const Array<T>&, int)'
Array.h:384: syntax error before `{' token
Test2.C:12: cannot declare `::main' to be a template
Test2.C:12: confused by earlier errors, bailing out

thanks for looking at this!! ... i appreciate it more than you can ever know. really.

crq

crq 0 Newbie Poster

hi there. i am on a UNIX system and am writing a template code called Array.h. it is called by a Test2.C file which does nothing more than declare a T object of type int. Array<int>.

i am getting all kinds of compile errors. have been over it and over it for about 8 hours now. can anyone help me here. i know there is something wrong with my non-member functions and i know there is something wrong with my init method that constructs my T's.

this is what i am getting from the compiler when i run Test2.C

Array.h:71: syntax error before `&' token
Array.h:71: `ostream' was not declared in this scope
Array.h:71: `os' was not declared in this scope
Array.h:71: syntax error before `<' token
Array.h:71: ISO C++ forbids declaration of `operator<<' with no type
Array.h:71: `int& operator<<(...)' must have an argument of class or enumerated
type
Array.h:71: `int& operator<<(...)' must take exactly two arguments
Array.h: In member function `void Array<T>::init(const T*, int)':
Array.h:349: syntax error before `;' token
Array.h: At global scope:
Array.h:366: syntax error before `>' token
Array.h:370: `b' was not declared in this scope
Array.h:370: syntax error before `;' token
Array.h:370: syntax error before `++' token

if anyone wanted to help me figure out what is going on with this mess that would be great. this is only PART A of what i need to be doing …

crq 0 Newbie Poster

>i can't initialize everything to 0 becuase what i pass in MAY not be ints.
I don't see what your problem is. If you read my answer you'll see that I used the default constructor for T (which works for built in types as well) as the replacement for 0. Did you try it? Or did you just assume that I haven't got a clue and proceed to give me irrelevant code snippets with the same question again?

so sorry. i didn't realize that you had made changes to the code. the changes were minor fixes that make a world of difference. i didn't see them though. i thought you were quoting the code and had put it up to show me why the asserts did/didn't work. now i see that you took out the one that wasn't ok, and that you inserted the T. thanks for your help. i will try it now. didn't mean to imply that you "haven't got a clue". obviously, i am the one who hasn't got a clue. i won't bother you again.

thanks
crq

crq 0 Newbie Poster

i have 2 constructors that use this function

template<class T> 
Array<T>::
Array ( int sz )
{
                                  
        assert ( 0 <= sz <= MAXINT );              
        cout << "!!! IntArray default constructor called" <<endl;
                                    
        init ( NULL, sz );
}

template<class T>
Array<T>::
Array ( const Array<T>& x )
{
        cout << "!!! IntArray copy constructor called" <<endl;
                                                                              
        init ( x.d_array, x.size() );
}

since the default constructor assigns the number of elements, there would be a NULL declared for the array. i just don't know what to put in the init() method (for either of these) to make the initialization work for a template. i can't initialize everything to 0 becuase what i pass in MAY not be ints. i can't initialize all the T elements of the array to NULL can i?

thanks
crq

void  IntArray::
init ( const T *data, int sz)
{
        assert( (0 <= sz) && (sz <= MAXINT));

        d_num_elements = sz;
        if ( 0 == d_num_elements ){          // if no elements in array
                d_array = NULL;                  // array points to NULL
        }
        else{
                d_array = new T [d_num_elements];
        }
        for (int i=0; i < sz; i++ ){
                if ( NULL == data ){
                        self[i] = T();
                }
                else{
                        self[i] = data[i];
                }
        }
}

>assert ( NULL != d_array );
This is a very bad idea for two reasons. First, assert is meant for debugging program errors. Memory allocation failing is not a program error, it's a runtime error that needs …

crq 0 Newbie Poster

yes. that makes sense to me. yet, my prof wants us to use one of his already compiled files (a .o file) that was made from a .h file. so that's why i was wondering if I needed to do the same.

thanks
crq

headerfiles are not compiled, clear and simple.

.o files are compiled compilation units which have not yet been linked, header files are not compilation units.

crq 0 Newbie Poster

STILL trying to figure out how to link files in C++ in a UNIX environment!! i just don't get it. how do i make .o files? what are they for? how do you "link" them with other stuff and "run" them. all i am capable of is g++ somepgm.C
and then a.out

sorrry .... frustrated. prof and TA just ASSUME that i know this ... and i don't.

i have a template class Arrray.h. i need to make a Test.C class that will have main() and make some Array<T> objects and test them. can ANYONE tell me the commands to do this?

thanks
crq

crq 0 Newbie Poster

hey folks,

i have a template created that holds an array of type T elements. my problem is this ...

i am converting this code from a souped up Array class that takes ints as it's elements to this template Array<T> class that will hold T elements. in the original class, i knew that i was going to be dealing with ints as the elements. so i had a default constructor and a copy constructor that both called a private init method to initialize the array depending on what was passed in.

void  IntArray::
init ( const int *data, int sz)
{ 
                                                
        assert( (0 <= sz) && (sz <= MAXINT));

        d_num_elements = sz;
        if ( 0 == d_num_elements ){          // if no elements in array
                d_array = NULL;                  // array points to NULL
        }
        else{
                d_array = new int [d_num_elements];
                                                     // allocate memory
                                                     // if new returns NULL then no
                                                     // more available memory
                assert ( NULL != d_array );
        }
        for (int i=0; i < sz; i++ ){
                                           
                if ( NULL == data ){
                        self [i] = 0;
                }
                else{
                        self[i] = data[i];
                }
        }
}

so how do i conver this to make it work for type T? i can't initialize the elements to 0 ...

thanks
crq

crq 0 Newbie Poster

WOW!! thanks!!

i will try that.

cr1

// Class.h
class C {
  int i;
public:
  C();
public:
  int geti() const;
};
// Class.cpp
#include "Class.h"

C::C(): i(0) {}

int C::geti() const { return i; }
// main.cpp
#include <iostream>
#include "Class.h"

using namespace std;

int main()
{
  C c;

  cout<< c.geti() <<endl;
}

From the command line you would compile it something like this (your mileage may vary):

$ g++ class.cpp main.cpp
$ ./a.out
crq 0 Newbie Poster

ok, here's the scenario (and maybe i will finally get this assignment finished) ...

i have a class IntArray. at the moment i have it all in a .C file. i have a little main section that has minimal testing in it (will do something more formal later). my prof. wants us to "split" it into an IntArray.C an IntArray.h and a Test.C.

i want to know what i need to #include and where so that when i run my Test.C it will be able to make objects from IntArray (.C or .h) and thoroughly test them. i don't know what the difference in an IntArray.C and an IntArray.h is, though ... so i don't know what is to be in those ...

advice?

THANKS
crq

crq 0 Newbie Poster

oh, and there is this one too ... they both call each other. i am getting the same syntax error in both.

bool IntArray::
operator != ( const IntArray &rhs) const
{
        if ( size() == rhs.size() ) {
                return ( FALSE );
        }
        int i = 0;
        while ( i < size() ){
                if ( self[i] == rhs[i] ){ 
                        return ( TRUE );
                }
                else i++
        }
        return ( FALSE );
}

can anyone help me with this one? i am getting a syntax error that reads like this:
lab2aa.C: In member function `bool IntArray::operator==(const IntArray&)
const':
lab2aa.C:80: syntax error before `}' token

my size() method works (tested it before i put in these operator things) ...

and self is *this in an include file ... TRUE and FALSE are true and false respectively in the include file, too.

what am i missing here?

bool IntArray::
operator == ( const IntArray &rhs ) const
{
        if ( size() != rhs.size() ) {
                return ( FALSE ); 
        }
        int i = 0;
        while ( i < size() ){
                if ( self[i] != rhs[i] ){                      
                        return ( FALSE );
                }
                else i++
        }
        return ( TRUE );
}

thanks!!

charity

crq 0 Newbie Poster

can anyone help me with this one? i am getting a syntax error that reads like this:
lab2aa.C: In member function `bool IntArray::operator==(const IntArray&)
const':
lab2aa.C:80: syntax error before `}' token

my size() method works (tested it before i put in these operator things) ...

and self is *this in an include file ... TRUE and FALSE are true and false respectively in the include file, too.

what am i missing here?

bool IntArray::
operator == ( const IntArray &rhs ) const
{
        if ( size() != rhs.size() ) {
                return ( FALSE ); 
        }
        int i = 0;
        while ( i < size() ){
                if ( self[i] != rhs[i] ){                      
                        return ( FALSE );
                }
                else i++
        }
        return ( TRUE );
}

thanks!!

charity

crq 0 Newbie Poster

in the skeleton code that my instructor provided, he has some assert (boolean statement) calls. is assert a keyword in C++ and is it a built-in method ...? do i need to put something at the top of my code to make sure it is called?

at the top of my code right now i have

#include <iostream>
using namespace std;

#include "file in the system that has a few things .. but no assert"

and then my class declaration starts.

thanks
charity

crq 0 Newbie Poster

in working on this pgm, i have made notes to myself ... questions that keep popping up. if anyone wants to clarify some of these concepts for me ....

1. are TRUE and FALSE keywords? if a method says
return (TRUE);
will it work?

2. const on END of method declaration and const IN the parameter of the method declaration ...
sometype somemethod (const sometype& x) const
what do both of these do? are they basically INSURANCE ... just be sure the method won't change the parameter passed in and ... ?

3. does the deconstructor happen at the end of the pgm automatically. i know that you need to manually delete things sometimes ... like in an assignment ... but does the
virtual ~ClassName(); deconstructor somehow automatically called on all objects that have been created at the end of method main?

4. scenario 1
int* d_array;
d_array = NULL;
this is read as d_array points to an int and the object that d_array points to is NULL (which would be 0 in the case of an int) ... is that right?
scenario 2
int* d_array;
*d_array = NULL;
what does this say? would it cause an error? is the '*' only used when declaring a pointer?

5. what is the proper way to do the & and * ...
int *d_array; or
int* d_array;

6. i am confused with …

crq 0 Newbie Poster

your implementation of int size() has no class before it; it should be

int IntArray::size()

and int IntArray::&operator [] ( int i ) const

does not exist in your class definition (instead you have an = operator that you don't implement), and the '&' doesn't belong before the word 'operator'.

Good Luck!

THANK YOU THANK YOU THANK YOU!! the size() stuff and the = operator stuff were just dumb ... and i had no clue about the & part ... thanks for looking this over. i couldn't see anymore!!

crq

crq 0 Newbie Poster

i have been working on this all DAY LONG. i am new to C++ and Unix and need to write this little tiny code in both. i am tripping all over myself with references and dereferences and operator overloads .... if anyone could help me decipher what my problem(s) are, that would be a lifesaver right now.
this is the error that i keep getting ... and below is the code.

classroom(52)% g++ lab2aa.C
lab2aa.C: In function `int size()':
lab2aa.C:55: `d_num_elements' undeclared (first use this function)
lab2aa.C:55: (Each undeclared identifier is reported only once for each
function it appears in.)
lab2aa.C: At global scope:
lab2aa.C:59: syntax error before `&' token
lab2aa.C: In member function `void IntArray::init(const int*, int)':
lab2aa.C:82: no match for `IntArray& [int&]' operator
lab2aa.C:85: no match for `IntArray& [int&]' operator

#include <iostream>
using namespace std;

#include "/unc/hedlund/121/lib/cpluslib.h"

class IntArray{


public:
        IntArray ( int sz = 10 );      
 
        IntArray ( const IntArray &x);

        virtual ~IntArray();

        int size () const;
    
        int &operator = ( int i ) const;

protected:
        int d_num_elements;
        int *d_array;


private:
        void init (const int *x, int sz);
};

IntArray::
IntArray  ( int sz )
{
        cout << "!!! default constructor called " <<endl;
        init ( NULL, sz);
}

IntArray::
IntArray ( const IntArray &x )
{
        cout<< "!!!  constructor" <<endl;
        init ( x.d_array, x.size() );
}

IntArray::
~IntArray()
{
        cout << "!!! IntArray destructor " <<endl;
        if ( size() > 0){
                delete [] d_array; …
crq 0 Newbie Poster

hi, i am REALLY new to C++ and don't really understand much of anything about the pointers and references and the way the files are set up.

HOWEVER, i have an assignment to do that SHOULD help me get a grasp on some of the key concepts before we go to the big stuff. we are working in Unix and i am using Pico. UNIX is ALSO very new to me. i know about 8 commands.

so the prof. has given us an assignment and a skeleton code that has the names of some of the functions that we need to define ... BUT he wrote in the instructions taht was are to "split" the solution into 3 files: IntArray.h, IntArray.C, and test.C.

i have only done ONE pgm in C++ on UNIX. i just wrote the thing (consisted of some loops and branches only) closed pico. saved my file as a .C. then i did g++ on it and a.out. i have no clue how to SPLIT my file and what it is for and what parts are to be in which!!

any suggestions (in as non-techie language as possible ... ) would be great!

thanks
crq

crq 0 Newbie Poster

at the bottom of the pico window there should be a load from file or something its been a while since i used it but it will load from a file all of the text you edited before

SUPER!! i will try that. it would be a real time saver. i have been copying and pasting and opening and closing ... a real pain.

THANKS!

crq

crq 0 Newbie Poster

In the unix that I used last semester in school the editor was called "pico" just type that on the command line and the text editor comes up. I don't know if that is standard unix or if it was on the server that I was using but you can always give it a shot.

hey, sykobag,

i am using pico too. i type pico and a blank window comes up. the problem that i have is this ... when i quit a session, save my file and want to come back to it. if i log back on and type pico, i get a blank editor. how do i call pico for the file that i want to edit instead of calling a blank window each time?

thanks
crq

crq 0 Newbie Poster

thanks for replying!

i actually found the problem ... and it was a goof-up silly mistake. i was doing things in an array element that hadn't been initalized. i am still in java mode it seems (elements of int arrays are by default initalized to 0). somehow that was producing the segmentation fault. i still don't know what that term means, but it turned out to be something silly. i am so unfamiliar with C++ and with Unix that i was sure my mistake was the result of something that i wouldn't know to do.

oh, series is a function. was declared above. i had tested it and i had the problem isolated to 2 lines because of the cout<< calls that i had going.

thanks for the help .. i am sure i will need more in the coming weeks.

thanks
crq

crq 0 Newbie Poster

THANKS for the style tips. i agree with you. i like lots of spacing in my coding. however, i have been beaten over the head with doing it the exact opposite ....

for(int i=1; i<MAX_CALLS; i++){ h[i]=0} ....
...........

.......

that stuff makes me crazy, but it seems to be the going style where i am. anything else is a bit "novice" or something ... silly, though it may be.

i think i have figured it out, though. i had some uninitialized array stuff that was making my code not work ... and yes i need to check on the number part. either i left it off when i copied and pasted ... or i lost it somewhere in my coding. i am so new at unix, that i don't know how to go to my file, and open it in the editior. all i know how to do is VIEW the file, copy it. then open a text editor window and paste it in there EVERY time i want to make a change after i compile and run ... ARRRRGGGHHH!!

thanks, though.
crq

crq 0 Newbie Poster

hi there. i am new to C++ AND unix. this assignment that i have is really really easy, but the specifics of C++ and getting around in Unix is making it really hard and time consuming. i have a really small program that should be working, but i am getting the wrong output AND at the end of the output i get a Segmentation fault.

i have a function called series that is declared and works fine. i have inserted a cout<< to check that the function is working right. but i am still getting the wrong output AND this error. am i doing something illegal? using the function result as the array index? i know i am not telling you much about the code, i am just wondering if there are any GLARING unix or C++ no-nos that anyone sees.

int main (){
   const int MAX_NUM = 60;

   const int MAX_CALLS = 41;

   int num_calls [MAX_CALLS];
   
   int k = 0;

   for(int i=1; i<=MAX_NUM; i++){
      num_calls[i] = 0;
      cout<< series(i) <<endl;
      if ((series (i)) < MAX_CALLS){
        num_calls[series (i)]++;
      }
      else{ 
        k++;
      }
   }
   
   for(int l=1; l<MAX_CALLS; l++){
      cout<< l << "       " << num_calls[l] <<endl;
   }
   
   cout<< "and " << k << "numbers have series length greater than 40" <<endl;
}

thanks
crq