鱼C论坛

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

[已解决]返回值和类型的问题

[复制链接]
发表于 2022-2-11 17:28:23 | 显示全部楼层 |阅读模式

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

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

x
  1. int* myfind2(const vector<int>&vec, int value) {
  2.         for (int ix = 0; ix <vec.size(); ++ix) {
  3.                 if (vec[ix] == value)
  4.                         return &vec[ix];
  5.         return 0;
  6.         }
  7.        

  8. }
复制代码

上述代码中,一直报返回类型和值不匹配的问题,请问哪里有问题,纯菜鸟,求助
最佳答案
2022-2-11 17:42:22
  1. #include <vector>

  2. using std::vector;

  3. const int *myfind2(const vector<int> &vec, int value) {
  4.     for(int ix = 0; ix < vec.size(); ++ix) {
  5.         if(vec[ix] == value)
  6.             return &vec[ix];
  7.         return 0;
  8.     }

  9.     // return ???;
  10.     return 0;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-11 17:39:57 | 显示全部楼层
因为你的代码里面有一个return 0,而你的返回类型应该是一个整型的指针,不是整型
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-11 17:42:22 | 显示全部楼层    本楼为最佳答案   
  1. #include <vector>

  2. using std::vector;

  3. const int *myfind2(const vector<int> &vec, int value) {
  4.     for(int ix = 0; ix < vec.size(); ++ix) {
  5.         if(vec[ix] == value)
  6.             return &vec[ix];
  7.         return 0;
  8.     }

  9.     // return ???;
  10.     return 0;
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-2-11 17:52:43 | 显示全部楼层
试试这个:
  1. #include <iostream>
  2. #include <vector>

  3. using std::cout, std::endl, std::vector;

  4. int* myfind2(const vector<int>& vec, int value) {
  5.     for (int x: vec) {
  6.         if (x == value) {
  7.             return &x;
  8.         }
  9.     }
  10.     return NULL;
  11. }

  12. int main() {
  13.     vector<int>arr = { 13, 2, 3, 4, 5 };
  14.     int x = 3;
  15.     int* p = myfind2(arr, x);
  16.     cout << *p;
  17.     return 0;
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-11 19:07:26 | 显示全部楼层

返回局部变量的地址?
  1. $ cat main.cpp
  2. #include <iostream>
  3. #include <vector>

  4. using std::cout, std::endl, std::vector;

  5. int* myfind2(const vector<int>& vec, int value) {
  6.     for (int x: vec) {
  7.         if (x == value) {
  8.             return &x;
  9.         }
  10.     }
  11.     return NULL;
  12. }

  13. int main() {
  14.     vector<int>arr = { 13, 2, 3, 4, 5 };
  15.     int x = 3;
  16.     int* p = myfind2(arr, x);
  17.     cout << *p;
  18.     return 0;
  19. }
  20. $ g++-debug -o main main.cpp
  21. main.cpp: In function ‘int* myfind2(const std::vector<int>&, int)’:
  22. main.cpp:9:20: warning: address of local variable ‘x’ returned [-Wreturn-local-addr]
  23.     9 |             return &x;
  24.       |                    ^~
  25. main.cpp:7:14: note: declared here
  26.     7 |     for (int x: vec) {
  27.       |              ^
  28. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-11 20:22:20 | 显示全部楼层
人造人 发表于 2022-2-11 19:07
返回局部变量的地址?

抱歉,失误了
  1. #include <iostream>
  2. #include <vector>

  3. using std::cout, std::endl, std::vector;

  4. int* myfind2(const vector<int>& vec, int value) {
  5.     int n = 0;
  6.     for (int x: vec) {
  7.         if (x == value) {
  8.             return (int*)&vec[n];
  9.         }
  10.         n++;
  11.     }
  12.     return NULL;
  13. }

  14. int main() {
  15.     vector<int>arr = { 13, 2, 3, 17, 5 };
  16.     int x = 3, y = 17;
  17.     int* p = myfind2(arr, x), *q = myfind2(arr, y);
  18.     cout << *p << endl;
  19.     cout << *q << endl;
  20.     return 0;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 23:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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