1

how can I divide the header and implementation of a global function?

My way is:

split.h

#pragma once #include <string> #include <vector> #include <functional> #include <iostream> void split(const string s, const string c); 

split.cpp

#include "split.h" void split(const string& s, const string& c){ ... } 

main.cpp

// main.cpp : Defines the entry point for the console application. // #include <string> #include <vector> #include <functional> #include <iostream> #include "stdafx.h" #include "split.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<string> v; string s = "The;;woraaald;;is;;not;;enoaaaugh"; string c = " aaa ;; ccc"; split(s,c); return 0; } 

And errors are:

Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...\split.h 8

Error 2 error C2146: syntax error : missing ',' before identifier 's' ...\split.h 8

How can I solve this problem? thx

2
  • 2
    Classes in the standard library are in the std:: namespace. Do not use using namespace in the header file, fully qualify the name. For consistency, perhaps try to rid yourself of that habit in source files as well. Commented Mar 2, 2010 at 22:40
  • The second error is caused by string which needs to be std::string (unless you specify using std::string;). Commented Mar 2, 2010 at 22:41

4 Answers 4

4

In header file use std:: namespace qualifier - std::string

Sign up to request clarification or add additional context in comments.

Comments

1

In the header file, you have to give the fully qualified name std::string. In the source files, you can add using namespace std; or using std::string; and then just spell it string.

Also, you've declared the function taking arguments by value, but defined it taking arguments by reference.

Comments

1

At least one problem is, you are missing the 'std::' namespace qualifier in split.h:

#pragma once #include <string> #include <vector> #include <functional> #include <iostream> void split(const std::string s, const std::string c); 

Comments

0

I think you either forgot to put using std::string; before split declaration or use std::string const& as split parameter declarations.

Also split declaration mismatch from split definition string const vs string const&

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.