Hello, I'm a beginner in programming and studying the C++ programming language using the book : Programming principles and practice using C++. Today I'm here because I was trying to write some random code to understand them better, so I decided to write a very simple class just for try :
1 2 3 4 5 6
class Room {
public:
string name;
};
Then I decided to create a simple function using a Room object as argument :
1 2 3 4 5
void print(Room current) {
cout << "call of a function\n";
}
But then in main function I tried to pass an int argument to the print function expecting a compiler error, instead, the program compiles but I don't get the string : "call of a function\n.
1 2 3 4 5 6 7 8 9
int main()
{
int a = 100;
move(a);
}
What i can't understand is why the compiler doesn't report me that there is a call to an undeclared function in main. I defined the print function but it takes a Room argument, not an int argument, so there is no function called print wich takes an int argument, so I expected to get a compiler error . Am i wrong ? I know that a function definition must have the same types, both return and argument types as we use in our file. Why the compiler doesn't report me a call to an undeclared function ? It has something to do with class ?
What i can't understand is why the compiler doesn't report me that there is a call to an undeclared function in main
Under normal circumstances you'd be right, and this would produce a compiler error... however....
This is the problem with usingnamespace std;. When you do that, you dump EVERYTHING in the std namespace into the global namespace, not just the stuff you intended.
Here, std::move is an actual function... which, when you do using namespace std... becomes move. That is the function you are calling. So there is no error.