求助 简单的 C++程序(已解决,非常感谢帮忙。。。)

软件和网站开发以及相关技术探讨
aws453
帖子: 12
注册时间: 2007-03-12 13:31

求助 简单的 C++程序(已解决,非常感谢帮忙。。。)

#1

帖子 aws453 » 2007-03-13 13:56

程序代码如下:
#include <stdio.h>
void main()
{
char my_string[]="Hello world!";
my_print (my_string);
my_print2 (my_string);
}

void my_print(char * string)
{
printf("The string is %s\n",string);
}

void my_print2(char * string)
{
char *string2;
int size,size2,i;
size=strlen (string);
size2=size-1;
string2=(char*) malloc (size+1);
for(i=0;i<size;i++)
string2[size-i]=string;

string2[size]='\0';
printf("the string printed backward is %s \n",string2);
}


不知道是不是还少装什么包之类的 ,还是真的存在问题。显示的错误如下:
hello.c:2: error: ‘::main’ must return ‘int’
hello.c: In function ‘int main()’:
hello.c:5: error: ‘my_print’ was not declared in this scope
hello.c:6: error: ‘my_print2’ was not declared in this scope
hello.c: In function ‘void my_print2(char*)’:
hello.c:18: error: ‘strlen’ was not declared in this scope
hello.c:20: error: ‘malloc’ was not declared in this scope

我是新手,刚开始学习,能人们帮帮忙呀。。。。。。。。
上次由 aws453 在 2007-03-16 12:35,总共编辑 1 次。
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
联系:

Re: 求助 简单的 C++程序

#2

帖子 dbzhang800 » 2007-03-13 14:10

aws453 写了: 不知道是不是还少装什么包之类的 ,还是真的存在问题。显示的错误如下:
hello.c:2: error: ‘::main’ must return ‘int’
很清楚,永远不要写 void main()
尽管很多书上的例子上这样写了,有些编译器也默许了
aws453 写了:hello.c: In function ‘int main()’:
hello.c:5: error: ‘my_print’ was not declared in this scope
hello.c:6: error: ‘my_print2’ was not declared in this scope
先声明,再使用,c的基本规则,不用说了吧
aws453 写了:hello.c: In function ‘void my_print2(char*)’:
hello.c:18: error: ‘strlen’ was not declared in this scope
hello.c:20: error: ‘malloc’ was not declared in this scope
先include必须的头文件
aws453 写了:我是新手,刚开始学习,能人们帮帮忙呀。。。。。。。。
好好补充些c/c++的知识
头像
houdini
帖子: 250
注册时间: 2006-04-08 22:07
联系:

#3

帖子 houdini » 2007-03-13 17:59

完全是C阿
aws453
帖子: 12
注册时间: 2007-03-12 13:31

#4

帖子 aws453 » 2007-03-14 9:56

谢谢。。。 :D
aws453
帖子: 12
注册时间: 2007-03-12 13:31

#5

帖子 aws453 » 2007-03-14 10:26

hello.c: In function ‘my_print2’:
hello.c:13: warning: incompatible implicit declaration of built-in function ‘strlen’
hello.c:15: warning: incompatible implicit declaration of built-in function ‘malloc’

改了后显示的警告。。。
aws453
帖子: 12
注册时间: 2007-03-12 13:31

#6

帖子 aws453 » 2007-03-14 10:37

#include <iostream.h>
using namespace std;

class Hello {
public:
void Hello(int,int);
void Display();
private:
int day;
int month;
} ;

void Hello::Hello(int d,int m)
{
day=d;
month=m;
}
void Hello::Display()
{
cout<<"Hello,World!\n"<<endl;
}
int main()
{
Hello theHello;
theHello.Hello(14,03);
theHello.Display();
return 0;
}
显示的 错误:
aws@ubuntu:~/myRun$ g++ -c Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files

但目录下确实存在Hello.cpp 文件的 呀。。。
头像
titainium
帖子: 689
注册时间: 2006-12-02 12:25

#7

帖子 titainium » 2007-03-14 11:42

g++ -o Hello Hello.cpp
Titainium
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
联系:

#8

帖子 dbzhang800 » 2007-03-14 11:43

aws453 写了:#include <iostream.h>
using namespace std;
去掉.h
aws453 写了: 显示的 错误:
aws@ubuntu:~/myRun$ g++ -c Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files

但目录下确实存在Hello.cpp 文件的 呀。。。
检查一下文件名的大小写
aws453
帖子: 12
注册时间: 2007-03-12 13:31

#9

帖子 aws453 » 2007-03-15 9:41

ls 上的,为什么要去掉 .h 呀?我去掉后还是显示:
aws@ubuntu:~/myRun$ g++ -c Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files
aws@ubuntu:~/myRun$ g++ -o Hello Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files

觉得和文件名大小写没什么关系吧!
头像
titainium
帖子: 689
注册时间: 2006-12-02 12:25

#10

帖子 titainium » 2007-03-15 10:38

iostream.h是旧式的写法,这种形式的头文件将namespace里面的东西全部放到全局变量里面去了,换句话说就是没有namespace,这是为了和老式的C++编译器兼容而额外编写的头文件。

另外,你试一试我的那句编译命令。
Titainium
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
联系:

#11

帖子 dbzhang800 » 2007-03-15 11:38

aws453 写了:ls 上的,为什么要去掉 .h 呀?我去掉后还是显示:
aws@ubuntu:~/myRun$ g++ -c Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files
aws@ubuntu:~/myRun$ g++ -o Hello Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files

觉得和文件名大小写没什么关系吧!
记住一点,linux/unix 对大小写是敏感的
aws453
帖子: 12
注册时间: 2007-03-12 13:31

#12

帖子 aws453 » 2007-03-15 13:28

titainium 写了:iostream.h是旧式的写法,这种形式的头文件将namespace里面的东西全部放到全局变量里面去了,换句话说就是没有namespace,这是为了和老式的C++编译器兼容而额外编写的头文件。

另外,你试一试我的那句编译命令。
我把 .h 去掉了 ,然后用你上面的命令: g++ -o Helle Hello.cpp 还是显示:
aws@ubuntu:~/myRun$ g++ -o Hello Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files

哪位能人有时间,帮帮忙调试一下,我实在是不知道怎么找出错误在哪。。。。。。 :( :( :( :( :( :( :(
chinese_ys
帖子: 116
注册时间: 2006-11-03 8:49
来自: Halifax

#13

帖子 chinese_ys » 2007-03-15 14:35

现不说你的文件能不能找的到,程序就有问题阿!

代码: 全选

#include <iostream>
using namespace std;

class Hello
{
public:
  Hello(){}
  Hello(int, int);
  void Display();
private:
  int day;
  int month;
}hello;

Hello::Hello(int a, int b)
{
  day = a;
  month = b;
  cout<<"The date is "<<day<<'-'<<month<<endl;
}

void Hello::Display()
{
  cout<<"This is a class testing!"<<endl;
}

int main()
{
  int a = 18, b = 4;
  Hello(a, b);
  hello.Display();
  return 0;
}
把我的保存成任意名称,后缀名cpp。编译看看。
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
联系:

#14

帖子 dbzhang800 » 2007-03-15 14:35

我只想问你一句:

你的文件名是 Hello.cpp , 还是 hello.cpp
头像
zhan
帖子: 1880
注册时间: 2005-08-15 0:04
来自: 南7技校

#15

帖子 zhan » 2007-03-15 15:45

楼主在当前目录下面执行 ls 然后把结果贴出来吧.
飞得高,飞得低,学习再学习,多少大秘密!
http://zhan.blog.ubuntu.org.cn
回复