passing string type in multi-file compilation

After using c-strings / character arrays for a year now I'm being encouraged to use the string type instead. After getting some errors in a multi-file project where i passed strings, I checked textbooks, websites... still not working.

So I wrote a simple test program. But even that isn't working. I'm pulling my hair out trying to figure out what I'm doing wrong, because I know it's probably something really simple to fix.

Why can't I get it to work?

here's the code:

MAIN.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "foo.h"
using namespace std;

int main()
{
	string foo;
	cin >> foo;

	cout << foobar(foo) << endl;

return 0;
}


FOO.CPP
1
2
3
4
5
6
7
8
#include <iostream>
//#include "foo.h"

int foobar(string bar)
{
	cout << bar;
	return 1;
}


FOO.H
1
2
3
#include <string>

int foobar(string);


I can paste the errors (from g++ and CC) if that helps.
using namespace only applies to the source where it is used. Although my personal recommendation is that you pretend it doesn't exist and just write the whole type (std::string).
When using string type you have always to use either using namespace std; or explicit notation std::string because string type is a member of std namespace.

You have used using namespace std; only in your main.cpp file so this would compile whilst foo.cpp wouldn't.

In header files (foo.h) is generally better to use the explicit notation std::string because in some cases you don't want to use using namespace std; in all files which #include your header file.
Last edited on
checked back for replies and even before I read any of them I noticed the missing line. o_o

I am literally LOLing right now.
sorry for using up your time guys.
Topic archived. No new replies allowed.