不匹配 operator= 错误 [英] No match for operator= error

查看:87
本文介绍了不匹配 operator= 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 C++ 程序的片段.下面给出了重载的 operator= 符号.在 main 方法中创建了 stringstream 类型的数组,我想比较该数组的内容.

This is a snippet of c++ program. Below are given overloaded operator= sign. In the main method is created stringstream type array and I want to compare the contents of that array.

*.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)

请问我的代码有什么问题?

What is wrong in my code, please?

推荐答案

这个问题与Assessment3无关.将 array[i] = value; 更改为 array[i] <<值;.

This problem has nothing to do with Assessment3. Change array[i] = value; to array[i] << value;.

这篇关于不匹配 operator= 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆