简体   繁体   English

C头文件中的字符串

[英]Strings in C Header Files

My header file is as follows: 我的头文件如下:

#include <iostream>
#include <string>
#include <windows.h>
#include <math.h>

//using namespace std;

std::string StringMultiply(string Str, int Mult)
{
    std::string Return;

    for (int Index = 0; Index <= Mult; Index++)
    {
        Return += Str;
    }

    return Return;
}

Compiling it produces a slew of errors, most of them pertaining to the absence of a string datatype. 编译它会产生一系列错误,其中大多数与缺少string数据类型有关。 Uncommenting the using namespace std; 取消对using namespace std;注释using namespace std; line fixes it, but I've been told this is bad practice in header files. 行修复了它,但有人告诉我这是头文件中的不良做法。

change 更改

std::string StringMultiply(string Str, int Mult)

to

std::string StringMultiply(std::string Str, int Mult)

You need to qualify string as std::string every time you use it if you comment out the using line. 如果您注释掉using行,则每次使用时都需要将string限定为std::string Return value for StringMultiply is correct but parameter is not. StringMultiply返回值正确,但参数不正确。

Personally, I don't understand the advice versus using namespace std; 就个人而言,与using namespace std;相比,我不了解建议using namespace std; - I don't like typing any more than I have to. -我不喜欢输入多余的文字。

If you replaced string Str with std::string Str in the parameter list, everything would compile fine. 如果换成string Strstd::string Str在参数列表中,一切都将编译好的。 What exactly is the issue? 到底是什么问题?

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

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