So i was trying to print hello world on screen using oop
Here is my header
virtual void Hello(std::string str);
and here is the definition
1 2 3 4
|
void Base::Hello(std::string str) {
std::cout << "hello " << str << "\n"; // this gets an error
}
|
But for some reason I kept having this error
1 2
|
no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << std::string
|
But if I put
&
on the str
std::cout << "hello " << &str << "\n";
It is working fine.
I dont get it. Why is that?
The str is pass by value. I should not have to dereference it
1 2 3 4 5 6 7
|
int main() {
BaseClass b;
b.Load("hello");
return 0;
}
|
are all string in c++ a pointer?
also I never declare any pointer on string. (string*)
Last edited on
Did you remember to #include <string>
?
Yeah I did. Its already on my header file. Im still confuse about the & part though.
What compiler are you using and which standard of C++ are you targeting?
I think Visual studio compiler? I am using VS2015.
Im not really sure about the standard c++ question.
I'm also using Visual Studio 2015, and I don't have that issue. Try uninstalling and reinstalling (sorry, I know it takes forever to install).
Last edited on
If pre-compiled headers are being used, turn them off:
Project => Properties => C/C++ => PreCompiled Headers => Not Using Precompiled Headers
The do a clean rebuild. Build => Rebuild