成员引用基类型'Matrix(Vector& Vector; amp;)不是结构或联合 [英] Member Reference base type 'Matrix(Vector &, Vector &) is not a structure or union

查看:578
本文介绍了成员引用基类型'Matrix(Vector& Vector; amp;)不是结构或联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用void print函数时,我收到错误Member Reference base typeMatrix(Vector& Vector; amp;)不是一个结构或联合。该程序应该从用户接收值,并将它们存储在数据数组中,然后它接受向量a和向量b,并进行向量乘法以打印矩阵A.

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;
    }


推荐答案

A和B是函数名。函数声明为

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

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

在这些声明中,a和b被视为函数参数。

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

在此语句中

A.print();

编译器将A作为函数名处理。

the compiler processes A as a function name.

我认为你的意思是

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

编辑:这是您的评论的答案。

Here is an answer to your comment.

Class Vector具有默认构造函数

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.

使用默认构造函数创建Vector类型的对象

You created objects of type Vector using the default constructor

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

,然后使用这些向量创建Matrix类型的对象A和B.

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

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

因此对象包含零。

您应该删除这些定义并写入主

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;
}

顺便说一句,

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);
}
    }

无效。它有两个参数,但它们不在构造函数的主体中使用。此外,局部变量d和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.

这篇关于成员引用基类型'Matrix(Vector&amp; Vector; amp;)不是结构或联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆