Just a typo:
"no match for 'operator<<' (operand types are '__FILE*' {aka '__sFILE64*'} and 'MyObject' ) error when trying to print String or Object in C++"
solved by using cout, not stdout.
but contains Eclipse troubleshooting:
(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?)
that I didn't see anywhere, and it's just luck I fixed it.
Thank you to those who took the time to point that typo out to me in my tired panic, I appreciate it so much I was too wrapped up in my ide troubles!
I am brand new to C++ and I have been fighting with my compiler and IDE set up for 3 days now, I finally have Cygwin and Eclipse building files peacefully without any fatal errors with pathing or makefiles. The builders are: CDT Builder and Scanner Configuration Builder.
That might be relevant because the below code wont compile for my object nor for a string, and even if I replace with:
stdout << "hello";
directly it still returns
invalid operands of types '__FILE*' {aka '__sFILE64*'} and 'const char [6]' to binary 'operator<<'
I thought this setup was working because I initialized a hello world file and it ran, but now that there is all my actual code with subdirectories in the src folder, it seems to not work anymore.
What I have tried:
-Had to use VisualStudio for school. It stopped working completely on mylaptop. I was having issues with stdout which causes me to question my code but it seemed to be visual studio having changed the data structure of flags, plus the make files were being a nightmare and couldn't figure out where to put their .o files, so I decided to install eclipse on another computer and see if that was easier.
-installed eclipse for c++ and the vers is:4.14.0
-I updated cygwin gcc,gnu,debug & etc to latest versions
-enabled auto refresh in general properties -I fixed sources.mk not including subdirectories by renaming a headers folder to r_headers, deleted the sources.mk and refreshed a lot, and then added another folder called a_test. I don't know why it wouldn't work before adding another folder, but it magically started finding all subdirectories.
-in my includes file it looks to me like everything necessary is included (Windows 10 64 bit) c:/cygwin64/usr/(include/, include/w32api) c:/cygwin64/lib/gcc/.../ (include,c++,c++/backward,c++/x86_64-pc-cygwin)
The relevant MyObject snippets
(the code itself is more complicated, this is purely made up snippets, but I just want to know if the error lies somewhere really simple and I am just not getting something very basic!)
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
}
Thank you very much, these past few days have been so unproductive because I just cant get anything to work properly!
For starters there is a typo
class MyObject {
private:
string s1;
sting s2;
^^^^^
//...
This operator
std::ostream& operator<<(MyObject const& o);
is not the same as this operator
std::ostream& operator<<(std::iostream& os, MyObject const& o) {
std::string s = toString();
os << s;
return os;
}
And it seems this member function
std::ostream& operator<<(MyObject const& o);
is redundant.
Moreover in definitions of members of the class outside the class you have to use a qualified name as for example
MyObject::MyObject(){
s1 = "hello";
s2 = "world";
}
The class can be defined in the header like
class MyObject {
private:
string s1;
sting s2;
public:
MyObject();
std::string toString() const;
};
Class members can be defined like
MyObject::MyObject() : s1( "hello" ), s2( "world" ){
}
std::string MyObject::toString() const {
return s1 + " " + s2;
}
and the operator << can be defined in the header like
inline std::ostream & operator<<(std::ostream& os, MyObject const& o) {
return os << o.toString();
}
Note that it should take a std::ostream&
, not a std::iostream&
.