| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 224 人关注过本帖
标题:matrix类实现的权限问题
取消只看楼主 加入收藏
学C的菜鸡
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2025-4-14
收藏
 问题点数:20 回复次数:0 
matrix类实现的权限问题
程序代码:
class matrix {
private:
    vector<vector<float>>data;
    int rows;
    int cols;
public:
    matrix(int _rows, int _cols) :rows(_rows), cols(_cols), data(rows, vector<float>(cols, 0.0f)) {}
    matrix(const vector<vector<float>>& input) :data(input), rows(input.size()), cols(input[0].size()) {}

    int getCols()const { return cols; }
    int getRows()const { return rows; }
    void setValue(int i, int j, float value) { data[i][j] = value; }
    float& operator()(int i, int j) { return data[i][j]; }
    const float& operator()(int i, int j)const { return data[i][j]; }

    vector<float> operator *(const vector<float>& vec) const {
        if (cols != vec.size()) {
            throw invalid_argument("Matrix and vector dimensions do not match!");
        }
        vector<float> result(rows, 0.0f);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result[i] += data[i][j] * vec[j];
            }
        }
        return result;
    }
};
matrix mat(3,3);
mat(0,0) = 1.0f;
为什么我这样写通过mat(0,0)赋值会报错,通过setValue同样会报错,把data从private拿到public又不会报错了
5 天前 17:44
快速回复:matrix类实现的权限问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017613 second(s), 11 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved