Member Reference base type 'Matrix(Vector &, Vector &) is not a structure or union

点点圈 提交于 2020-01-06 08:38:27

问题


When I attempt to use the void print function I receive the error "Member Reference base type 'Matrix(Vector &, Vector &) is not a structure or union". This program is supposed to take in values from the user and store them in the data array then it takes the Vector a and Vector b and does vector multiplication to print Matrix A.

    #include <iostream>
 using namespace std;

 const int rows=3;
 const int columns=3;
 const int elements=3;


 class Vector{
 private:
    double data[elements];
 public:
     Vector();
     void read();
     double get_element(int);
 };
 Vector::Vector(){
     int i=0;
     while(i<elements){data[i++]=0;}
 }
 Vector a;
 Vector b;
 Vector c;
 Vector d;
 void Vector::read(){
      int j=0;
      cout<<"Enter "<<elements<<" elements of vector a"<<endl;
     while(j<elements){cin>>data[j++];}


    }
    double Vector:: get_element(int n){
        while(n<elements)
            return data[n];

    }




    class Matrix {
            private:
                double data [rows*columns];
            public:
                Matrix(Vector &, Vector &);
                void add (const Matrix &);
                void mult (double);
                double trace();
                double norm();
                void print ();

    };

    Matrix::Matrix(Vector &, Vector &){
        int d,f;
                for (d=0; d<(rows); d++){
                    for (f=0; f<columns;f++){
                data[d*f]=a.get_element(d)*b.get_element(f);
    }
        }

    }
    Matrix A (Vector &a, Vector &b);
    Matrix B (Vector &c, Vector &d);

    void Matrix::print(){

        cout.precision(3);
                for (int i=0; i<rows; i++) {
                    cout << endl;
            for (int j=0; j<columns; j++) {
                cout << " " << data[i*j];
            }
        //This is printing Matrix A.
        }

    }

    int main(){
            a.read();
            b.read();
            c.read();
            d.read();
            A.print();
            //The error occurs here.
           return 0;
    }

回答1:


if you're trying to define A and B as global variables, change Matrix A (Vector &a, Vector &b); Matrix B (Vector &c, Vector &d);

to Matrix A (a, b); Matrix B (c, d);




回答2:


A and B are function names. The functions were declared as

   Matrix A (Vector &a, Vector &b);
   Matrix B (Vector &c, Vector &d);

in these declarations a and b are considered as function parameters.

So in this statement

A.print();

the compiler processes A as a function name.

I think you meant

   Matrix A (a, b);
   Matrix B (c, d);

EDIT: Here is an answer to your comment.

Class Vector has default constructor

 Vector::Vector(){
     int i=0;
     while(i<elements){data[i++]=0;}
 }

that sets all elements if data member data to zero.

You created objects of type Vector using the default constructor

 Vector a;
 Vector b;
 Vector c;
 Vector d;

and after that you created objects A and B of type Matrix using these Vectors.

Matrix A (a, b);
Matrix B (c, d);

So the objects contain zeroes.

You should remove these definitions and write in main

int main()
{
        Vector a, b, c, d;

        a.read();
        b.read();
        c.read();
        d.read();

        Matrix A( a, b );
        Matrix B( c, d );

        A.print();

        return 0;
}

By the way constructor

Matrix::Matrix(Vector &, Vector &){
    int d,f;
            for (d=0; d<(rows); d++){
                for (f=0; f<columns;f++){
            data[d*f]=a.get_element(d)*b.get_element(f);
}
    }

is invalid. It has two parameters but they are not used in the body of the constructor. Also local variables d and f are not initialized.



来源:https://stackoverflow.com/questions/23070741/member-reference-base-type-matrixvector-vector-is-not-a-structure-or-uni

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!