cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/26/06:15:24

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Operator overloading problem
Date: Wed, 26 Aug 1998 09:44:53 GMT
Organization: EUnet Austria
Lines: 103
Message-ID: <35e5b656.487056@news.Austria.EU.net>
References: <35e30d75 DOT 267206 AT news DOT telepac DOT pt>
NNTP-Posting-Host: e146.dynamic.vienna.at.eu.net
Mime-Version: 1.0
NNTP-Posting-Date: 26 Aug 1998 09:46:28 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: morgado AT mail DOT telepac DOT pt (Jorge Bruno S. S. Morgado)
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Tue, 25 Aug 1998 19:34:05 GMT:

>Can you tell what is wrong with this code:
>
>I've made a class called Matriz, like this:
>
>
>
>#define	MIN_LINHAS	2
>#define	MIN_COLUNAS	2
>
>class Matriz
>{
> protected:
> double *matriz_ptr;
> unsigned linhas;
> unsigned colunas;
>
> public:
>  Matriz(unsigned i = MIN_LINHAS, unsigned j = MIN_COLUNAS):
>   linhas(i), colunas(j) {matriz_ptr = new double[linhas * colunas];}
>  ~Matriz() {delete[] matriz_ptr;}
>  unsigned n_linhas() const {return linhas;}
>  unsigned n_colunas() const {return colunas;}
>  double& operator()(unsigned linha, unsigned coluna)
>   {return *(matriz_ptr + (linha-1) + (coluna-1) * linhas);}
>  friend bool operator==(Matriz&, Matriz&);
>};

remove this "friend" keyword.

>bool operator==(Matriz &a, Matriz &b)

and this should be

bool Matriz::operator==(Matriz &a, Matriz &b)

I think this should work then.

>{
> int i, j;
>
> if(b.n_linhas()!=a.n_linhas() || b.n_colunas()!=a.n_colunas())
>  return(false);
>
> for(i=1; i<=a.n_linhas(); i++)
>  for(j=1; j<=a.n_colunas(); j++)
>   if(a(i,j) != b(i,j));
>    return (false);
> return(true);
>}
>
>
>
>
>And in main i've put something like this:
>
>
>
>
>void main()
>{
> Matriz A(2, 3);
> Matriz B;
>
> A(1,1) = 11;
> A(1,2) = 12;
> A(1,3) = 13;
> A(2,1) = 21;
> A(2,2) = 22;
> A(2,3) = 23;
>
> if(A == B)
>  cout << "\n\nB = A";
> if(c != y)
>  cout << "\nB != A";
>}
>
>
>But the problem is that when I try to compile, it gives the following 
>error:
>
>Error: A undeclared (first use this function)
>Error: B undeclared (first use this function)
>
>
>
>Do you know what is wrong with this code ?
>
>
>

--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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