这是一个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)
我的代码有什么问题吗?
答案 0 :(得分: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";
}
答案 1 :(得分:1)
此问题与Assessment3
无关。将array[i] = value;
更改为array[i] << value;
。