c++ - "no match for ' 运算符<< ' (operand types are ' __FILE* ' {aka ' __sFILE64* '} and ' MyObject ' ) error C++"

标签 c++ eclipse makefile cygwin

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




只是一个错字:

“尝试在 C++ 中打印字符串或对象时,'operator<<' (操作数类型为 '__FILE*' {aka '__sFILE64*'} 和 'MyObject' )不匹配”

通过使用 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?)



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

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

原来的

我是 C++ 的新手,我已经与我的编译器和 IDE 设置斗争了 3 天,我终于让 Cygwin 和 Eclipse 和平地构建文件,没有任何路径或 makefile 的 fatal error 。构建器是:CDT 构建器和扫描仪配置构建器。

这可能是相关的,因为下面的代码不会为我的对象或字符串编译,即使我替换为:stdout << "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/.../(包括,c++,c++/backward,c++/x86_64-pc-cygwin)

相关的 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();
};


我的对象.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;
}


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

是多余的。

此外,在类外的类成员的定义中,您必须使用限定名称,例如
MyObject::MyObject(){
    s1 = "hello";
    s2 = "world";
}

该类可以在标题中定义,例如
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& ,而不是 std::iostream& .

关于c++ - "no match for ' 运算符<< ' (operand types are ' __FILE* ' {aka ' __sFILE64* '} and ' MyObject ' ) error C++",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60402539/

相关文章:

c++ - 如何扫描文件夹中的所有文件,包括 VC++ 2008 中的子文件夹?

linux - $(VAR) : in makefile 的含义

python - 树莓派的官方 V4L2 驱动程序,如何将 lrt 标志添加到 makefile?

c++ - 如何将 git commit-number 包含到 C++ 可执行文件中?

c++ - 在 mousePressEvent() 中更改 QWidget 父级不起作用

c++ - 传递给 std::for_each 时,仿函数能否保留值?

python - 在 Cython 中访问 C++ 类的私有(private)成员变量/函数

c - 带有 switch 语句的交互式菜单

eclipse - 尝试在 tomcat 上部署我的 Web 应用程序的 war 文件时出现 HTTP 状态 404 错误

java - 如何为新的 java 项目生成 build.xml?