我试图让C++中的函数返回字符串,但函数名称是红色下划线(Visual Studio),并显示"无法重载仅通过返回类型区分的函数"字符串操作在函数外运行良好,但当我将其放在返回字符串的函数中时,会显示许多错误。
刚在课堂上学会了函数。对不起,我对C++有点陌生。只是想知道如何通过函数内部的一些字符串操作正确地返回带函数的字符串。感谢
这是我的部分代码:
main.cpp
#include"header.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
example7();
return 0;
}
函数.cpp
#include "header.h"
#include<iostream>
#include<string>
using namespace std;
string returnString(int a)
{
static string str1 = " ";
const string symbol = "!&";
static int num;
static int left;
num = (a / 5);
left = (a % 5);
str1 = to_string(num) + symbol + to_string(left) + symbol;
return str1;
}
int example7()
{
static int input;
cin >> input;
string abc = returnString(input);
cout << abc << endl;
system("pause");
return 0;
}
header.h
string returnString(int a);
int example7();
在header.h中也需要#include <string>
和using namespace std;
,否则编译器在声明returnString
时不知道string
是什么。
我建议打开编译器警告。