"no match for 'operator<<' (operand types are '__FILE*' {aka '__sFILE64*'} and 'MyObject' ) error C++"

"no match for 'operator<<' (operand types are '__FILE*' {aka '__sFILE64*'} and 'MyObject' ) error C++"

只是一个错字:

"no match for 'operator<<' (operand types are '__FILE*' {aka '__sFILE64*'} and 'MyObject' ) error when trying to print String or Object in C++"

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

但包含 Eclipse 故障排除:

(Eclipse) sources.mk not finding and including all subdirectories (Solved) by renaming a folder named headers to r_headers, deleting the sources.mk and refreshing, and then added another folder called a_test. I don't know why it wouldn't work even by closing eclipse before adding another folder, but it magically started finding all subdirectories. (Note: thjis was after using this fix: How to solve "Unresolved inclusion: <iostream>" in a C++ file in Eclipse CDT?)

我在任何地方都没有看到,幸好我修好了它。

感谢那些在我疲惫的恐慌中花时间向我指出打字错误的人,我非常感谢,我太忙于 ide 的麻烦了!

原创

我是 C++ 的新手运行,我一直在与我的编译器和 IDE 设置斗争 3 天,我终于有了 Cygwin 和 Eclipse 和平地构建文件,没有任何致命的路径或 makefile 错误。这些构建器是:CDT 构建器和扫描仪配置构建器。

这可能是相关的,因为下面的代码不会为我的 object 或字符串编译,即使我替换为: stdout << "hello";直接还是returns 无效的 ope运行ds 类型 '__FILE*' {aka '__sFILE64*'} 和 'const char [6]' 到二进制 'operator<<'

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

我试过的:

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

-安装 eclipse for c++ 和 vers is:4.14.0

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

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

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

相关的 MyObject 片段

(代码本身比较复杂,这纯粹是一些片段,但我只是想知道错误是否出在某个地方真的很简单,我只是没有得到一些非常基本的东西!)

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;
}

main.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

}

非常感谢,这几天实在是太没效率了,因为我什么都做不了!

初学者有错字

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);

是多余的。

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

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

可以在header中定义class like

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

Class 成员可以这样定义

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

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

和运算符 << 可以在 header 中定义,如

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

请注意,它应该是 std::ostream&,而不是 std::iostream&