错误:"运算符<<不匹配
error: no match for ‘operator<<’
这是我的运算符<<实现:
std::ostream& operator<< (std::ostream &out, FileDir &obj) {
out << obj.toString();
return out;
}
我在 FileDir 类声明之后将这一行添加到我的 FileDir 头文件中:
std::ostream& operator<< (std::ostream &out, FileDir &obj);
在我的 FileDirTest 中,为了测试运算符<<,我有以下内容:
assert(cout << t1 == "testFileOne 50kb");
(其中 t1 是文件目录)
这是我得到的错误:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘FileDir’)
assert(cout << t1 == "testFileOne 50kb");
另外,这是完整的头文件:
#include <sstream>
class FileDir {
public:
FileDir();
FileDir(std::string nameVal, long sizeVal = 4, bool typeVal = false);
FileDir(const FileDir &obj);
~FileDir(); // destructor
long getSize() const;
std::string getName() const;
bool isFile() const;
std::string rename(std::string newname);
long resize(long newsize);
std::string toString();
bool operator== (const FileDir &dir1);
bool operator<(const FileDir &obj);
private:
std::string name;
long size;
bool type;
};
std::ostringstream& operator<< (std::ostringstream &out, FileDir &obj);
这是我的 toString():
std::string FileDir::toString()
{
std::string whatever;
std::stringstream converter;
converter << size;
converter >> whatever;
std::string combined;
if (type == false) {
combined = name + " " + whatever + "kb";
}
if (type == true) {
combined = name + "/" + " " + whatever + "kb";
}
return combined;
}
这是导致错误的FileDirTest部分:
static void OperatorsTest() {
FileDir t1("testFileOne", 50, false);
FileDir t2("testDirectory", 100, true);
FileDir t3("testFileTwo", 20, false);
assert(t1 < t2);
assert(t3 < t2);
std::ostringstream oss;
oss << t1;
assert(oss.str() == "testFileOne 50kb");
}
更新:事实证明我的头文件以前已经编译过,所以我对它所做的任何更改都没有通过。一旦我删除了.gch文件,FileDirTest终于编译了。
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 模板专用化与派生类不匹配