user3116147
user3116147

Reputation: 11

Function undeclared(first use this function)

i'm working on Matrix class,but i cant figure it out why i get this error since i already defined problematic function(Dopisi()).This is what i have so far:

This is what i have so far:

#include<IOStream>
using namespace std;

class Matrica{

private:int Duzina;
    int Sirina;
    int** pok;

public:
    Matrica(){//default constructor
        Duzina=0;
        Sirina=0;
        int i,j;
        pok=new int* [Duzina];
        for(i=0;i<Duzina;i++){
            pok[i]=new int[Sirina];

        }
    }
    Matrica(int duz,int sir){//constructor
        Duzina=duz;
        Sirina=sir;
        int i,j;
        pok=new int* [Duzina];
        for(i=0;i<Duzina;i++){
            pok[i]=new int[Sirina];
            for(j=0;j<Sirina;j++){
                pok[i][j]=0;
            }
        }
    }
    void Dodaj(int i,int j){//add one element
        cout<<"Unesite element ["<<i<<"]["<<j<<"]"<<endl;
        cin>>pok[i][j];
    }
    void Popuni(){//fill entire matrix
        int i,j;
        for(i=0;i<Duzina;i++)
            for(j=0;j<Sirina;j++)
                Dodaj(i,j);
    }
    Matrica(Matrica& mat){//copy constructor
        Duzina=mat.Duzina;
        Sirina=mat.Sirina;
        int i,j;
        pok=new int* [Duzina];
        for(i=0;i<Duzina;i++){
            pok[i]=new int[Sirina];
            for(j=0;j<Sirina;j++){
                pok[i][j]=mat.pok[i][j];
            }
        }
    }
    ~Matrica(){}
    int max(int a,int b){//max value
        if(a>b)return a;
        else
            return b;
    }
    void Ispis(){//cout
        int i,j;
        for(i=0;i<Duzina;i++){
            for(j=0;j<Sirina;j++){
                cout<<pok[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    Matrica& operator =(const Matrica& x){
        for(int i=0;i<Duzina;i++)
            delete pok[i];

        Duzina=x.Duzina;
        Sirina=x.Sirina;
        for(int i=0;i<Duzina;i++){
            for(int j=0;j<Sirina;j++){
                pok[i][j]=x.pok[i][j];
            }
        }
        return *this; 
    }
    Matrica Dopisi(const Matrica a,const Matrica b);
};
Matrica Matrica::Dopisi(const Matrica a,const Matrica b){//function must take two      matrices 
    int i,j,duz,sir;
    duz=max(a.Duzina,b.Duzina);
    sir=a.Sirina+b.Sirina;
    Matrica temp(duz,sir);
    for(i=0;i<a.Duzina;i++){
        for(j=0;j<a.Sirina;j++){
            temp.pok[i][j]=a.pok[i][j];
        }
    }
    for(i=0;i<b.Duzina;i++){
        for(j=0;j<b.Sirina;j++){
            temp.pok[i][j+a.Sirina]=b.pok[i][j];
        }
    }
    temp.Ispis();
    // return temp;
}
int main(){
    Matrica mat1(3,3);
    mat1.Popuni();
    mat1.Ispis();
    Matrica mat2(4,4);
    mat2.Popuni();
    mat2.Ispis();
    mat2=mat1;
    mat2.Ispis();
    Matrica mat3;
    mat3=Dopisi(mat1,mat2);
    system("pause");
    return 0;
}

P.S.Names of variables and methods are in Serbian,hope you'll figure it out.Thanks in advance

Upvotes: 0

Views: 313

Answers (5)

Some programmer dude
Some programmer dude

Reputation: 409356

Dopisi is a member function in the Matrica class. If you want it to be a free-standing function it should be declared or defined outside the class.

If you want it to be able to access private member, then declare it as a friend function, or make it a static member function (but then you have to call it as Matrica::Dopisi(...)).

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

Besides the problem with the function the class definition is invalid. First of all the destructor does not release memory pointed by pok. Also the assignment operator is incorrect.

Matrica& operator =(const Matrica& x){
    for(int i=0;i<Duzina;i++)
        delete pok[i];

    Duzina=x.Duzina;
    Sirina=x.Sirina;
    for(int i=0;i<Duzina;i++){
        for(int j=0;j<Sirina;j++){
            pok[i][j]=x.pok[i][j];
        }
    }
    return *this; 
}

First of all instead of delete pok[i]; shall be delete []pok[i]; Also you did not release used memory and allocate a new memory according to new values of Duzina and Sirina.

So the operator function has undefined behavior and without doubts its work will result in program exception.

Upvotes: 0

user2711915
user2711915

Reputation: 2809

Make the function

public:
static Matrica Dopisi(const Matrica &a,const Matrica &b);

Then you can call it outside the class as

mat3 = Matrica::Dopisi(mat1,mat2);

without needing an instance of the class

Upvotes: 1

gthacoder
gthacoder

Reputation: 2351

You declare Dopisi function as a class method (put it inside your class declaration), but then you call it as a global function. For example, it could be called as mat3.Dopisi(mat1, mat2).

In your case, Dopisi needs to have access to Matrica class, so just make it a friend-function:

...

// in class declaration:
friend Matrica Dopisi(const Matrica a,const Matrica b);

...

// in function definition:
Matrica Dopisi(const Matrica a,const Matrica b){//function must take two      matrices

...

// leave your function call as it is

Upvotes: 0

mathematician1975
mathematician1975

Reputation: 21351

You are calling Dopisi as if it were a regular function. It is a member function and so needs to be called on an instance of class Matrica. The actual style of this function would indicate that it would be better as a non-member function as calling this function as currently, it requires an instance of Matrica to call on but also takes 2 instances of Matrica as arguments and also returns a fourth. Thus you have 4 instances in total. It seems more appropriate to use a nonmember function taking t instances of Matrica and returning the result.

This would of course also require getter/setter functions for the private members of the class that are accessed within that function.

Upvotes: 0

Related Questions