C++ 指向C+中成员的指针出错+;
头文件C++ 指向C+中成员的指针出错+;,c++,C++,头文件 #include <iostream> #include <cstring> const unsigned MaxLength = 11; class Phone { public: Phone(const char *phone) { setPhone(phone); } void setPhone(const char Phone[ ]); const char* getPhone(); priv
#include <iostream>
#include <cstring>
const unsigned MaxLength = 11;
class Phone {
public:
Phone(const char *phone) {
setPhone(phone);
}
void setPhone(const char Phone[ ]);
const char* getPhone();
private:
char phone[MaxLength+1];
};
#包括
#包括
常量无符号MaxLength=11;
班级电话{
公众:
电话(常量字符*电话){
设置电话(电话);
}
void setPhone(const char Phone[]);
const char*getPhone();
私人:
字符电话[MaxLength+1];
};
Cpp文件
#include "Phone.h"
#include <iostream>
#include <ctype.h>
#include <cstring>
using namespace std;
bool checkNum(char num[]);
void Phone::setPhone(const char Phone[ ]) {
strncpy(phone, Phone, MaxLength);
phone[MaxLength] = '\0';
}
const char* Phone::getPhone() {
return phone;
}
int main() {
Phone i1("12345678901");
cout << i1.getPhone() << endl;
if (checkNum(i1.getPhone))
cout << "Correct" << endl;
else
cout << "Invalid Wrong" << endl;
}
bool checkNum(char num[]) {
bool flag = true;
if (isdigit(num[0]) == 0)
flag = false;
return flag;
}
#包括“Phone.h”
#包括
#包括
#包括
使用名称空间std;
bool-checkNum(char-num[]);
void Phone::setPhone(const char Phone[]){
strncpy(电话,电话,最大长度);
电话[MaxLength]='\0';
}
const char*Phone::getPhone(){
回电话;
}
int main(){
电话i1(“12345678901”);
cout在if(checkNum(i1.getPhone))
中,你在getPhone之后缺少一对括号;它应该是if(checkNum(i1.getPhone())
在if(checkNum(i1.getPhone))
中,你在getPhone之后缺少一对括号;它应该是if(checkNum(i1.getPhone()))
行:
if (checkNum(i1.getPhone))
应该是
if (checkNum(i1.getPhone()))
该行:
if (checkNum(i1.getPhone))
应该是
if (checkNum(i1.getPhone()))
我的天…我看了这么久这个代码…一直在不知不觉中传递这个。哈哈谢谢!我的天…我看了这么久这个代码…一直在不知不觉中传递这个。哈哈谢谢!