gpt4 book ai didi

c++ - 不匹配 operator= 错误

转载 作者:行者123 更新时间:2023-11-30 04:31:06 25 4
gpt4 key购买 nike

这是一段C++程序。下面给出了重载的 operator= 符号。在 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)

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

最佳答案

此问题与Assessment3无关.更改 array[i] = value;array[i] << value; .

关于c++ - 不匹配 operator= 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8371957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com