cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/20/08:54:22

From: jakarppi AT paju DOT oulu DOT fi (Jari Karppinen)
Newsgroups: comp.os.msdos.djgpp
Subject: Trouble with templates and gcc-2.95
Date: 20 Sep 1999 12:09:59 GMT
Organization: University of Oulu, Department of Mathematical Sciences
Lines: 306
Message-ID: <7s586n$k60$1@ousrvr3.oulu.fi>
NNTP-Posting-Host: paju.oulu.fi
Mime-Version: 1.0
X-Trace: ousrvr3.oulu.fi 937829399 20672 130.231.240.20 (20 Sep 1999 12:09:59 GMT)
X-Complaints-To: news AT news DOT oulu DOT fi
NNTP-Posting-Date: 20 Sep 1999 12:09:59 GMT
X-Newsreader: TIN [UNIX 1.3 unoff BETA release 960807]
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I wrote a simple templatized vector class. There was no trouble 
compiling the source with egcs-1.1.2, but after I switched to 
gcc-2.95, I got the following:

vec.cc: In instantiation of `Vector<int>':
vec.cc:285:   instantiated from here
vec.cc:44: invalid use of undefined type `class Vector<int>'
vec.cc:48: forward declaration of `class Vector<int>'
vec.cc:44: confused by earlier errors, bailing out
 
I made no changes to the source, and can't see why this happens. I would
really appreciate advice from experts.

-- vec.cc:

#include <cmath>
#include <iostream>
#include <string>
#include <strstream>

template<class T> class Vector {
private:
	unsigned size_;
        T *data;
private:
	void init(unsigned);
        void copy(const T *);
        void destroy();
public:
	Vector();
	Vector(unsigned);
        Vector(unsigned, const T&);
        Vector(unsigned, const T *);
        Vector(unsigned, const char *);
	Vector(const Vector<T>&);
        Vector(istream&);
	~Vector();

	Vector<T>& operator = (const Vector<T>&);

	const T& operator() (unsigned) const;
	T& operator() (unsigned);

	friend ostream& operator << <> (ostream&, const Vector<T>&);
	friend istream& operator >> <> (istream&, Vector<T>&);

	unsigned size() const;
	void resize(unsigned);
        void fill(const T&);

	Vector<T> operator - ();

	Vector<T>& operator += (const Vector<T>&);
	Vector<T>& operator -= (const Vector<T>&);
	Vector<T>& operator *= (const T&);
	Vector<T>& operator /= (const T&);

	friend Vector<T> operator + <> (const Vector<T>&, const Vector<T>&);
	friend Vector<T> operator - <> (const Vector<T>&, const Vector<T>&);
	friend Vector<T> operator * <> (const Vector<T>&, const T&);
	friend Vector<T> operator * <> (const T&, const Vector<T>&);
	friend Vector<T> operator / <> (const Vector<T>&, const T&);
};

template<class T> inline void Vector<T>::init(unsigned size)
{
        data = new T[size_ = size];
}

template<class T> inline void Vector<T>::copy(const T *v)
{
        for (unsigned i = 0; i < size_; i++) {
        	data[i] = v[i];
	}
}

template<class T> inline void Vector<T>::destroy()
{
	delete[] data;
        data = 0;
}

template<class T> Vector<T>::Vector()
{
	size_ = 0;
        data = 0;
}

template<class T> Vector<T>::Vector(unsigned size)
{
	init(size);
}

template<class T> Vector<T>::Vector(unsigned size, const T& filler)
{
	init(size);
        fill(filler);
}

template<class T> Vector<T>::Vector(unsigned size, const T *v)
{
	init(size);
        copy(v);
}

template<class T> Vector<T>::Vector(unsigned size, const char *v)
{
        istrstream s(v, strlen(v));

        init(size);
        for (unsigned i = 0; i < size; i++) {
        	s >> data[i];
	}
}

template<class T> Vector<T>::Vector(const Vector<T>& vector)
{
	init(vector.size());
        copy(vector.data);
}

template<class T> Vector<T>::Vector(istream& s)
{
	unsigned size;

        s >> size;
        init(size);
        for (unsigned i = 0; i < size; i++) {
                s >> data[i];
	}
}

template<class T> Vector<T>::~Vector()
{
	destroy();
}

template<class T> Vector<T>& Vector<T>::operator = (const Vector<T>& vector)
{
        if (this == &vector) return *this;
	resize(vector.size());
        copy(vector.data);
        return *this;
}

template<class T> inline const T& Vector<T>::operator() (unsigned i) const
{
	return data[i];
}

template<class T> inline T& Vector<T>::operator() (unsigned i)
{
	return data[i];
}

template<class T> ostream& operator << (ostream& s, const Vector<T>& vector)
{
        s << vector.size() << endl;
        for (unsigned i = 0; i < vector.size(); i++) {
		s << vector(i) << " ";
	}
        s << endl;
	return s;
}

template<class T> istream& operator >> (istream& s, Vector<T>& vector)
{
	unsigned size;
        
        s >> size;
        vector.resize(size);
        for (unsigned i = 0; i < size; i++) {
                s >> vector(i);
	}
	return s;
}

template<class T> inline unsigned Vector<T>::size() const
{
	return size_;
}

template<class T> void Vector<T>::resize(unsigned size)
{
	if (size_ != size) {
        	destroy();
                init(size);
	}
}

template<class T> void Vector<T>::fill(const T& filler)
{
	for (unsigned i = 0; i < size_; i++) {
                data[i] = filler;
	}
}

template<class T> Vector<T> Vector<T>::operator - ()
{
	Vector<T> result(size_);
        for (unsigned i = 0; i < size_; i++) {
                result.data[i] = -data[i];
	}
        return result;
}

template<class T> Vector<T>& Vector<T>::operator += (const Vector<T>& vector)
{
 	for (unsigned i = 0; i < size_; i++) {
                data[i] += vector(i);
	}
        return *this;
}

template<class T>
Vector<T>& Vector<T>::operator -= (const Vector<T>& vector)
{
 	for (unsigned i = 0; i < size_; i++) {
                data[i] -= vector(i);
	}
        return *this;
}

template<class T> Vector<T>& Vector<T>::operator *= (const T& x)
{
	for (unsigned i = 0; i < size_; i++) {
		data[i] *= x;
	}
	return *this;
}

template<class T> Vector<T>& Vector<T>::operator /= (const T& x)
{
	for (unsigned i = 0; i < size_; i++) {
		data[i] /= x;
	}
	return *this;
}

template<class T>
Vector<T> operator + (const Vector<T>& A, const Vector<T>& B)
{
	Vector<T> result = A;
        result += B;
        return result;
}

template<class T>
Vector<T> operator - (const Vector<T>& A, const Vector<T>& B)
{
	Vector<T> result = A;
        result -= B;
        return result;
}

template<class T>
Vector<T> operator * (const Vector<T>& A, const T& x)
{
	Vector<T> result = A;
        result *= x;
        return result;
}

template<class T>
Vector<T> operator * (const T& x, const Vector<T>& A)
{
	Vector<T> result = A;
        result *= x;
        return result;
}

template<class T> Vector<T> operator / (const Vector<T>& A, const T& x)
{
	Vector<T> result = A;
        result /= x;
        return result;
}

template<class T> T dot(const Vector<T>& x, const Vector<T>& y)
{
 	T result(0);
	for (unsigned i = 0; i < x.size(); i++) {
        	result += x(i) * y(i);
	}
        return result;
}

template<class T> T norm(const Vector<T>& x)
{
	return sqrt(dot(x, x));
}

template<class T> void normalize(Vector<T>& x)
{
	x /= norm(x);
}

int main()
{
	Vector<int> vec(5);

	vec.fill(0);
        cout << vec << endl;
}


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019