关于友元函数和向前声明的问题,编译通不过,大侠们帮忙看下

paobuzou 2009-04-17 03:49:56
程序源代码如下:

定义了两个类 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类的成员,怎么会这样呢?错在哪里?
...全文
127 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
paobuzou 2009-04-17
  • 打赏
  • 举报
回复
唉,竟然真的是拼写错了,现在通过了。谢谢大家,这么没技术含量的问题,马上结贴散分,呵呵
liubuweiright 2009-04-17
  • 打赏
  • 举报
回复
学习...
beyond071 2009-04-17
  • 打赏
  • 举报
回复
楼主 在Vector中的声明拼写错了friend Vector multiply(const Matrix& m,const Vector& vec); //multiply
jame2001 2009-04-17
  • 打赏
  • 举报
回复
Vector::sz is private
Vector::v is private

改放到public
cheng_fengming 2009-04-17
  • 打赏
  • 举报
回复
这些东西我现在还不是很懂,是不是因为Vector类是私有变量呢?
在定义Vector类里面的变量的时候 public:下面的需要在别处使用的变量是不是应该用{}括起来?
class Vector{
int* v;
int sz;
public:
{Vector(int i=1);
Vector(const Vector& vec);
int& operator[](int);
int size(){ return sz; }:
}
如果是错的请别见怪 呵呵!

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧