开发者

Global function header and implementation

开发者 https://www.devze.com 2022-12-21 22:18 出处:网络
how can I divide the header and implementation of a global function? My way is: split.h #pragma once #include <string>

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' ...\spli开发者_开发知识库t.h 8

How can I solve this problem? thx


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


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.


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


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&

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号