简体   繁体   中英

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. Uncommenting the 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. Return value for StringMultiply is correct but parameter is not.

Personally, I don't understand the advice versus 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. What exactly is the issue?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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