使用函数编译Helper文件

时间:2013-02-05 23:40:55

标签: c++ compilation

我不知所措 - 我刚刚进入C ++,由于某种原因,这对我来说不合适。所以我使用Netbeans,并且我有以下主文件:

#include <cstdlib>

#include "functions.h"

using namespace std;

int main(int argc, char** argv) {

    f("help");

    return 0;
}

Functions.h文件:

#include <string>

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void f( string a );

#endif

和Functions.cpp文件:

#include "functions.h"

void f( string a ) {
    return;
}

所以,长话短说,它没有编译。它说它无法理解字符串变量?我没有得到它,我试图在整个地方移动包含字符串,但似乎没有任何帮助。我该怎么办?

3 个答案:

答案 0 :(得分:2)

如果您尝试使用std::string,则必须在函数标头中#include <string>,并将其称为std::string,因为它位于std命名空间中。< / p>

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <string>

void f( std::string a );

#endif

请参阅this related post以及why is 'using namespace std' considered bad practice in C++?

答案 1 :(得分:2)

您需要在Functions.h中包含字符串头文件,并告诉编译器string来自std命名空间。

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <string>
void f( std::string a );

#endif

Functions.cpp文件:

#include "functions.h"

void f( std::string a ) {
    return;
}

更好的做法是通过const引用传递字符串

void f(const std::string& a ) {
    return;
}

请参阅Why is 'using namespace std;' considered a bad practice in C++?

答案 2 :(得分:0)

包含标准标题:<string>

#include <string>