Нет совпадения для оператора = ошибка - PullRequest
1 голос
/ 04 декабря 2011

Это фрагмент программы на С ++. Ниже приведены перегруженные операторы = знак. В методе main создается массив типа 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)

Что не так в моем коде, пожалуйста?

Ответы [ 2 ]

1 голос
/ 04 декабря 2011

Эта проблема не имеет ничего общего с Assessment3.Измените array[i] = value; на array[i] << value;.

1 голос
/ 04 декабря 2011

Вы не можете назначить 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";
}
...