一个“hello”程序,请教。。

Akagg 2004-11-12 09:40:22
//child.cpp

char str[] = "Hello World!\n";

//main.cpp
#include <iostream>

extern char *str; // The string to print

int main()
{
std::cout << str << std::endl;
return (0);
}

为什么在主程序得不到指向数组的指针???
问题何在?如何最简单改动最少??
...全文
215 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
healer_kx 2004-11-16
  • 打赏
  • 举报
回复
能编译通过?
Akagg 2004-11-16
  • 打赏
  • 举报
回复
呵呵,谢谢 maxcode(人弋石马) 了。

关于using namespace std的问题:
//a.cpp--------
using namespace std;
xxx{
std::cout<<
}

//b.cpp------
using namespace std;
xxx{
cout<<
}

//c.cpp--------
xxx{
std::cout<<
}


想想原理就知道了
maxcode 2004-11-12
  • 打赏
  • 举报
回复
<How not to programm in C++>上的解释:
Answer 7: The problem is that sub.cpp defines str as a character array (char []). The extern statement in main.cpp
defines str as a character pointer (char *).
***************************************************************************************
Now character arrays and character pointers are interchangeable almost everywhere in C++. This is one of the few
cases they are not.
*************************************************************************************
In this case, the program main thinks that str is a character pointer, so it goes to that location and
reads the first four bytes expecting an address. The first four bytes are "Hell," which is not an address, and so the
program crashes.

一般情况下char arr[]与char *parr中的arr/parr参数可互换,但这次不行
哈哈
将extern char *str; // The string to print==>>extern char str[]
OK
还有:
using namespace std;漏了
Akagg 2004-11-12
  • 打赏
  • 举报
回复
to kobefly(科比---不惧挑战!) :
数组下标就是指针阿,也可以自加阿。记得”数组名就是指向数组第一个字符的指针“这句话吗?
在一个文件里这样是可以的,如下:

char str[]="hello,world.";
char *pstr=str;
std::cout<<pstr<<std::endl;

关键是如何extern的问题。。。(平时都是extern函数,很少变量的)
Akagg 2004-11-12
  • 打赏
  • 举报
回复
我知道是错的,也知道改,只是想多召集一些改法。((这个错误的程序就叫“画蛇添足”))

重要的一点,还没有人说-----到底错在什么地方------(该错误的内部机制)
FromNoWhere 2004-11-12
  • 打赏
  • 举报
回复
嗯,我不include那个cpp文件的话就没有上面的问题,include后就出现上面的错误了。。。
FromNoWhere 2004-11-12
  • 打赏
  • 举报
回复
我的怎么按照上面的写上去出现下面的错误呢???


out.obj : error LNK2005: "char * str" (?str@@3PADA) already defined in Test.obj
Debug/Test.exe : fatal error LNK1169: one or more multiply defined symbols found
Acyk 2004-11-12
  • 打赏
  • 举报
回复
不太清楚,我没用extern 的习惯。
Acyk 2004-11-12
  • 打赏
  • 举报
回复
不好意思,是#include "child.cpp"
我用C习惯了……
pacman2000 2004-11-12
  • 打赏
  • 举报
回复
不需要include cpp文件,直接把声明改成extern char str[];就行。
Acyk 2004-11-12
  • 打赏
  • 举报
回复
加一个#include "child.c" 即可……
paybfly 2004-11-12
  • 打赏
  • 举报
回复
#include "child.cpp"
extern char str[];
王旺旺旺 2004-11-12
  • 打赏
  • 举报
回复
脱了裤子放P
sharkhuang 2004-11-12
  • 打赏
  • 举报
回复
extern char str[]; //这样声明一下
kobefly 2004-11-12
  • 打赏
  • 举报
回复
char str[] = "Hello World!\n";

extern char *str; // The string to print

一个是指针,一个是数组,这样不大好吧

原地方的str不可以自加
但到了你的main.cpp呢
变了指针
是不是可以做++了呢?

chinadragonss 2004-11-12
  • 打赏
  • 举报
回复
直接都放在main.cpp好了阿,没必要2个文件。
goodluckyxl 2004-11-12
  • 打赏
  • 举报
回复
#include <iostream>

#include "child.cpp" //加头文件

extern char str[]; //这样声明一下

int main()
{
std::cout << str << std::endl;
return (0);
}
MCR 2004-11-12
  • 打赏
  • 举报
回复
为什么要这么写呢?
Akagg 2004-11-12
  • 打赏
  • 举报
回复
没人看到帖子吗?
顶一下往上提一提。。

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧