#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass
{
private:
int _x;
int _y;
std::string _word;
public:
MyClass();
MyClass(int x, int y, std::string word);
int getSum();
std::string getName();
~MyClass();
};
#endif
I cant get it to work, the debbuger just shoot me in the footā¦
I cant get it to work with strings. I wasted like 2hours trying to get it to work/learn about classes in different files. Then i backed up a bit i did one with just int and it worked out. So i got back to strings again and now ive wasted like 30min on itā¦
Best regards!
PS ivent took the time lelarning this with c++, āclasses in different filesā. Always feelt so overwhelming. Coming from c#, you just create classes and start using themā¦ Isnt possible to create classes in c++ without the .h-part? I mean its probably 2-3years away that i would really need that anyway hehe
Because youāre importing the header file into main.cpp. So, like in C# when you declare using system.Collections;, you donāt have to declare all of the Collections objects from that source.
At least, I think
Itās been a while since Iāve looked at C++. I miss it a great deal, apparently. Sorry my answer isnāt more elegant or technical. Maybe someone with deeper knowledge can answer.
Hey, same boat. When I was in CE, I raged and it was usually a typo lol. Visual Studio just said Error x8082221
I kinda buy that explanation, BUT it ādidā work before(and after) i put the "#include " in the .h-file(i mean no errors in the other two filesā¦) while not having it in neither main & MyClass.cpp ā¦ hmm i dont knowā¦
It works because the string-header file is included in main.cpp indirectly, through myclass.h, which is included in main.cpp.
The error you mention in the original post indicates that youāre missing the #include for the string header file somewhere.
It is not possible to create C++ classes without the .h part. You can put the implementation of your class into the header file and skip the cpp file, but thatās a bad idea for various reasons (compilation time, multiple declarations, etc. see this link for more info: https://stackoverflow.com/questions/583255/c-code-in-header-files). It can be useful for trivial classes or functions. your code would look like this:
#include <iostream>
#include <string>
class MyClass
{
private:
int _x;
int _y;
std::string _word;
public:
MyClass(int x, int y, std::string& word): _x(x), _y(y), _word(word){}
int getSum(){ return _x + _y; }
std::string getName(){ return _word; }
};
int main()
{
std::string name = "Herrow";
MyClass myclass(27, 32, name);
std::cout << "myclass " << myclass.getSum() << std::endl;
std::cout << myclass.getName() << std::endl;
std::string iGiveUp;
iGiveUp = myclass.getName();
//HERE LIES THE problem: "no operator "<<" matches these operands
//operand types are: std::ostream << std::string"
// v
std::cout << iGiveUp << std::endl;
std::cin.get();
return 0;
}
Thanks but i had already found the issue, the issue was that string was not included in the h-file.
Sounds weird that it worked out for you( if you had the same setup as i did :S ).
The problem with that is that i hadnt include strings in any of the files, but it worked in 2 out of 3 files. The one that didnt work was the header file.
So my idea was that maybe it gets include via #include āstdafx.hā?
You actually can it is just VERY strongly recommended against. As long as you declare everything above the main it will be fine. I had to do this for my CS classes when my teacher only wanted one file. Itās just plain gross honestly.