C++ classes, help please :(

main.cpp

#include "stdafx.h"
#include "MyClass.h"
#include <iostream>


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

MyClass.cpp

#include "stdafx.h"
#include "MyClass.h"
#include <iostream>


MyClass::MyClass(){}
MyClass::MyClass(int x, int y, std::string word)
{
	_x = x;
	_y = y;
	_word = word;
}

std::string MyClass::getName() {
	return _word;
}

int MyClass::getSum()
{
	return _x + _y;
}

MyClass::~MyClass(){}

MyClass.h

#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

Take out the quotes from name.

Still cloning your project, but thatā€™s the first thing I notice.

Hello Thanks!
TryĀØā€™d it right away, no luckā€¦

Hm, weirdā€¦ Iā€™m not the ā€œit works for meā€ kinda guy, but I did want to show you just in case :slight_smile:

image

Try cleaning up the headers a bit. I added string, and just declared the std namespace globally.

I removed using namespace std;, added all the stds back (lol), and it still works. I think youā€™re just missing #include <string>

2 Likes

Thanks mate!

Yepp that did it: just added #include ā€˜stringā€™ in the .h file it it all works nowā€¦
didnt have to put in the main nor the MyClass.cppā€¦

Is it supposed to be like that?! wt

Why does it work without "#include " in main for example?

thanks!

To bad i wasted a millon hours on that lol!

1 Like

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 :slight_smile:

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

2 Likes

haha yeaā€¦ :smiley:

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ā€¦

Gonna remeber this now at least hehe

PS mabe its included via #include ā€œstdafx.hā€?

1 Like

Youā€™re code worked fine for meā€¦

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ā€?

Best regards!

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.