无法使模板类与链接器一起使用

时间:2014-07-03 20:23:06

标签: c++ class templates

我目前正在自学C ++,我正在努力让我的基本模板类的实现进行编译。我写了一个IntMatrix类来实现一个带有整数元素的矩阵,我现在正在尝试扩展该类。代码如下。当我尝试编译它时,我收到错误:

g++ -o hw3 -std=c++11 -Wall -Wextra -ansi -pedantic -Wshadow -Weffc++ -Wstrict-overflow=5 -Wno-unused-parameter -Wno-write-strings hw3.cpp
Undefined symbols for architecture x86_64:
  "Matrix<int>::~Matrix()", referenced from:
      _main in hw3-c5e16c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我将非常感谢任何帮助。代码:

// main.cpp
#include "matrix.ch"
#include <iostream>

int main() {
    Matrix<int> adj(3, 3);
    std::cout << adj;
    return 0;
}

矩阵头文件:

// matrix.h
#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include <iomanip>

using namespace std;

template <typename T>
class Matrix {
public:
    //Default constructor.
    Matrix( );

    //Constructor builds r x c matrix with all zeros.
    Matrix(int r, int c);

    //Class destructor.
    ~Matrix( );

    //Copy constructor.
    Matrix(const Matrix<T>& right);

    void print( );

    //Assignment operator.
    Matrix<T>& operator= (const Matrix<T>& right);

    //Element access operator: M(i,j) is element in row i, column j.
    int& operator() (int i, int j);
    int operator() (int i, int j) const;

    //Define matrix addition: A+B.
    //Assumes dimension(A)=dimension(B).
    friend Matrix<T> operator+ (const Matrix<T>& left, const Matrix<T>& right);

    //Define matrix multiplication: A*B.
    //Assumes (#cols of A) = (#rows of B).
    friend Matrix<T> operator* (const Matrix<T>& left, const Matrix<T>& right);

    //Define matrix powers: A^N = A*A*A...A  (N times).
    //Assumes matrix A is square: #rows = #cols.
    friend Matrix<T> operator^<>(const Matrix<T>& left, int power);

    //Define matrix input and output.
    friend ostream& operator<< (ostream& out, const Matrix<T>& right);
    friend istream& operator>> (istream& in, Matrix<T>& right);

private:
    int rows;   //Number of rows in matrix.
    int cols;   //Number of columns in matrix.
    T* elements;   //Matrix elements as an array, read in raster order.
};

template <typename T>
Matrix<T>::Matrix() {
    rows = 0;
    cols = 0;
    elements = NULL;
}

template <typename T>
Matrix<T>::Matrix (int r, int c) {
    rows = r;
    cols = c;
    elements = new T[r*c];
}

template <typename T>
Matrix<T>::~Matrix () {
    delete[] elements;
}

template <typename T>
void Matrix<T>::print() {
    for (int i = 0; i < rows * cols; i++) {
        cout <<setw(10) << elements[i];
        if ((i+1) % cols == 0) {
            cout << "\n"; // End of row
        }
        return;
    }
}

template <typename T>
int& Matrix<T>::operator() (int i, int j) {
    return elements[i * cols + j];
}

template <typename T>
int Matrix<T>::operator() (int i, int j) const{
    return elements[i * cols + j];
}

template <typename T>
ostream& operator<< (ostream& out, const Matrix<T>& right) {
    for (int i = 0; i < right.rows*right.cols; i++) {
        out << setw(10) << right.elements[i];
        if ((i+1) % right.cols == 0) {
            out << "\n";
        }
    }
    return out;
}

template <typename T>
Matrix<T> operator* (const Matrix<T>& left, const Matrix<T>& right) {
    Matrix<T> A (left.rows, right.cols);
    for (int i = 0; i < left.rows; i++) {
        for (int j = 0; j < right.cols; j++) {
            A(i, j) = 0;
            for (int k = 0; k < left.cols; k++) {
                A(i, j) += left(i, k) * right (k, j);
            }
        }
    }
    return A;
}

template <typename T>
Matrix<T>& Matrix<T>::operator= (const Matrix<T>& right) {
    if (this != &right) {
        rows = right.rows;
        cols = right.cols;
        delete[] elements; // Note that we clear the array before reassigning it.
        elements = new T[rows * cols];
        for (int i = 0; i < rows * cols; i++) {
            elements[i] = right.elements[i];
        }
    }
    return *this;
}

0 个答案:

没有答案