Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建包含模板的静态库_C++_Templates - Fatal编程技术网

C++ 创建包含模板的静态库

C++ 创建包含模板的静态库,c++,templates,C++,Templates,我正在使用模板为矩阵操作开发静态库,我们可以在其中对各种数据类型(short、int、long、long、float、double、longdouble)执行基本的矩阵操作。但据我所知,在使用模板时,类声明和定义不能保存在单独的文件(.h和.cpp文件)中。如果是这种情况,如何在不公开实现代码的情况下创建库(只需向其他用户提供类语法声明和库)。这就是我到目前为止写的 MatrixClass.h #ifndef __MATRIXCLASS_H__ #define __MATRIXCLASS_H__

我正在使用模板为矩阵操作开发静态库,我们可以在其中对各种数据类型(short、int、long、long、float、double、longdouble)执行基本的矩阵操作。但据我所知,在使用模板时,类声明和定义不能保存在单独的文件(.h和.cpp文件)中。如果是这种情况,如何在不公开实现代码的情况下创建库(只需向其他用户提供类语法声明和库)。这就是我到目前为止写的

MatrixClass.h

#ifndef __MATRIXCLASS_H__
#define __MATRIXCLASS_H__

#include<iostream>
#include<vector>


template<typename T>
class CMatrix { 
public:
    CMatrix();
    ~CMatrix();

    unsigned long rows;
    unsigned long cols;

    std::vector<std::vector<T> > data;

    void setMatrixSize(unsigned long rows, unsigned long cols);
    void printMatrix();
    void add(CMatrix<T>& A, CMatrix<T>& B);
    void sub(CMatrix<T>& A, CMatrix<T>& B);
    void mul(CMatrix<T>& A, CMatrix<T>& B);
};


template<typename T>
CMatrix<T>::CMatrix() {
    this->rows = 0;
    this->cols = 0;

    data.clear();
}


template<typename T>
CMatrix<T>::~CMatrix() {
    this->rows = 0;
    this->cols = 0;

    data.clear();
}


template<typename T>
void CMatrix<T>::setMatrixSize(unsigned long rows, unsigned long cols) {
    this->rows = rows;
    this->cols = cols;

    data.clear();
    data.resize(cols, std::vector<T>(rows, 0));
}


template<typename T>
void CMatrix<T>::printMatrix() {
    for(unsigned long i = 0; i < rows; i++) {
        for(unsigned long j = 0; j < cols; j++) {
            std::cout << data.at(i).at(j) << "\t";
        }
        std::cout << std::endl;
    } 
}


template<typename T>
void CMatrix<T>::add(CMatrix<T>& A, CMatrix<T>& B) {
    if((A.rows == B.rows) && (A.cols == B.cols)) {
        for(unsigned long i = 0; i < rows; i++) {
            for(unsigned long j = 0; j < cols; j++) {
                data.at(i).at(j) = A.data.at(i).at(j) + B.data.at(i).at(j);
            }
        }
    }
}


template<typename T>
void CMatrix<T>::sub(CMatrix<T>& A, CMatrix<T>& B) {
    if((A.rows == B.rows) && (A.cols == B.cols)) {
        for(unsigned long i = 0; i < rows; i++) {
            for(unsigned long j = 0; j < cols; j++) {
                data.at(i).at(j) = A.data.at(i).at(j) - B.data.at(i).at(j);
            }
        }
    }
}


template<typename T>
void CMatrix<T>::mul(CMatrix<T>& A, CMatrix<T>& B) {
    if((A.cols == B.rows) && (rows == A.rows) && (cols == B.cols)) {
        for(unsigned long i = 0; i < A.rows; i++) {
            for(unsigned long j = 0; j < B.cols; j++) {
                for(unsigned long k = 0; k < A.cols; k++) {
                    data.at(i).at(j) += A.data.at(i).at(k) * B.data.at(k).at(j);
                }
            }
        }
    }
}

#endif
\ifndef\uu矩阵类__
#定义矩阵类__
#包括
#包括
模板
类CMatrix{
公众:
CMatrix();
~CMatrix();
无符号长行;
无符号长列;
std::矢量数据;
void setMatrixSize(无符号长行、无符号长列);
无效打印矩阵();
无效添加(CMatrix&A、CMatrix&B);
无效子公司(CMatrix&A、CMatrix&B);
无效mul(CMatrix&A、CMatrix&B);
};
模板
CMatrix::CMatrix(){
此->行=0;
这->cols=0;
data.clear();
}
模板
CMatrix::~CMatrix(){
此->行=0;
这->cols=0;
data.clear();
}
模板
void CMatrix::setMatrixSize(无符号长行、无符号长列){
这->行=行;
这->cols=cols;
data.clear();
调整大小(cols,std::vector(行,0));
}
模板
void CMatrix::printMatrix(){
for(无符号长i=0;istd::cout您必须显式实例化您的类型。用户将无法将您的库用于您显式实例化的任何其他类型。无关:
\uuuu MATRIXCLASS\u H\uu
是保留标识符(因为它以两个下划线开头).下划线+大写字母也是保留的。不要使用这样的名称。与您的问题无关,但请注意,所有以双下划线开头的符号(例如
\uuuu MATRIXCLASS\uu H\uu
)在所有范围内都是保留的,您不应该自己定义此类符号。有关更多详细信息,请参阅例如。如果您尝试此操作,您将遇到此问题:@Harry。不,因为问题已结束。但是请阅读我上面链接的文章。这里有一个示例。