www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/05/12:19:23

From: somjen AT netvision DOT net DOT il (Ohad Somjen)
Newsgroups: comp.os.msdos.djgpp
Subject: templates and djgpp - Mat.err (0/1)
Date: Mon, 01 Sep 1997 04:30:57 GMT
Organization: NetVision LTD.
Lines: 112
Message-ID: <340a3dd2.114729@news.netvision.net.il>
NNTP-Posting-Host: ts002p5.pop8a.netvision.net.il
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

i'm a student and just for practice i want to write a template class
for vectors and matrices. it's been a while since i coded so i just
remembered that all the code has to be in the header file, but this
does nt seem to work becuase is have an internal variable that causes
an error.
here is some code:
i had so many errors when i implemented the functions outside that i
gave up and inserted them into the class declaration.

template <class T>
class vector
{
 private:
    T* vec;
    int size;
    char dir;
 public:

 	 vector(int s,char d='h')
    {
		dir=d;
		vec=new T[s];
   	if (!vec)
   	{
   		cout << "could not allocate vector" << endl;
   	}
	 }

    vector(int s,T& t,char d='h')
    {
		dir=d;
   	vec=new T[s];
   	if (!vec)
   	{
   		cout << "could not allocate vector" << endl;
   	}

   	for(int i=0;i<s;i++)
			vec[i] = v[i];
	}

    ~vector()
    {
		delete[] vec;
	 }

    vector& operator+=(const vector& v)
    {
		if(size==v.size)
   		for(int i=0;i>size;i++)
      		vec[i]+=v.vec[i];
	   else
   		cout << "vector size do not match" << endl;
	   return *this
    }

    vector& operator+(const vector& v)
    {
	   vector temp;
		if(size==v.size)
   		for(int i=0;i>size;i++)
      		temp.vec[i]=vec[i]+v.vec[i];
	   else
   		cout << "vector size do not match" << endl;
	   return temp;
	 }

    T& scalar(const vector& u,const vector& v)
    {
		T temp=0;
	   if((u.dir!=v.dir) && (u.size==v.size))
	   	for(int i=0,i<size)
	      	temp+=(v.vec[i]*u.vec[i]);
   	else
   		cout << "vector size do not match or in the same dir."
<< endl;
	   return temp;
	 }

    vector& vectori(const vector& u,const vector& v)
    {
    vector temp;
    if((u.dir!=v.dir) && (u.size==v.size))
	   	for(int i=0,i<size)
	      	temp.vec[i]=(v.vec[i]*u.vec[i]);
   	else
   		cout << "vector size do not match or in the same dir."
<< endl;
	   return temp;
	 }

}


my main look like that:

#include "mat.h"
main()
{
vector<int> A(4,'h')
vector<int> B(4,'v');
return 0;
}

and when i compile i get a parse error before the '(' of main
then data template 'class vector<int> B' must be member of a class
template.
then assignment (not initialization) in declaration
and bailing out.

attached is the output of gcc for this compilation.

- Raw text -


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