求助 简单的 C++程序(已解决,非常感谢帮忙。。。)
-
- 帖子: 12
- 注册时间: 2007-03-12 13:31
求助 简单的 C++程序(已解决,非常感谢帮忙。。。)
程序代码如下:
#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
我是新手,刚开始学习,能人们帮帮忙呀。。。。。。。。
#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 次。
-
- 帖子: 3182
- 注册时间: 2006-03-10 15:10
- 来自: xi'an China
- 联系:
Re: 求助 简单的 C++程序
很清楚,永远不要写 void main()aws453 写了: 不知道是不是还少装什么包之类的 ,还是真的存在问题。显示的错误如下:
hello.c:2: error: ‘::main’ must return ‘int’
尽管很多书上的例子上这样写了,有些编译器也默许了
先声明,再使用,c的基本规则,不用说了吧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
先include必须的头文件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
好好补充些c/c++的知识aws453 写了:我是新手,刚开始学习,能人们帮帮忙呀。。。。。。。。
- houdini
- 帖子: 250
- 注册时间: 2006-04-08 22:07
- 联系:
-
- 帖子: 12
- 注册时间: 2007-03-12 13:31
-
- 帖子: 12
- 注册时间: 2007-03-12 13:31
#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 文件的 呀。。。
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 文件的 呀。。。
-
- 帖子: 3182
- 注册时间: 2006-03-10 15:10
- 来自: xi'an China
- 联系:
-
- 帖子: 12
- 注册时间: 2007-03-12 13:31
- titainium
- 帖子: 689
- 注册时间: 2006-12-02 12:25
-
- 帖子: 3182
- 注册时间: 2006-03-10 15:10
- 来自: xi'an China
- 联系:
-
- 帖子: 12
- 注册时间: 2007-03-12 13:31
我把 .h 去掉了 ,然后用你上面的命令: g++ -o Helle Hello.cpp 还是显示:titainium 写了:iostream.h是旧式的写法,这种形式的头文件将namespace里面的东西全部放到全局变量里面去了,换句话说就是没有namespace,这是为了和老式的C++编译器兼容而额外编写的头文件。
另外,你试一试我的那句编译命令。
aws@ubuntu:~/myRun$ g++ -o Hello Hello.cpp
g++: Hello.cpp: No such file or directory
g++: no input files
哪位能人有时间,帮帮忙调试一下,我实在是不知道怎么找出错误在哪。。。。。。
-
- 帖子: 116
- 注册时间: 2006-11-03 8:49
- 来自: Halifax
现不说你的文件能不能找的到,程序就有问题阿!
把我的保存成任意名称,后缀名cpp。编译看看。
代码: 全选
#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;
}
-
- 帖子: 3182
- 注册时间: 2006-03-10 15:10
- 来自: xi'an China
- 联系:
- zhan
- 帖子: 1880
- 注册时间: 2005-08-15 0:04
- 来自: 南7技校