2014-11-15 1 views
1

나는 원하는대로 출력을 제공하기 위해 < < 연산자를 오버로드하려고합니다. 나는 다음과 같은 코드를 실행하면오버로드 << 결과가 valgrind 오류

#include<iostream> 

#include<stdlib.h> 

#include<string.h> 

using namespace std; 

template <class type> 
class matrix 
{ 
public: 
    string _name; 
    int _rows, _columns; 
    type** _data; 

    matrix() 
    { 
    _name = "A";// Default name is A 
    _rows = 1; 
    _columns = 1; 
    _data = new type*[_columns]; 
    _data[0] = new type[_rows*_columns]; 
    _data[0][0] = 0;  
    } 

    matrix(int rows, int columns, string name) 
    { 
    _rows = rows; 
    _columns = columns; 
    _name = name; 

    _data = new type*[_columns]; 
    _data[0] = new type[_rows*_columns]; 

    for (int i=0; i<_columns; i++) 
     { 
    _data[i] = _data[0] + i*_rows; 
     } 

    for (int i=0; i<_columns; i++) //Initialise matrix elements to zeros 
     {for (int j=0; j<_rows; j++) 
     { 
     _data[i][j] = 0; 
     } 
     } 
    } 


    ~matrix()   // Destructor 
    { 
    _name = " "; 
    _rows = 0; 
    _columns = 0; 
    delete [] _data[0]; 
    delete [] _data; 
    } 


}; 

template<class type> 
ostream &operator<<(ostream &os, matrix<type> &mat) 
{ 
    string name = mat._name; 
    int columns = mat._columns; 
    int rows = mat._rows; 
    cout<<"Matrix "<<name<<endl; 
    cout<<"Col: "<<columns<<" Row: "<<rows<<endl; 
    for (int i=0; i<rows; i++) 
    {for (int j=0; j<columns; j++) 
    { 
     cout<<mat._data[j][i]; 
     cout<<" "; 
    } 
     cout<<endl; 
    } 
    cout<<endl; 
} 

main(){ 

    // constructor 
    matrix<float> A(2,2, "A"); 
    cout << A << endl; //Problematic 
} 

이 코드가있는 경우 완벽하게 실행 출력하지 않는다 < < ENDL ;. 문제없이 컴파일 < < endl; 그러나 나는 seg 오류 및 다음 valgrind 오류가 발생합니다. 이 오류가

Valgrind의를 밖으로 던져되고있는 이유는 확실하지 않다 :

==3862== Invalid read of size 4 
==3862== at 0x40D4FA7: std::basic_ostream<char, std::char_traits<char> >&   std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) 
==3862== by 0x40D44AD: std::ostream::operator<<(std::ostream& (*)(std::ostream&))  (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) 
==3862== by 0x8048B2E: main (check.cpp:85) 
==3862== Address 0xfffffff5 is not stack'd, malloc'd or (recently) free'd 
==3862== 
==3862== 
==3862== Process terminating with default action of signal 11 (SIGSEGV) 
==3862== Access not within mapped region at address 0xFFFFFFF5 
==3862== at 0x40D4FA7: std::basic_ostream<char, std::char_traits<char> >&  std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>  >&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) 
==3862== by 0x40D44AD: std::ostream::operator<<(std::ostream& (*)(std::ostream&))  (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) 
==3862== by 0x8048B2E: main (check.cpp:85) 
==3862== If you believe this happened as a result of a stack 
==3862== overflow in your program's main thread (unlikely but 
==3862== possible), you can try to increase the size of the 
==3862== main thread stack using the --main-stacksize= flag. 
==3862== The main thread stack size used in this run was 1048576. 
==3862== 
==3862== HEAP SUMMARY: 
==3862==  in use at exit: 38 bytes in 3 blocks 
==3862== total heap usage: 3 allocs, 0 frees, 38 bytes allocated 

PS :

>g++ -g -o a check.cpp 

답변

3

당신은

이 필요합니다 : 나는 다음과 같이 ++ g을 사용하여 CPP 파일을 컴파일하고
return os ; 

과부하 << operator이고대신 os을 사용하십시오. 어떤 ostream 객체

template<class type> 
ostream &operator<<(ostream &os, const matrix<type> &mat) 
           /* ~~~ Use const ref 
           to avoid copies and ensuring 
           you aren't modifying anything on mat object */ 
{ 
    // ... 
     os << "Matrix "<<name <<endl; 
    //~~~ 
     os << mat._data[j][i]; 
    // ~~~ 
    // etc... 
    return os ; 
} 
+0

하나에 클래스 객체를 직렬화하는 inorder를개 모든 장소는 /뿐만 아니라 너무 const를 심판하여 행렬을 걸릴 수 있습니다. 건배. –

+0

안녕하세요! 실제로 문제가 해결되었습니다. OS를 사용하는 것이 이치에 맞지만 다른 경우에는 기묘한 작업을하고있었습니다. @ 토니 : 제안에 감사드립니다. –

+2

@Mathews_M_J : "cout"에 대해 이상한 점은 없다 "operator"'overload 내에서'cout'을 확실히 사용할 수 있지만, 출력은 항상'cout'/표준 출력 스트림으로 가고, 그래서'std :: cerr << m;'또는'my_file << m;''''cout' "이라고 쓰면 실제로 표준 에러 나 파일에서 끝나지 않을 것입니다. 물건을 실제로'os'로 스트리밍하면 호출자가 지정한 스트림으로 이동합니다 .... –