2011-12-03 98 views
1

這是C++程序的一部分。下面給出了重載的operator = sign。在主要方法是創建stringstream類型的數組,我想比較該數組的內容。運營商不匹配=錯誤

* cpp文件:

template class Assessment3<stringstream>; 

template <class T> Assessment3<T> & Assessment3<T>:: operator=(const Assessment3<T>& refer){ 
    if(this != &refer){ 
     for(int x = 0; x < size; x++){ 
      this->array[x]= refer.array[x]; 
     } 
    } 
    return *this; 

} 

頭文件:

#include <string> 

using namespace std; 

#ifndef ASSESSMENT3_HPP 
#define ASSESSMENT3_HPP 

template <class T> class Assessment3 { 
    friend ostream& operator<< (ostream& os, const Assessment3<T>& assess){// overloads << operator 
     os << assess.calls << assess.swaps << assess.array; 
    return os; } 
public: 
    Assessment3(); 
    Assessment3(const Assessment3& orig); 
    ~Assessment3(); 
    bool bubbleSort(T * array, int size, int & calls, int & swaps); 
    void addition(T * array, int size); 
    void copy(const Assessment3 &orig); 
    Assessment3 & operator=(Assessment3<T> & other); // overloaded = sign 
    bool operator == (Assessment3<T> assess) const; 
    bool operator > (Assessment3<T> assess); 
    bool operator < (Assessment3<T> assess); 
    Assessment3<T> & operator=(const Assessment3<T> & refer); // overloaded = sign 

private: 
    T * array; 
    int calls; 
    int swaps; 
    int size; 
}; 

#endif /* ASSESSMENT3_HPP */ 

主要方法:

Assessment3 <stringstream> defaultObject; 

stringstream * array = new stringstream[4]; 
    stringstream so; 
    int i = 0; 
    string value=""; 
    for(char x = 'a'; x < 'e'; x++){ 
     so << x + "Bill Gates";   
     so >> value; 
     array[i] = value; 
     i++; 
    } 
    defaultObject.addition(array, 4); 

它引發以下錯誤:

g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/Run.o.d -o build/Debug/Cygwin-Windows/Run.o Run.cpp 
Run.cpp: In function `int main(int, char**)': 
Run.cpp:65: error: no match for 'operator=' in '*((+(((unsigned int)i) * 188u)) + array) = value' 
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/iosfwd:84: note: candidates are: std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >& std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::operator=(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&) 
make[2]: *** [build/Debug/Cygwin-Windows/Run.o] Error 1 
make[1]: *** [.build-conf] Error 2 
make: *** [.build-impl] Error 2 
make[2]: Leaving directory `/cygdrive/g/Aristotelis/C++/assessment3' 
make[1]: Leaving directory `/cygdrive/g/Aristotelis/C++/assessment3' 

BUILD FAILED (exit value 2, total time: 3s) 

請問我的代碼有什麼問題?

回答

1

此問題與Assessment3無關。將array[i] = value;更改爲array[i] << value;

1

您不能將std::string分配給std::stringstream

array[i] = value; //line 65 right? 

stringstream * array = new stringstream[4]; //same thing as you wrote 
int i = 0; 
for(char x = 'a'; x < 'e'; x++, i++){ 
    array[i] << x << "Bill Gates"; 
}