[英]C4716 Error - Must Return a Value
我不確定為什么我的程序無法運行。 就是說User :: receiveMessage和User :: sendMessage必須返回一個值。 抱歉,我是編程新手。
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
class User {
public:
char username[16];
char userMotto[64];
public:
User(char name[16], char motto[64]) {
strcpy(username, name);
strcpy(userMotto, motto);
}
int sendMessage(const char *messageString, User &otherUser) {
cout << "\n" << username << " is sending this message: " << messageString;
otherUser.receiveMessage(messageString);
}
int receiveMessage(const char *messageString) {
cout << endl << username << " received this message: " << messageString;
}
};
int main() {
User sender("Bill", "I message, therefore I am");
char message[64];
cout << "Enter your Message to Send: ";
cin >> message;
User receiver("Ted", "I message, therefore I am");
sender.sendMessage(message, receiver);
return 0;
}
我通過clang格式和在線Clang編譯器傳遞了您的代碼,這些是錯誤消息:
prog.cc:20:3: warning: control reaches end of non-void function [-Wreturn-type]
}
^
prog.cc:24:3: warning: control reaches end of non-void function [-Wreturn-type]
}
^
prog.cc:28:15: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
User sender("Bill", "I message, therefore I am");
^
prog.cc:28:23: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
User sender("Bill", "I message, therefore I am");
^
prog.cc:32:17: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
User receiver("Ted", "I message, therefore I am");
^
prog.cc:32:24: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
User receiver("Ted", "I message, therefore I am");
^
要修復前兩個警告,您需要更改這些功能:
int receiveMessage(const char *messageString) {
cout << endl << username << " received this message: " << messageString;
}
receiveMessage
是否需要返回一個int
? 如果不是,請將其更改為void
返回類型:
void receiveMessage(...
如果確實需要返回一個int
,則在最后添加一個return
語句:
int receiveMessage(const char *messageString) {
...
return 0;
}
對於其他警告,您可以在整個過程中用std::string
替換char[]
,但還不錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.