Ringrazio anticipatamente chi volesse cimentarsi nel darmi una mano, martedì ho un esame di informatica! :P
Ho implementato la classe Matrice, che riporto qui di seguito (veramente l'ho scopiazzata dal prof), ma mentre io non riesco a vedere alcun errore ecco cosa mi esce dopo la compilazione in Visual C++:
Compiling...
matrice.cpp
matrice.cpp(32) : error C2556: 'int __thiscall Matrice::trasposta(void)' : overloaded function differs only by return type from 'void __thiscall Matrice::trasposta(void)'
matrice.h(24) : see declaration of 'trasposta'
matrice.cpp(32) : error C2371: 'trasposta' : redefinition; different basic types
matrice.h(24) : see declaration of 'trasposta'
matrice.cpp(39) : error C2374: 'i' : redefinition; multiple initialization
matrice.cpp(34) : see declaration of 'i'
matrice.cpp(48) : error C2556: 'int __thiscall Matrice::diagonale(void)' : overloaded function differs only by return type from 'void __thiscall Matrice::diagonale(void)'
matrice.h(26) : see declaration of 'diagonale'
matrice.cpp(48) : error C2371: 'diagonale' : redefinition; different basic types
matrice.h(26) : see declaration of 'diagonale'
matrice.cpp(67) : error C2556: 'int __thiscall Matrice::stampa(void)' : overloaded function differs only by return type from 'void __thiscall Matrice::stampa(void)'
matrice.h(28) : see declaration of 'stampa'
matrice.cpp(67) : error C2371: 'stampa' : redefinition; different basic types
matrice.h(28) : see declaration of 'stampa'
Error executing cl.exe.
Matrici.exe - 7 error(s), 0 warning(s)
Il codice è il seguente:
// matrice.h
#ifndef MATRICE_H
#define MATRICE_H
#include <iostream>
using namespace std;
class Matrice {
private:
int numRighe;
int numColonne;
int **p; //puntatore ad un array di puntatori
public:
//costruttore di default
Matrice ();
//costruttore
Matrice (int nr, int nc);
//distruttore
~Matrice ();
//inverte la matrice
void inversa();
//restituisce l'elemento (i,j)
int getElemento (int i, int j);
//set dell'elemento (i,j)
int setElemento (int i, int j, int valore);
//calcola la trasposta
void trasposta();
//verifica che sia diagonale
void diagonale();
//stampa della matrice
void stampa();
};
#endif
//matrice.cpp
#include "Matrice.h"
//costruttore di default
Matrice::Matrice(){
numRighe=0;
numColonne=0;
p=0;
}
//costruttore
Matrice::Matrice(int nr, int nc){
numRighe=nr;
numColonne=nc;
p= new int* [numRighe]; //array di puntatori
for (int i=0; i<numRighe; i++)
p[i] =new int [numColonne]; //array di valori
}
//distruttore
Matrice::~Matrice(){
cout << "Distruzione della matrice..." << endl;
for (int i=0; i<numRighe; i++)
delete [] p[i];
delete [] p;
}
//restituisce l'elemento (i,j)
Matrice::getElemento (int i, int j) {
return p[i][j];
}
//set dell'elemento (i,j)
Matrice::setElemento (int i, int j, int valore){
p[i][j]=valore;
}
//calcola la trasposta
Matrice::trasposta (void){
int **q= new int *[numColonne];
for (int i=0; i<numRighe; i++)
q[i]= new int [numRighe];
for (i=0; i<numRighe; i++)
for (int j=0; j < numColonne; j++)
q[i][j]=p[j][i];
for (int i=0; i<numRighe; i++)
delete [] p[i];
delete [] p;
int temp=numRighe;
numRighe=numColonne;
numColonne=temp;
p=q;
}
//verifica che sia diagonale
Matrice::diagonale(){
bool check=true;
if (numRighe!=numColonne)
check=false;
else {
for (int i=0; i<numRighe && check==true; i++) {
for (int j=0; j<numRighe && check==true; j++) {
if (i!=j && p[i][j]!=0)
check=false;
}
}
}
if (check==true)
cout << "La matrice è diagonale" << endl;
if (check==false)
cout << "La matrice non è diagonale" << endl;
}
//stampa della matrice
Matrice::stampa(){
for (int i=0; i<numRighe; i++){
for (int j=0; j<numColonne; j++)
cout << p[i][j] << " ";
cout << endl;
}
}