猿问

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

我的类是这样写的
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; ignored
e:\wokc++\继承多态\test\base.h(16) : error C2501: 'getName' : missing storage-class or type specifiers
e:\wokc++\继承多态\test\base.h(18) : warning C4183: 'getName': member function definition looks like a ctor, but name does not match enclosing class
E:\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 identifier
E:\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 ambiguous
E:\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;
}
错了..麻烦指点下应该怎么写

天涯尽头无女友
浏览 240回答 3
3回答

函数式编程

#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;}

拉莫斯之舞

#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;}

人到中年有点甜

char* getName(){return name;}
随时随地看视频慕课网APP
我要回答