m.mukhanov
m.mukhanov

Reputation: 13

Global function header and implementation

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

Upvotes: 1

Views: 1738

Answers (4)

lollinus
lollinus

Reputation: 434

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&

Upvotes: 0

richb
richb

Reputation: 4906

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);

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254421

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.

Upvotes: 1

dimba
dimba

Reputation: 27571

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

Upvotes: 4

Related Questions