C4018,错误C4996和错误C4716 c++

warning C4018, error C4996 and error C4716 C++

本文关键字:错误 C4716 c++ C4996 C4018      更新时间:2023-10-16

嘿,伙计们,当我试图编译我的代码,我得到这些错误。非常感谢你的帮助。对于初学者,

错误编号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