执行时出现c++数组错误
c++ array error while executing
当我运行程序时,我不断收到这个错误。该程序仍将执行和运行,并在终端中显示正确的名称,但我需要它来输出另一个文件并将它们放在那里。请帮助刚接触c++的人。
bash-3.2$g++-Wall 1.cpp
1.cpp:在函数"std::string IP_Calculation(std::string*,std::字符串,int)"中:1.cpp:71:1:警告:控件到达非无效函数[-Wreturn type]的末尾
string IP_Calculation(string IP[], string Company_name, int total_IPS)
{
string temp = "";
char buf[80];
char buf2[80];
if (total_IPS != 0)
{
int UniqueIP_count = total_IPS;
for (int i = 0; i < total_IPS; i++)
{
for (int j = i + 1; j < total_IPS; j++)
{
if (strcmp(IP[i].c_str(), IP[j].c_str()) == 0)
{
if (strcmp(IP[i].c_str(), "") == 0)
{
continue;
}
IP[j] = "";
UniqueIP_count--;
}
}
}
temp = print_array(IP);
cout << Company_name << " | Number of Visitor: " << total_IPS
<< "| Unique Visitors: " << UniqueIP_count << endl;
//cout<<Company_name<<" | Number of Visitor: "<<buf <<"| Unique Visitors: "<<UniqueIP_count<<endl;
cout << temp;
sprintf(buf, "%d", total_IPS);
sprintf(buf2, "%d", UniqueIP_count);
// return temp=Company_name+" | Number of Visitor: "+( total_IPS) +"| Unique Visitors: "+to_string( UniqueIP_count)+"n"+temp+"n";
return temp = Company_name + " | Number of Visitor: " + buf
+ "| Unique Visitors: " + buf2 + "n" + temp + "n";
}
}
警告是因为函数被声明为返回string
,但编译器已经确定它可能不会这样做。您有一个return
语句,它在if
语句中返回一个字符串。但是当Total_IPs
是0
时,您将不会执行该代码块,也永远不会执行return
语句。由于没有else
块,您只需退出函数,而不需要根据需要返回字符串。您需要将其更改为:
if (Total_IPs != 0) {
...
} else {
return "";
}
以便在条件失败时返回一些内容。
我不确定我是否理解。您想将输出写入文件中吗?像这样的东西应该起作用:
int writeFile ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.n";
myfile.close();
return 0;
}
相关文章:
- 类动态数组错误
- 如何返回多维数组?错误C2440
- 数组错误(不允许类型不完整)
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 从文本文件读取 2D 数组错误
- 动态结构数组错误
- 旋转数组 - 错误
- 按字母顺序排序名称,数组错误
- 因为每个循环都读取数组错误
- 二维数组错误
- C 获取数组错误
- 编译时出现字符串数组错误
- c++2维指针类数组错误(使用openframework)
- C++数组错误
- C++上数组错误的大小
- C++数组错误:访问冲突读取位置0xC0000005
- c++2D数组错误
- C++:为“char [4]”数组错误获取太多初始值设定项
- 分段错误(核心转储)排序字符串数组错误