【问题标题】:"no match for 'operator<<' (operand types are '__FILE*' {aka '__sFILE64*'} and 'MyObject' ) error C++" [closed]“'operator<<' 不匹配(操作数类型是 '__FILE*' {aka '__sFILE64*'} 和 'MyObject' )错误 C++” [关闭]
【发布时间】:2020-06-09 15:42:37
【问题描述】:

只是一个错字:

“尝试在 C++ 中打印字符串或对象时出现 'operator

通过使用 cout 解决,而不是 stdout。

但包含 Eclipse 故障排除:

(Eclipse) sources.mk 找不到并包含所有子目录(已解决),方法是将名为 headers 的文件夹重命名为 r_headers,删除 sources.mk 并刷新,然后添加另一个名为 a_test 的文件夹。我不知道为什么即使在添加另一个文件夹之前关闭 eclipse 也不起作用,但它神奇地开始查找所有子目录。 (注意:这是在使用此修复后:How to solve "Unresolved inclusion: <iostream>" in a C++ file in Eclipse CDT?

我在任何地方都没有看到,只是运气好我修好了它。

感谢那些在我疲倦的恐慌中花时间向我指出那个错字的人,我非常感谢,我太沉浸在我的 ide 麻烦中了!

原创

我是 C++ 的新手,我已经与我的编译器和 IDE 设置斗争了 3 天,我终于让 Cygwin 和 Eclipse 和平地构建文件,没有任何路径或 makefile 的致命错误。构建器是:CDT Builder 和 Scanner Configuration Builder。

这可能是相关的,因为下面的代码不会为我的对象或字符串编译,即使我替换为: stdout &lt;&lt; "hello"; 直接还是返回 '__FILE*' {aka '__sFILE64*'} 和 'const char [6]' 类型的无效操作数到二进制 'operator

我认为这个设置是有效的,因为我初始化了一个 hello world 文件并且它运行了,但是现在我的所有实际代码都在 src 文件夹中带有子目录,它似乎不再起作用了。

我尝试过的:

-不得不在学校使用 VisualStudio。它在我的笔记本电脑上完全停止工作。我遇到了 stdout 问题,这导致我质疑我的代码,但似乎是 Visual Studio 更改了标志的数据结构,加上 make 文件是一场噩梦,无法弄清楚将它们的 .o 文件放在哪里,所以我决定在另一台计算机上安装 eclipse 看看是否更容易。

-安装了 c++ 的 eclipse,版本为:4.14.0

-我将 cygwin gcc、gnu、debug 等更新到最新版本

-在常规属性中启用自动刷新 -我通过将 headers 文件夹重命名为 r_headers 来修复 sources.mk 不包括子目录,删除了 sources.mk 并刷新了很多,然后添加了另一个名为 a_test 的文件夹。我不知道为什么在添加另一个文件夹之前它不起作用,但它神奇地开始查找所有子目录。

-在我的包含文件中,它看起来像是包含了所有必要的东西(Windows 10 64 位) c:/cygwin64/usr/(包括/,包括/w32api) c:/cygwin64/lib/gcc/.../ (include,c++,c++/backward,c++/x86_64-pc-cygwin)

相关的MyObject sn-ps

(代码本身更复杂,这纯粹是由sn-ps组成,但我只想知道错误是否出在非常简单的地方,我只是没有得到非常基本的东西!)

MyObject.h

#include <iostream>
#include <string>
using std::string;

class MyObject {
private:
    string s1;
    string s2;
public:
    MyObject();
    std::ostream& operator<<(std::ostream& os,MyObject const& o);
    string toString();
};

MyObject.cpp

class MyObject{
    string s1,s2;
    MyObject(){
       s1 = "hello";
       s2 = "world";
 }
};

std::ostream& operator<<(std::iostream& os, MyObject const& o) {
    string s = toString();
    os << s;
    return os;
}

string toString() {
    return s1+s2;
}

ma​​in.cpp

#include <iostream>
#include <string>

#include "../r_headers/MyObject.h"

using std::string;
using std::cout;
using std::endl;

void test_print_mo() {
    MyObject o = MyObject();
    //string s= MyObject.toString();
    //stdout << o; **// where I initially made a typo** 
    cout << o; //works, solution

}
int main() {

    test_print_mo();

    //do not delete, make sure window doesn't close
    cin.get();
    return 0; //for compiler

}

非常感谢,这几天效率太低了,因为我什么都不能正常工作!

【问题讨论】:

  • stdout 是一个 C 风格的流,它只是一个 FILE* 对象。你想要std::cout,这是一个与stdout关联的c++ ostream对象。
  • std::string toString() { -> std::string MyObject::toString() {。我不确定这甚至是如何编译的。编译器应该已经正确地指出了这一点。
  • 是啊,其实她有点不对劲。撤回作为一个简单的错字并不能涵盖所有内容。
  • 抱歉错别字,我修正了代码,我一定是因为试图阅读编译器文档而失明,非常感谢
  • 提示:如果您删除了描述实际问题所不需要的所有内容,人们通常更有可能感兴趣。现在很难说出你在问什么——或者你是否已经解决了问题——如果你解决了问题,是弗拉德的回答做到了吗?如果是,请接受答案。

标签: c++ eclipse makefile cygwin


【解决方案1】:

对于初学者来说有一个错字

class MyObject {
private:
    string s1;
    sting s2;
    ^^^^^
    //...

这个运算符

std::ostream& operator<<(MyObject const& o);

和这个运算符不一样

std::ostream& operator<<(std::iostream& os, MyObject const& o) {
    std::string s = toString();
    os << s;
    return os;
}

而且好像是这个成员函数

std::ostream& operator<<(MyObject const& o);

是多余的。

此外,在类外的类成员的定义中,您必须使用限定名称,例如

MyObject::MyObject(){
    s1 = "hello";
    s2 = "world";
}

类可以在header中定义

class MyObject {
private:
    string s1;
    sting s2;
public:
    MyObject();
    std::string toString() const;
};

类成员可以这样定义

MyObject::MyObject() : s1( "hello" ), s2( "world" ){
}

std::string MyObject::toString() const {
    return s1 + " " + s2;
}

和操作符一样在头部定义

inline std::ostream & operator<<(std::ostream& os, MyObject const& o) {
    return os << o.toString();
}

请注意,它应该采用std::ostream&amp;,而不是std::iostream&amp;

【讨论】:

  • 非常感谢,我没有注意到ostream&!我的印象是在头文件中声明内联函数被认为是不好的做法,这对于重载运算符有什么不同吗?
猜你喜欢
  • 2021-12-17
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多