advertencia C4018, error C4996 y error C4716 C++ [cerrado]
Frecuentes
Visto 371 equipos
-4
Hey guys when I tried to compile my code I get these errors. Help will be greatly appreciated. regards beginner programer,
error number 1:
Warning 1 warning C4018: '<' : signed/unsigned mismatch (cpp speech analyst line 23)
error number 2:
Warning 2 warning C4018: '<' : signed/unsigned mismatch (cpp speech analyst line 31)
error number 3:
Error 3 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. (cpp main line 13)
error number 4:
Error 4 error C4716: 'operator<<' : must return a value (cpp main line 44)
And this is my code divided into three parts
1)
Encabezamiento
#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 Speech Analyst
#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)
Main 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 Respuestas
0
In functions getNumberOfWords and getNumberOfSentences you are comparing an object of signed int with an object of some unsigned integral type
for (int i = 0; i<(this->myData).length(); i++){
The correct statement will look as
for ( std::string::size_type i = 0; i < this->myData.length(); i++){
Operator << is declared as having return type ostream&
ostream& operator << (ostream& outs, const SpeechAnalyst &sa){
but in the function body you are returning nothing.
As for the error relative to using function strcpy
you should simply know that in Microsoft there are many idiots
that try to make the life of programmers harder. Define name
#define _CRT_SECURE_NO_WARNINGS
before including headers as it is written in the error message.
contestado el 28 de mayo de 14 a las 13:05
Thank you man I really appreciate your help you saved my grade. I hope I was able to vote your answer up, but I need more reputation points to do so. They take away some of my points every time I ask a question as if I am supposed to be born a wizard :( - Saleh
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c++ or haz tu propia pregunta.
Look at the errors one by one (actually leer the error message, they are all very explicit), and search a bit if it's not completely obvious. What exactly don't you understand about each of them? - Mat
The first three are warnings, only the last is an error and the solution is simple, if you say a function will return a value then you must return one. - Retired Ninja
Unfortunately since 3 questions were asked there's no single dupe that covers this, but here are some that cover each part. 1 y 2 3 4 - Retired Ninja