2016-04-23 13 views
0

Ich versuche, eine Matrix-Klasse mit einfachen Operationen zu definieren. Um mit int, float, double Datentyp kompatibel zu sein, verwende ich die Vorlage. Und ich überlade den Operator "< <", um die Matrix zu drucken. Wie auch immer, wenn ich das Programm kompiliere, erhalte ich einen LNK2019 Fehler.Fehler beim Definieren meiner C++ - Matrix-Klasse mit Vorlage Datentyp und Überladung der "<<" -Operator

Fehler 2 Fehler LNK1120: 1 nicht aufgelöste externe
Fehler 1 Fehler LNK2019: nicht aufgelöstes externes Symbol "Klasse std :: basic_ostream> & __cdecl Operator < < (Klasse std :: basic_ostream> &, Klasse Mat const &)" (?? 6 @ YAAAV? $ Basic_ostream @ DU? $ Char_traits @ D @ std @@@ std @@ AAV01 @ ABV? $ Mat @ H @@@ Z) referenziert in Funktion _main

Ich habe versucht, die zu finden Fehler und konnte einfach nicht das Problem herausfinden. Hier

ist das Detail: System infromation: Fenster 10 x 64, Visual Studio 2013

mat.h Datei

#ifndef _MAT_H_ 
#define _MAT_H_ 

#include <iostream> 
#include <ostream> 
#include <sstream> 
#include <vector> 
#include <cstring> 
#include <cassert> 

//implement Mat class in c++ 


template<typename T> 
class Mat{ 

friend std::ostream& operator<<(std::ostream &os, const Mat<T> &m); 
friend std::istream& operator>>(std::istream &is, Mat<T> &m); 

public: 

//construct 
Mat(); 
Mat(size_t i, size_t j); 

//destructor 
~Mat(); 

//access element value 
T& MatVale(size_t i, size_t j); 
const T& MatVale(size_t i, size_t j) const; 

//get row and col number 
const size_t rows() const{ return row; } 
const size_t cols() const{ return col; } 

private: 

size_t row; 
size_t col; 
T* pdata; 
}; 

#endif 

mat.cpp Datei

` 
#include "mat.h" 

using std::cout; 
using std::endl; 
using std::istream; 
using std::ostream; 

template<typename T> 
ostream& operator<<(ostream &os, const Mat<T>&m){ 
    for (int i = 0; i < m.row; i++){ 
     for (int j = 0; j < m.col; j++){ 
      os << m.pdata[i*m.col + j] << " "; 
     } 
     os << std::endl; 
    } 
    os << std::endl; 
    return os; 
} 

template<typename T> 
istream& operator>>(istream &is, Mat<T>&m){ 
    for (int i = 0; i < m.row; i++){ 
     for (int j = 0; j < m.col; j++){ 
      is >> m.pdata[i*m.col + j]; 
     } 
    } 
    return is; 
} 

//construct 
template<typename T> 
Mat<T>::Mat(){ 
    cout << "defalut constructor" << endl; 
    row = 0; 
    col = 0; 
    pdata = 0; 
} 

template<typename T> 
Mat<T>::Mat(size_t i, size_t j){ 
    row = i; col = j; 
    pdata = new T[row*col]; 
    std::memset(pdata, 0, row*col*sizeof(T)); 
} 

//destructor 
template<typename T> 
Mat<T>::~Mat(){ 
    if (pdata) delete[] pdata; 
} 

//access element value 
template<typename T> 
inline T& Mat<T>::MatVale(size_t i, size_t j){ 
    assert(pdata && i>=0 && j>=0 && i < row && j < col); 
    return pdata[i*col + j]; 
} 

template<typename T> 
inline const T& Mat<T>::MatVale(size_t i, size_t j) const{ 
    assert(pdata && i >= 0 && j >= 0 && i < row && j < col); 
    return pdata[i*col + j]; 
} 

int main(){ 

    Mat<int> mat1(7, 2); 
    Mat<int> mat2(7, 2); 

    for (size_t i = 0; i < mat1.rows(); i++){ 
     for (size_t j = 0; j < mat1.cols(); j++){ 
      mat1.MatVale(i, j) = i + j+3; 
      mat2.MatVale(i, j) = 2 * i + j+11; 
     } 
    } 
    std::cout << mat1;//comment this line then it is okay. 
    return 1; 
} 
+0

Ich weiß, wie Sie dieses Problem jetzt lösen. https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Making_New_Friends – Yan

+0

und ich fasste mehrere Methoden zusammen, um dieses Problem zu lösen. siehe hier: http://blog.csdn.net/yc461515457/article/details/51240348 – Yan

Antwort

0

in der Tat, Sie können eine Mitgliedsfunktion verwenden, um einen Druckauftrag auszuführen, und eine Nichtmitglieder-Nicht-Freund-Funktion definieren, die diese Mitgliedsfunktion aufruft. SO, dass Sie mit Friend-Funktion in einer Vorlagenklasse ungültig machen können.

etwas wie folgt aus:

//non-member and non-feriend overload function 
template<typename T> 
std::ostream& operator<<(std::ostream& os, const Mat<T>& m){ 
    m.CoutMat(os);//call member function 
    return os; 
} 

und in der Klassendefinition, haben Sie eine Memberfunktion Druckauftrag zu tun.

//member function to do print job 
void CoutMat(std::ostream& os) const;