关于友元函数和向前声明的问题,编译通不过,大侠们帮忙看下
程序源代码如下:
定义了两个类 Vector和Matrix,做矩阵乘向量的乘法。
在Vector类和Matrix类定义中均声明multipley函数为友元,但是好像编译提示友元函数不能访问Vector类的私有数据。
chapter09_04_vector.h
如下:
#ifndef HEADER_VECTOR
#define HEADER_VECTOR
class Matrix;
class Vector{
int* v;
int sz;
public:
Vector(int i=1);
Vector(const Vector& vec);
int& operator[](int);
int size(){ return sz; }
void display();
friend Vector mutiply(const Matrix&,const Vector&);
~Vector();
};
#endif //HEADER_VECTOR
chapter09_04_vector.cc
如下:
#include "chapter09_04_vector.h"
#include <iostream>
//-------------------------------------
Vector::Vector(int i):sz(i){
if(i <=0){
std::cerr < <"Bad Vector size.\n";
exit(1);
}
v = new int[sz];
for(int i=0; i <sz; ++i)
v[i] = 0;
}
Vector::Vector(const Vector& vec){
sz = vec.sz;
v = new int[sz];
for(int i=0; i <sz; ++i)
v[i] = vec.v[i];
}
int& Vector::operator[](int i){
if(i <=0 || i>=sz){
std::cerr < <"Vector index out of range.\n";
exit(1);
}
return v[i];
}
void Vector::display(){
for(int i=0; i <sz; ++i)
std::cout < <v[i] < <" ";
std::cout < <"\n";
}
Vector::~Vector(){
delete[] v;
}
chapter09_04_matrix.h
如下:
#ifndef HEADER_MATRIX
#define HEADER_MATRIX
#include "chapter09_04_vector.h"
class Vector;
class Matrix{
int szl,szr;
int* m;
public:
Matrix(int l=1,int r=1);
Matrix(const Matrix& m);
int sizeL(){ return szl; }
int sizeR(){ return szr; }
int& elem(int,int);
~Matrix();
friend Vector multiply(const Matrix& m, const Vector& vec);
};
#endif //HEADER_MATRIX
chapter09_04_matrix.cc
如下:
#include "chapter09_04_matrix.h"
#include <iostream>
//-------------------------------------
Matrix::Matrix(int l,int r):szl(l),szr(r){
if(l <=0 || r <=0){
std::cerr < <"Bad Matrix size.\n";
exit(1);
}
m = new int[szl*szr];
for(int i=0; i <szl*szr; ++i)
m[i] = 0;
}
Matrix::Matrix(const Matrix& ma){
szl = ma.szl;
szr = ma.szr;
m = new int[szl*szr];
for(int i=0; i <szl*szr; ++i)
m[i] = ma.m[i];
}
int& Matrix::elem(int i,int j){
if(i <=0 || i>=szl || j <=0 || j>=szr){
std::cerr < <"Matrix index out of range.\n";
exit(1);
}
return m[i*szr+j];
}
Matrix::~Matrix(){
delete[] m;
}
Vector multiply(const Matrix& m,const Vector& vec){
if (m.szr != vec.sz){
std::cerr < <"Bad multiply Matrix with Vector.\n";
exit(1);
}
Vector r(vec);
for(int i=0; i <m.szl; ++i)
for(int j=0; j <m.szr; ++j)
r.v[i] += m.m[i*m.szr+j]*vec.v[j];
return r;
}
主程序 chapter09_04.cc
如下:
#include "chapter09_04_vector.h"
#include "chapter09_04_matrix.h"
#include <iostream>
#include <fstream>
using namespace std;
//-------------------------------------
int main(){
ifstream ifs("chapter09_04_abc.txt");
int x,y;
ifs>>x>>y;
Matrix ma(x,y);
for(int i=0; i <x; ++i)
for(int j=0; j <y; ++j)
ifs>>ma.elem(i,j);
ifs>>x;
Vector ve(x);
for(int i=0; i <x; ++i)
ifs>>ve[i];
multiply(ma,ve).display();
}
我的makefile是这样写的:
chapter09_04:chapter09_04.o chapter09_04_vector.o chapter09_04_matrix.o
g++ -o chapter09_04
chapter09_04.o:chapter09_04.cc chapter09_04_vector.h chapter09_04_matrix.h
g++ -c chapter09_04.cc
chapter09_04_vector.o:chapter09_04_vector.h chapter09_04_vector.cc
g++ -c chapter09_04_vector.cc
chapter09_04_matrix.o:chapter09_04_matrix.cc chapter09_04_matrix.h
g++ -c chapter09_04_matrix.cc
运行make编译,给出如下提示的错误:
g++ -c chapter09_04_matrix.cc
chapter09_04_vector.h: In function `Vector multiply(const Matrix&, const Vector&)':
chapter09_04_vector.h:10: error: `int Vector::sz' is private
chapter09_04_matrix.cc:36: error: within this context
chapter09_04_vector.h:9: error: `int*Vector::v' is private
chapter09_04_matrix.cc:43: error: within this context
chapter09_04_vector.h:9: error: `int*Vector::v' is private
chapter09_04_matrix.cc:43: error: within this context
E:\Dev-Cpp\Bin\make.exe: *** [chapter09_04_matrix.o] Error 1
大概是说定义的友元函数无法访问Vector类的成员,怎么会这样呢?错在哪里?