鱼C论坛

 找回密码
 立即注册
查看: 1754|回复: 2

求大婶解决关于C++的问题

[复制链接]
发表于 2014-12-10 21:02:21 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <iostream>

  2. using namespace std;
  3. const int cities = 4;
  4. const int years =5 ;

  5. int main()
  6. {
  7.     const char *cities[cities]=
  8.     {
  9.       "Gribble city",
  10.       "Gribbletown",
  11.       "New Gribble",
  12.       "San Gribble",
  13.       "Gribble vista"
  14.     };
  15.     int maxtemps[years][cities]=
  16.     {
  17.       {96,100,87,101,105},
  18.       {97,101,88,102,106},
  19.       {98,102,89,103,107},
  20.       {99,103,90,104,108}

  21.     };
  22.     cout<<"Maximum teperatures for 2008 - 2011\n\n";
  23.     for(int city = 0; city<cities;++city)
  24.     {
  25.         cout<< cities[city];
  26.         for(int year=0;year<years;++year)
  27.             cout<<maxtemps[year][city];
  28.         cout<<endl;

  29.     }
  30.     return 0;
  31. }
复制代码
各位大婶们,小弟初学C++,现在看的书是C++ primer plus,现在看到第五章的二维数组,编译器是CodeBlocks,以上代码为何不能通过编译? 照着书上打上去的,跟着书的思路分析的,为何错误呢? 二维数组不能直接赋值和访问成员的吗? 错误原因请看截图,刚才发错板块了。。。


QQ截图20141210205411.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-10 22:15:12 | 显示全部楼层
const int cities = 4;
const char *cities[cities]=
cities先定义成了全局变量,在main函数中又重新定义成const char cities你数组的下标又使用cities。这个定义太乱了。即使上面的定义没有问题,你全局的cities是4,但是看你后面的字符串每个长度都超过了4。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-10 22:30:42 | 显示全部楼层
:victory:已解决,谢了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 11:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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