为了账号安全,请及时绑定邮箱和手机立即绑定

麻烦指点下在C++中指针的赋值与读取该怎么写?

麻烦指点下在C++中指针的赋值与读取该怎么写?

C++ C
天涯尽头无女友 2022-06-03 13:09:09
我的类是这样写的class base{private:int id;char *name;int getId(){return id;}*char getName(){return *name;}void setId(int id){this->id=id;}void setName(char *name){this->name=name;}}主函数里这样调用的int main(int argc, char* argv[]){base b;char *p="xxxxx";b.setName(p);cout<<b.getName();return 0;}报了一车错误:e:\wokc++\继承多态\test\base.h(15) : warning C4518: 'char ' : storage-class or type specifier(s) unexpected here; ignorede:\wokc++\继承多态\test\base.h(16) : error C2501: 'getName' : missing storage-class or type specifierse:\wokc++\继承多态\test\base.h(18) : warning C4183: 'getName': member function definition looks like a ctor, but name does not match enclosing classE:\wokc++\继承多态\test\test.cpp(11) : error C2628: 'base' followed by 'int' is illegal (did you forget a ';'?)E:\wokc++\继承多态\test\test.cpp(15) : error C2248: 'setName' : cannot access private member declared in class 'base'e:\wokc++\继承多态\test\base.h(25) : see declaration of 'setName'E:\wokc++\继承多态\test\test.cpp(17) : error C2065: 'cout' : undeclared identifierE:\wokc++\继承多态\test\test.cpp(17) : error C2248: 'getName' : cannot access private member declared in class 'base'e:\wokc++\继承多态\test\base.h(15) : see declaration of 'getName'E:\wokc++\继承多态\test\test.cpp(17) : error C2297: '<<' : illegal, right operand has type 'int *'E:\wokc++\继承多态\test\test.cpp(19) : error C2664: '__thiscall base::base(const class base &)' : cannot convert parameter 1 from 'const int' to 'const class base &'Reason: cannot convert from 'const int' to 'const class base'No constructor could take the source type, or constructor overload resolution was ambiguousE:\wokc++\继承多态\test\test.cpp(19) : error C2553: no legal conversion of return value to return type 'class base *'E:\wokc++\继承多态\test\test.cpp(20) : warning C4508: 'main' : function should return a value; 'void' return type assumed各位大师...我知道 ..我*char getName(){return *name;}错了..麻烦指点下应该怎么写
查看完整描述

3 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

#include<iostream>
using namespace std;

class base
{
private:
int id;
char *name;
public: //下面的成员函数不应该私有,应该公有,才能在main里使用
int getId()
{return id;}
char *getName() //*char getName()改为char *getName()
{return name;} //return *name;改为return name;
void setId(int id)
{this->id=id;}
void setName(char *name)
{this->name=name;}
}; //类的最后挂号后面应该加分号,不能丢,切记

int main(int argc, char* argv[])
{
base b;
char *p="xxxxx";
b.setName(p);
cout<<b.getName()<<endl;
return 0;
}

另外再补充点,不知道对楼主有没有用,希望对你有帮助。
上面程序的改写:

#include<iostream>
#include<string> //string头文件
using namespace std;

class base
{
private: //把私有成员与下面成员函数里的参数区分,可以不利用指针,更方便
int ID; ////
string Name; //利用string类型,加头文件#include<string>
public:
int GetID()
{return ID;} ////
string GetName()
{return Name;} ////
void SetID(int id)
{ID=id;} //私有成员和函数参数区分,这样就可以不用到指针
void SetName(string name)
{Name=name;} //私有成员和函数参数区分,这样就可以不用到指针
};

int main(int argc, char* argv[])
{
base b;
string p="xxxxx";
b.SetName(p);
cout<<b.GetName()<<endl;
return 0;
}


查看完整回答
反对 回复 2022-06-06
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

#include <iostream>
using namespace std;
class base
{
private:
int id;
public:
char *name;//这个不能私有

int getId()
{
return id;
}
char *getName()//这里错了
{
return name;//这里错了
}

void setId(int id)
{
this->id=id;
}

void setName(char *name)
{
this->name=name;
}
};

int main(int argc, char* argv[])
{
base b;
char *p="xxxxx";
b.setName(p);

cout<<b.getName();
system("pause");
return 0;
}


查看完整回答
反对 回复 2022-06-06
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

char* getName()
{
return name;
}

查看完整回答
反对 回复 2022-06-06
  • 3 回答
  • 0 关注
  • 239 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信