谁能帮我解决这个代码..我要做的就是复制显示在主函数中初始化的字符数组对cpp来说很新,我想不出办法
#include<iostream>
using namespace std;
class base1
{
public:
void setname(char *name);
void setpubl(char *publisher);
void setauth(char *author);
char getname();
char getpubl();
char getauth();
private:
char init_name[50], init_publ[50], init_auth[50];
};
void base1::setname(char *name)
{
int i=0;
while(init_name[i] != '\0')
{
init_name[i]=name[i];
i++;
}
}
void base1::setpubl(char *publisher)
{
int j=0;
while(init_publ[j] != '\0')
{
init_publ[j]=publisher[j];
j++;
}
}
void base1::setauth(char *author)
{
int k=0;
while(init_auth[k] != '\0')
{
init_auth[k]=author[k];
k++;
}
}
const char* base1::getname() const
{
return init_name;
}
const char* base1::getpubl() const
{
return init_publ;
}
const char* base1::getauth() const
{
return init_auth;
}
/*
base1::base1()
{
init_name[0]=0;
init_publ[0]=0;
init_auth[0]=0;
}*/
int main()
{
base1 hello;
char name[]="cpp";
char publisher[]="dreamworks";
char author[]="random";
hello.setname(name);
hello.setpubl(publisher);
hello.setauth(author);
cout<<hello.getname()<<endl;
cout<<hello.getpubl()<<endl;
cout<<hello.getauth()<<endl;
return 0;
}
当我尝试编译它时,它给了我很多错误。有人可以帮我吗。我正在使用 g++
错误:
copychar.cpp:49:13:错误:'const char* base1::getname() const' 的原型与类'base1'中的任何内容都不匹配</p>
copychar.cpp:10:6:错误:候选是:char base1::getname()
copychar.cpp:54:13:错误:'const char* base1::getpubl() const' 的原型与类'base1'中的任何内容都不匹配</p>
copychar.cpp:11:6:错误:候选是:char base1::getpubl()
copychar.cpp:58:13:错误:'const char* base1::getauth() const' 的原型与类'base1'中的任何内容都不匹配</p>
copychar.cpp:12:6:错误:候选是:char base1::getauth()
当我使用以下代码时,仅显示每个字符串的第一个字符
#include<iostream>
using namespace std;
class base1
{
public:
void setname(char *name);
void setpubl(char *publisher);
void setauth(char *author);
char getname();
char getpubl();
char getauth();
private:
char init_name[50], init_publ[50], init_auth[50];
};
void base1::setname(char *name)
{
int i=0;
while(name[i] != '\0')
{
init_name[i]=name[i];
i++;
}
}
void base1::setpubl(char *publisher)
{
int j=0;
while(publisher[j] != '\0')
{
init_publ[j]=publisher[j];
j++;
}
}
void base1::setauth(char *author)
{
int k=0;
while(author[k] != '\0')
{
init_auth[k]=author[k];
k++;
}
}
char base1::getname()
{
return *init_name;
}
char base1::getpubl()
{
return *init_publ;
}
char base1::getauth()
{
return *init_auth;
}
/*
base1::base1()
{
init_name[0]=0;
init_publ[0]=0;
init_auth[0]=0;
}*/
int main()
{
base1 hello;
char name[]="cpp";
char publisher[]="dreamworks";
char author[]="random";
hello.setname(name);
hello.setpubl(publisher);
hello.setauth(author);
cout<<hello.getname()<<endl;
cout<<hello.getpubl()<<endl;
cout<<hello.getauth()<<endl;
return 0;
}