简体   繁体   English

标头C ++中带有STL的函数声明

[英]function declaration with STL in header c++

I am pretty new to the concept of splitting a program into header and etc. Normally, it goes ok, but in this case I have a whole bunch of errors if i try to do next: 我对将程序拆分为header头等的概念很陌生。通常情况下,它还可以,但是在这种情况下,如果我尝试执行下一步操作,则会遇到很多错误:

Suppose I have a .cpp file: 假设我有一个.cpp文件:

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include "Header.h"
using namespace std;

int main() {
    //some code here
}

map <char, char> create(vector <char> &one, vector <char> &two) {
    //some code here
}

vector <char> conc(string phrase) {
    // some code here
} 

vector <char> result(vector<char> three, map <char, char> code) {
    // some code here
}

In Header.h I have: Header.h我有:

map <char, char> create(vector <char> &one, vector <char> &two);
vector <char> conc(string phrase);
vector <char> result(vector<char> three, map <char, char> code);

Which are just function declarations.. If I put them in .cpp the program works great, but if in Header.h - it does not. 只是函数声明。如果我将它们放在.cpp中,则该程序可以很好地运行,但是如果在Header.h中,则不能。 Could you, please tell what I am missing here? 你能告诉我我在这里想念的吗?

I am reading about the concept of splitting on cprogramming.com, but they never had an example with STL. 我在cprogramming.com上阅读了拆分的概念,但他们从未使用STL进行示例。 Thank you! 谢谢!

You use using namespace std; 您可以using namespace std; in cpp file, but not in header (and don't use it in header), so you should use fully qualified type names: 在cpp文件中,但不在标头中(并且不要在标头中使用),因此您应该使用完全限定的类型名称:

#ifndef HEADER_H
#define HEADER_H

#include <string>
#include <map>
#include <vector>

std::map <char, char> create(std::vector <char> &one, std::vector <char> &two);
std::vector <char> conc(std::string phrase);
std::vector <char> result(std::vector<char> three, std::map <char, char> code);

#endif // HEADER_H

This is mostly an educated guess since you didn't post the actual error nor the whole code. 这主要是有根据的猜测,因为您没有发布实际错误或整个代码。

You're missing std:: before every STL class name since you should not use using statement in header files. 您在每个STL类名之前都缺少std:: ,因为您不应该在头文件中使用using语句。

Example: std::map <char, char> create(std::vector <char> &one, std::vector <char> &two); 示例: std::map <char, char> create(std::vector <char> &one, std::vector <char> &two);

Also make sure you have the proper include statement at the top of your header file ( <vector> , etc...). 还要确保在头文件的顶部( <vector>等)具有正确的include语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM