Functions in separate CPP files (again, sorry)

I'm having trouble creating functions in separate cpp files to main. I've followed the simple example here; http://www.cplusplus.com/forum/articles/10627/ which works fine with no errors. However, I want my functions to accept strings as arguments, so I did the following;

main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "myclass.h"  // defines MyClass

int main()
{
  MyClass a;
  a.foo("test");
  system("pause"); // I know this is frowned upon and I wouldn't use this in real code :)
  return 0;
}


myclass.h
1
2
3
4
5
6
7
8
// in myclass.h

class MyClass
{
public:
  void foo(string str);
  int bar;
};


myclass.cpp
1
2
3
4
5
6
7
8
9
10
#include "myclass.h"
#include <iostream>
#include <string>

using namespace std;

void MyClass::foo(string str)
{
	cout << "WIN!";
}


I have an error on "foo" in myclass.cpp claiming declaration is incompatible with ... declared on line 6 of myclass.h. If I use int instead of string it runs perfectly.

Also is it possible to just include functions in a separate cpp file without making them part of a class?
"myclass.cpp" includes "myclass.h" before <string>. That means that void foo(string str) is invalid because string has not yet been declared for this source file.

include <string> in the header, or ensure that you include string before you include myclass.h.
@Stewbond hate to high jack a topic but is the "#include <iostream>" necessary in the main.cpp since it is already included via myclass.h - through myclass.cpp?
Yes it is.

myclass.cpp includes <iostream>, Only myclass.cpp and the headers included below #include <iostream> will have access to that header.

main.cpp does not have access to what is included in myclass.cpp. Therefore if you want <iostream> there, you need to include it with that source file.

Limiting scope of variables/functions/classes is what splitting files is all about.
Thanks for the help Stewbond, but I'm still getting the same error.

main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "myclass.h"  // defines MyClass

int main()
{
  MyClass a;
  a.foo("test");
  system("pause"); // I know this is frowned upon and I wouldn't use this in real code :)
  return 0;
}


myclass.h
1
2
3
4
5
6
7
8
#include <string>

class MyClass
{
public:
  void foo(string str);
  int bar;
};


myclass.cpp
1
2
3
4
5
6
7
8
9
10
#include <string>
#include "myclass.h"
#include <iostream>

using namespace std;

void MyClass::foo(string str)
{
	cout << "WIN!";
}


Here is my Output Window:
1
2
3
4
5
6
7
8
9
10
11
ClCompile:
  myclass.cpp
myclass.h(6): error C2061: syntax error : identifier 'string'
myclass.cpp(8): error C2511: 'void MyClass::foo(std::string)' : overloaded member function not found in 'MyClass'
          myclass.h(4) : see declaration of 'MyClass'
  main.cpp
myclass.h(6): error C2061: syntax error : identifier 'string'
main.cpp(7): error C2660: 'MyClass::foo' : function does not take 1 arguments
  Generating Code...

Build FAILED.
myclass.h(6): error C2061: syntax error : identifier 'string'

Using string before namespace specification. Try std::string

Last edited on
Excellent! Thank you! This is all starting to make more sense now :)
Topic archived. No new replies allowed.