警告C4018,错误C4996和错误C4716 C ++

时间:2014-05-28 10:56:10

标签: c++

嘿,当我试图编译我的代码时,我得到了这些错误。将非常感谢帮助。 关于初学者程序员,

错误编号1:

警告1警告C4018:'<' :签名/未签名不匹配(cpp语音分析员第23行)

错误编号2:

警告2警告C4018:'<' :签名/未签名不匹配(cpp语音分析员第31行)

错误编号3:

错误3错误C4996:'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++ != '\0'){
        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() << " sentences\n";
    else
        outs << "No Data to print Out\n";
}

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;
}

1 个答案:

答案 0 :(得分:0)

在函数getNumberOfWords和getNumberOfSentences中,您将signed int的对象与某些无符号整数类型的对象进行比较

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的错误,您应该知道在Microsoft中有许多idiots试图让程序员的生活变得更难。定义名称

#define _CRT_SECURE_NO_WARNINGS

在包含标题之前,在错误消息中写入。