Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* Could somebody pls try this C++ test program on ALPHA/OSF?
@ 1995-02-20 20:00 Dan Kelley
  1995-02-21  4:06 ` Dirk Grunwald
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Kelley @ 1995-02-20 20:00 UTC (permalink / raw)


Hi.  I am trying to compile a c++ program on an alpha/osf machine, and having
difficulties.  Would somebody be so kind as to try compiling it on their machine?
I'd like to learn whether the c++ compiler on this particular machine is
installed incorrectly, or whether the alpha c++ compiler is faulty.  The program
compiles fine with g++ on my sun. (Note: I'm uncertain of whether the compiler
was installed correctly because it was installed by somebody else -- a
scientist, not a system person -- on a friends machine I've just recently got
access to.) 

Thanks very much in advance.  This should only take a moment of your time if you
try it.

*** DEMO OF FAILURE TO LINK ON ALPHA MACHINE ***
  % cxx test.cc | & demangle
  ld:
  Unresolved:
  MyVector<float>::MyVector<float>(int)
  MyVector<float>::push_back(const float&)
  MyVector<float>::operator ()(unsigned int)


*** TEST PROGRAM ***

#include <stdio.h>
#include <iostream.h>
static const int CAPACITY_DEF =  16;

template<class T>
class MyVector
{
public:
    MyVector(int capacity = CAPACITY_DEF); // default capacity
    ~MyVector() {delete [] contents;}	    // clean up
    void push_back(const T& a);		    // store an item at top
    T& operator() (unsigned int offset);    // get item at index
    unsigned size() const {return the_depth;} // get present depth
protected: 
    int the_capacity;		// max elements
    int the_depth;		// number elements now
    T* contents;		// can hold capacity elements
    void enlarge();		// double capacity
    void err(const char *msg) const; // for debugging
};

template<class T>
MyVector<T>::MyVector(int capacity = CAPACITY_DEF)
{
    contents = new T[the_capacity = capacity];
    if (!contents)
	err("\nOut of memory\n");
    the_depth = 0;
}

// Push at back of list
template<class T>
void MyVector<T>::push_back(const T& item)
{
    while (the_depth > the_capacity - 1)
	enlarge();		// only done once, actually
    contents[the_depth++] = item;
}

template<class T>
void MyVector<T>::enlarge()
{
    if (the_capacity > 1)
	the_capacity *= 2;
    else
	the_capacity = 2;
    T* contents_new = new T[the_capacity];
    if (!contents_new)
	err("\nOut of memory\n");
    for (int i = 0; i < the_depth; i++)
	contents_new[i] = contents[i];
    delete [] contents;
    contents = contents_new;
}

template<class T>
T& MyVector<T>::operator()(unsigned int offset)
{
    if (offset < the_capacity)
	return contents[offset];
    else {
	char msg[100];
	sprintf(msg, "Index %d is out of bounds (limit is 0 to %d)\n",
		offset, the_depth);
	err(msg);
	return contents[0];	// to prevent compiler error
    }
}

template<class T>
void MyVector<T>::err(const char *msg) const
{
    cerr << msg;
    exit(1);
}

int
main(int argc, char *argv[])
{
    MyVector<float> a;
    for (int i = 0; i < 10; i++) {
	a.push_back(float(0.1 * i));
	//a.push_back(0.1 * i); // wrong on gcc
    }
    for (i = 0; i < 10; i++) {
	cout << "a[" << i << "] = " << a(i) << "\n";
    }
    return 0;
}







^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1995-02-21  4:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-02-20 20:00 Could somebody pls try this C++ test program on ALPHA/OSF? Dan Kelley
1995-02-21  4:06 ` Dirk Grunwald

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox