我在Visual Studio上编译和运行代码时遇到问题。它给我错误代码“声明与”不兼容。更具体地说,从主CPP在每个函数的开头调用代码中的标头时。我试过将标题中的void更改为int,但似乎并没有解决。我现在陷入困境,需要一些指导。
错误是: “严重性代码描述项目文件行抑制状态 错误(有效)E0147声明与“ void convertTime :: invalidHr(int hour)”(在“ Time.h”的第9行声明)不兼容。Source.cpp 6“
#include <iostream>
#include "Time.h"
using namespace std;
int convertTime::invalidHr(int hour) *//error on this line*
{
int convertHour = hour;
try
{
if (hour < 13 && hour > 0)
{
hour = hour + 12;
return hour;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input hour again in correct 12 hour format: ";
cin >> hour;
invalidHr(hour);
throw 10;
}
}
catch (int c) { cout << "Invalid hour input"; }
}
int convertTime::invalidMin(int min) *//error here*
{
int convertMin = min;
try
{
if (min < 60 && min > 0)
{
return min;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input minutes again in correct 12 hour format: ";
cin >> min;
invalidMin(min);
throw 20;
return 0;
}
}
catch (int e) { cout << "Invalid minute input" << endl; }
}
int convertTime::invalidSec(int sec) *//error here*
{
int convertSec = sec;
try
{
if (sec < 60 && sec > 0)
{
return sec;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input seconds again in correct 12 hour format: ";
cin >> sec;
invalidSec(sec);
throw 30;
return 0;
}
}
catch (int t) { cout << "Invalid second input" << endl; }
}
void convertTime::printMilTime()
{
cout << "Converted time: " << hour << ":" << min << ":" << sec;
}
这是我的标题:
class convertTime
{
public:
int hour, min, sec;
void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
void printMilTime();
};
答案 0 :(得分:1)
成员函数的返回类型不匹配。
在类定义中,您具有:
{('dict_1', 'dict_1_1', 'dict_1_1_1'): 'dict_1_1_1', ('dict_1', 'dict_1_1', 'dict_1_1_2'): 'dict_1_1_2', ('dict_2', 'dict_2_1'): 'dict_2_1', ('dict_2', 'dict_2_2'): 'dict_2_2', ('dict_3',): 'dict_3'}
在实现中,您具有:
void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
您需要更改其中之一,以便它们匹配。