鱼C论坛

 找回密码
 立即注册
查看: 1134|回复: 5

[已解决]类的构造函数用指针接收字符串报错

[复制链接]
发表于 2020-12-23 14:51:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. using namespace std;

  3. class Books{
  4. public:
  5.         Books(float pri,char* na,char *au,char *pu):price(pri),name(na),author(au),pub(pu){}
  6.         char* getName(){return name;}
  7.         void printinfo(){
  8.                 cout<<price;
  9.         }
  10. private:
  11.         const float price;
  12.         char *name;
  13.         char *author;
  14.         char *pub;
  15. };

  16. int main(){
  17.         Books b[4]={
  18.         (39,"平凡的世界","路遥","上海交通大学出版社"),
  19.         (32.6,"假如给我三天光明","海伦凯勒","上海交通大学出版社"),
  20.         (21.4,"活着","余华","电子科技大学出版社"),
  21.         (45,"老人与海","海明威","北京师范大学出版社")};                   //这里报错
  22.        
  23.         char *search;
  24.         cout<<"请输入要查询的书名:";
  25.         cin>>search;
  26.         for(int i = 0;i<5;i++){
  27.                 if(*b[i].getName() == *search){
  28.                         b[i].printinfo();
  29.                 }
  30.         }
  31. }
复制代码



                报错信息:[Error] conversion from 'const char [19]' to non-scalar type 'Books' requested
最佳答案
2020-12-23 15:10:48
  1. #include <iostream>
  2. using namespace std;

  3. class Books{
  4. public:
  5.         Books(float pri,char* na,char *au,char *pu):price(pri),name(na),author(au),pub(pu){}
  6.         char* getName(){return name;}
  7.         void printinfo(){
  8.                         cout<<price;
  9.         }
  10. private:
  11.         const float price;
  12.        
  13.         char *name;
  14.         char *author;
  15.         char *pub;
  16. };

  17. int main(){
  18.         //语法错误,应该改成
  19.         Books b[4]= {
  20.                         Books(39,"平凡的世界","路遥","上海交通大学出版社"),
  21.                         Books(32.6,"假如给我三天光明","海伦凯勒","上海交通大学出版社"),
  22.                         Books(21.4,"活着","余华","电子科技大学出版社"),
  23.                         Books(45,"老人与海","海明威","北京师范大学出版社")};
  24.        
  25.     //这么定义的字符串指针没有分配内存地址,是无法使用的。
  26.         //应该用以下几种方式定义:
  27.         //1: char search[64] = {0};
  28.         //2: char * search = new char [64];
  29.         //3: 用stl 的 string 类  std::string search;
  30.         //相同的道理,在类里面定义的字符串指针也是有问题的。
  31.         //但是,上面的数组 b 的初始化没有问题。
  32.     char *search;
  33.     cout<<"请输入要查询的书名:";
  34.     cin>>search;
  35.     for(int i = 0;i<5;i++)
  36.         {
  37.                 // c/c++不支持字符串 == 操作
  38.                 //应该用函数 if(strcmp(str1,str2) == 0)
  39.                 //加* 只是比较第一个字符是否相等
  40.                 if(*b[i].getName() == *search)
  41.                 {
  42.                         b[i].printinfo();
  43.                 }
  44.     }
  45.         return 0;
  46. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-23 15:10:48 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. using namespace std;

  3. class Books{
  4. public:
  5.         Books(float pri,char* na,char *au,char *pu):price(pri),name(na),author(au),pub(pu){}
  6.         char* getName(){return name;}
  7.         void printinfo(){
  8.                         cout<<price;
  9.         }
  10. private:
  11.         const float price;
  12.        
  13.         char *name;
  14.         char *author;
  15.         char *pub;
  16. };

  17. int main(){
  18.         //语法错误,应该改成
  19.         Books b[4]= {
  20.                         Books(39,"平凡的世界","路遥","上海交通大学出版社"),
  21.                         Books(32.6,"假如给我三天光明","海伦凯勒","上海交通大学出版社"),
  22.                         Books(21.4,"活着","余华","电子科技大学出版社"),
  23.                         Books(45,"老人与海","海明威","北京师范大学出版社")};
  24.        
  25.     //这么定义的字符串指针没有分配内存地址,是无法使用的。
  26.         //应该用以下几种方式定义:
  27.         //1: char search[64] = {0};
  28.         //2: char * search = new char [64];
  29.         //3: 用stl 的 string 类  std::string search;
  30.         //相同的道理,在类里面定义的字符串指针也是有问题的。
  31.         //但是,上面的数组 b 的初始化没有问题。
  32.     char *search;
  33.     cout<<"请输入要查询的书名:";
  34.     cin>>search;
  35.     for(int i = 0;i<5;i++)
  36.         {
  37.                 // c/c++不支持字符串 == 操作
  38.                 //应该用函数 if(strcmp(str1,str2) == 0)
  39.                 //加* 只是比较第一个字符是否相等
  40.                 if(*b[i].getName() == *search)
  41.                 {
  42.                         b[i].printinfo();
  43.                 }
  44.     }
  45.         return 0;
  46. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-23 15:57:00 | 显示全部楼层

x谢谢啦 但是还是有点问题就是返回wrong value而且有黄色警告在对象数组初始化的过程中
[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;

  4. class Books{
  5. public:
  6.         Books(float pri,char* na,char *au,char *pu):price(pri),name(na),author(au),pub(pu){}
  7.         char* getName(){return name;}
  8.         void printinfo(){
  9.                 cout<<"书名:"<<name<<endl<<"作者:"<<author<<endl<<"出版社:"<<pub<<endl;
  10.         }
  11. private:
  12.         const float price;
  13.         char *name;
  14.         char *author;
  15.         char *pub;
  16. };

  17. int main(){
  18.         Books b[4]={
  19.         Books(39,"平凡的世界","路遥","上海交通大学出版社"),\
  20.         Books(32.6,"假如给我三天光明","海伦凯勒","上海交通大学出版社"),\
  21.         Books(21.4,"活着","余华","电子科技大学出版社"),\
  22.         Books(45,"老人与海","海明威","北京师范大学出版社")};
  23.         char search[64] = {0};
  24.         cout<<"请输入要查询的书名:";
  25.         cin>>search;
  26.         for(int i = 0;i<5;i++){
  27.                 if(strcmp(b[i].getName(),search) == 0){
  28.                         cout<<"查询到书籍,以下是 "<<search<<" 的信息"<<endl;
  29.                         b[i].printinfo();
  30.                         break;
  31.                 }
  32.                 if(i == 4&&strcmp(b[i].getName(),search) != 0){
  33.                         cout<<"未找到书籍!";
  34.                 }
  35.         }
  36. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-23 16:04:44 | 显示全部楼层
Ryan_Li 发表于 2020-12-23 15:57
x谢谢啦 但是还是有点问题就是返回wrong value而且有黄色警告在对象数组初始化的过程中
[Warning] depre ...

因为你类构造函数是
Books(float pri,char* na,char *au,char *pu);
全是 char * 类型,但是 “平凡的世界” 等字符串是常量,也就是 const char *
把常量赋给char * 是有问题的。
另外,你的循环结束条件  i < 5 会造成数组越界,因为你的数组只有4个元素。

最好这样写:

  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. class Books
  5. {
  6. public:
  7.         Books(float pri,char* na,char *au,char *pu):price(pri),name(na),author(au),pub(pu){}

  8.         bool nameEqual(const char * _name)
  9.         {
  10.                 return name.compare(_name) == 0 ? true : false;
  11.         }

  12.         void printinfo()
  13.         {
  14.                 cout<<price;
  15.         }
  16. private:
  17.         const float price;
  18.         string name;
  19.         string author;
  20.         string pub;
  21. };

  22. int main(){
  23.         Books b[4]={
  24.                         Books(39.0f,"平凡的世界","路遥","上海交通大学出版社"),
  25.                         Books(32.6f,"假如给我三天光明","海伦凯勒","上海交通大学出版社"),
  26.                         Books(21.4f,"活着","余华","电子科技大学出版社"),
  27.                         Books(45.0f,"老人与海","海明威","北京师范大学出版社")};
  28.         
  29.         char search[64] = {0};

  30.     cout<<"请输入要查询的书名:";
  31.     cin>>search;
  32.     for(int i = 0;i<4;i++)
  33.         {
  34.                 if(b[i].nameEqual(search))
  35.                 {
  36.                         b[i].printinfo();
  37.                         break;
  38.                 }
  39.     }        
  40.         return 0;
  41. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-23 16:05:59 | 显示全部楼层

黄色报错我搞定了 换成const就行了 我是傻*....但是如果输入一个不匹配的书名 会返回一个wrong walue
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-23 16:11:57 | 显示全部楼层
xieglt 发表于 2020-12-23 16:04
因为你类构造函数是
Books(float pri,char* na,char *au,char *pu);
全是 char * 类型,但是 “平凡的 ...

搞定了 谢谢大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-4 06:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表