【问题标题】:Strings in C Header FilesC 头文件中的字符串
【发布时间】:2010-11-11 21:21:47
【问题描述】:

我的头文件如下:

#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;
}

编译它会产生大量错误,其中大部分与缺少 string 数据类型有关。取消注释 using namespace std; 行可以修复它,但有人告诉我这是头文件中的不好做法。

【问题讨论】:

    标签: c++ string namespaces header std


    【解决方案1】:

    改变

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

    到

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

    【讨论】:

      【解决方案2】:

      如果您注释掉using 行,则每次使用时都需要将string 限定为std::string。 StringMultiply 的返回值正确但参数不正确。

      就我个人而言,我不理解与 using namespace std; 相比的建议 - 我不喜欢打字了。

      【讨论】:

      • 通常的建议“不要在头文件中的全局范围内放置using namespace std;”,说有人忽略了该建议并完成了它,你正在使用某人的头文件,而你有一个名为count 的东西。哦,还有一个std::count (IIRC)。它可以解决,这就是命名空间的用途,但是与其让头文件的许多用户做额外的工作,头文件的作者做一些额外的工作是有意义的。干杯,
      【解决方案3】:

      如果您在参数列表中将string Str 替换为std::string Str,则一切都会正常编译。究竟是什么问题?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        • 2013-03-29
        • 1970-01-01
        • 2015-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多