C4018,错误C4996和错误C4716 c++
warning C4018, error C4996 and error C4716 C++
嘿,伙计们,当我试图编译我的代码,我得到这些错误。非常感谢你的帮助。对于初学者,
错误编号1:
警告1警告C4018: '<':有符号/无符号不匹配(cpp语音分析行23)
错误编号2:
警告2警告C4018: '<':有符号/无符号不匹配(cpp语音分析行31)
错误编号3:
错误3:'strcpy':这个函数或变量可能不安全。考虑使用strcpy_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。(cpp主线13)
错误编号4:
错误4 C4716: 'operator<<':必须返回一个值(cpp主线44)
我的代码分为三部分
1)
头
#ifndef SPEECHANALYST_H
#define SPEECHANALYST_H
#include<iostream>
#include<string>
using namespace std;
class SpeechAnalyst{
private:
std::string myData;
public:
SpeechAnalyst();
void clear();
void addData(char * stuff);
void addStringData(std::string stuff);
int getNumberOfWords() const;
int getNumberOfSentences() const;
friend ostream& operator << (ostream& outs, const SpeechAnalyst & sa);
};
#endif
2)
cpp语音分析
#include "SpeechAnalyst.h"
SpeechAnalyst::SpeechAnalyst(){
clear();
}
void SpeechAnalyst::clear(){
myData = "";
} // resets everything...
void SpeechAnalyst::addData(char * stuff){
while (*stuff++ != ' '){
myData += *stuff;
}
}
void SpeechAnalyst::addStringData(std::string stuff){
myData = stuff;
}
int SpeechAnalyst::getNumberOfWords() const{
int countSpace = 0;
for (int i = 0; i<(this->myData).length(); i++){
if ((this->myData).at(i) == ' ')
countSpace++;
}
return countSpace + 1;
}
int SpeechAnalyst::getNumberOfSentences() const{
int countDot = 0;
for (int i = 0; i<(this->myData).length(); i++){
if ((this->myData).at(i) == '.')
countDot++;
}
return countDot;
}
ostream& operator << (ostream& outs, const SpeechAnalyst &sa){
if (sa.myData.length()>0)
outs << "Data has " << sa.getNumberOfWords() << " words and " << sa.getNumberOfSentences() << " sentencesn";
else
outs << "No Data to print Outn";
}
3)
主要cpp#include "SpeechAnalyst.h"
#include <iostream>
#include <string>
int main(){
SpeechAnalyst sa;
cout << sa << endl;
std::string speech("Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.");
sa.addStringData(speech);
cout << sa << endl;
sa.clear();
char * data = new char[500];
strcpy(data, "Muffin says Hello.");
sa.addData(data);
cout << sa << endl;
sa.clear();
strcpy(data, "Muffin says Hello Muffin says Hello.");
sa.addData(data);
cout << sa << endl;
sa.clear();
strcpy(data, "Muffin says Hello. Muffin says Hello. Muffin says Hello.");
sa.addData(data);
cout << sa << endl;
return 0;
}
在函数getNumberOfWords和getnumberofsentence中,你是在比较一个有符号整型对象和一个无符号整型对象
for (int i = 0; i<(this->myData).length(); i++){
正确的语句应该是
for ( std::string::size_type i = 0; i < this->myData.length(); i++){
操作符& lt; & lt;声明返回类型为ostream&
ostream& operator << (ostream& outs, const SpeechAnalyst &sa){
但是在函数体中你没有返回任何东西。
关于使用函数strcpy
的错误,你应该简单地知道,在微软有许多idiots
,试图使程序员的生活更加困难。定义名称
#define _CRT_SECURE_NO_WARNINGS
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 错误 C4716"当事方::运算符+":必须返回一个值
- 错误C4716:rectionDownLog:必须返回值
- C4716错误 - 必须返回值
- 错误C4716:必须返回一个值,由实际返回值的函数抛出
- C4018,错误C4996和错误C4716 c++