In capybara.h:
You are including <iostream>, but nothing in header requires it.
You are using string, but do not include <string> which is reqired. (Same for capybara.cpp)
And most importantly: never place usingnamespace in headers. It can silently break so much for any user of this header.
MiiniPaa:
Thanks! But, I have a few questions (please forgive me if they are silly, I am very new to C++):
What can usingnamespace mess up and why?
Since I had usingnamespace std; in capybara.h and capybara.cpp (whether or not it is good practice), why do I still need to #include <string> seperatly?
ne555:
Thanks for showing me that, I don't know how I didn't notice that. I guess I need to work on being more observant. About the mistakes in the OP, I made that post after being awake for 36 hours (partially due to staring at this code) so I am very sorry for any errors as it was copy/pasted from another forum that did not even reply at all (probably due to most people on that forum mainly programming in Java).
why do I still need to #include <string> seperatly?
Because you should include headers for anything you use. std::string is defined in <string> header. So if you not include it, your program is not guaranteed to work. It moght work for you but not for others, and even for you if you update your compiler, change some settings. usingnamespacehas nothing to do with included definitions. It only dumps names from some namespace in global.
#include <iostream>
#include <algorithm>
usingnamespace std;
void func1();
void func2();
int count;
int main()
{
int i;
for (i=0; i<10; i++) {
count = i * 2;
func1();
}
}
void func1()
{
func2();
std::cout << "count: " << count;
std::cout << '\n';
}
void func2()
{
int count;
for(count=0; count<3; count++) std::cout<< '.';
}
Try to compile it. It will give you an error. Remove usingnamespace std and it will work fine. Now imagine if you have code like that and you will include header with usingnamespace in it without knowing.