分页: 1 / 2

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

发表于 : 2007-03-13 13:56
aws453
程序代码如下:
#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

我是新手,刚开始学习,能人们帮帮忙呀。。。。。。。。

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

发表于 : 2007-03-13 14:10
dbzhang800
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++的知识

发表于 : 2007-03-13 17:59
houdini
完全是C阿

发表于 : 2007-03-14 9:56
aws453
谢谢。。。 :D

发表于 : 2007-03-14 10:26
aws453
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’

改了后显示的警告。。。

发表于 : 2007-03-14 10:37
aws453
#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 文件的 呀。。。

发表于 : 2007-03-14 11:42
titainium
g++ -o Hello Hello.cpp

发表于 : 2007-03-14 11:43
dbzhang800
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 文件的 呀。。。
检查一下文件名的大小写

发表于 : 2007-03-15 9:41
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

觉得和文件名大小写没什么关系吧!

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

另外,你试一试我的那句编译命令。

发表于 : 2007-03-15 11:38
dbzhang800
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 对大小写是敏感的

发表于 : 2007-03-15 13:28
aws453
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

哪位能人有时间,帮帮忙调试一下,我实在是不知道怎么找出错误在哪。。。。。。 :( :( :( :( :( :( :(

发表于 : 2007-03-15 14:35
chinese_ys
现不说你的文件能不能找的到,程序就有问题阿!

代码: 全选

#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。编译看看。

发表于 : 2007-03-15 14:35
dbzhang800
我只想问你一句:

你的文件名是 Hello.cpp , 还是 hello.cpp

发表于 : 2007-03-15 15:45
zhan
楼主在当前目录下面执行 ls 然后把结果贴出来吧.