在Code::Blocks中出现编译错误,急待解决?

软件和网站开发以及相关技术探讨
回复
头像
smhong
帖子: 11
注册时间: 2008-10-24 10:59
来自: 重庆
联系:

在Code::Blocks中出现编译错误,急待解决?

#1

帖子 smhong » 2008-10-31 11:37

在VC下能通过编译,但在Code::Blocks中出现编译错误,并出现如下提示,这个是什么原因啊,怎么解决和配置,请赐教。谢谢,在线等候。

/usr/include/c++/4.2/backward/backward_warning.h|32|警告: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.|
/exam3-11.cpp|2|错误: ‘::main’必须返回‘int’|
||=== Build finished: 2 errors, 0 warnings ===|
谢谢。。
人应“愤“发自强,没有愤怒,怎能自强!加油,洪哥!
头像
wenstream
帖子: 186
注册时间: 2008-06-18 22:02

Re: 在Code::Blocks中出现编译错误,急待解决?

#2

帖子 wenstream » 2008-10-31 12:59

smhong 写了:在VC下能通过编译,但在Code::Blocks中出现编译错误,并出现如下提示,这个是什么原因啊,怎么解决和配置,请赐教。谢谢,在线等候。

/usr/include/c++/4.2/backward/backward_warning.h|32|警告: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.|
/exam3-11.cpp|2|错误: ‘::main’必须返回‘int’|
||=== Build finished: 2 errors, 0 warnings ===|
谢谢。。
第一个warning写得很清楚,你用了旧版本的头文件(deprecated or antiquated header)
warning提供了两个办法,照做应该OK:
1。将头文件改成带名空间的新版本头文件
或者2。编译时去掉__DEPRECATED这个宏定义

第二个错误,main函数最后记得返回一个整形值
头像
smhong
帖子: 11
注册时间: 2008-10-24 10:59
来自: 重庆
联系:

Re: 在Code::Blocks中出现编译错误,急待解决?

#3

帖子 smhong » 2008-10-31 14:30

Compiling: /home/hong/c++code/教材/exam3-10.cpp
In file included from /home/hong/c++code/教材/exam3-10.cpp:1:
/home/hong/c++code/教材/exam3-10.h: In member function ‘void Line::showA() const’:
/home/hong/c++code/教材/exam3-10.h:43: 错误: ‘cout’在此作用域中尚未声明
/home/hong/c++code/教材/exam3-10.h:44: 错误: ‘endl’在此作用域中尚未声明
/home/hong/c++code/教材/exam3-10.h: In member function ‘void Line::showB() const’:
/home/hong/c++code/教材/exam3-10.h:49: 错误: ‘cout’在此作用域中尚未声明
/home/hong/c++code/教材/exam3-10.h:50: 错误: ‘endl’在此作用域中尚未声明
/home/hong/c++code/教材/exam3-10.cpp: At global scope:
/home/hong/c++code/教材/exam3-10.cpp:2: 错误: ‘::main’必须返回‘int’
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 0 warnings

头文件中我使用了<iostream>怎么还说没有申明cout 呢?
人应“愤“发自强,没有愤怒,怎能自强!加油,洪哥!
头像
smhong
帖子: 11
注册时间: 2008-10-24 10:59
来自: 重庆
联系:

Re: 在Code::Blocks中出现编译错误,急待解决?

#4

帖子 smhong » 2008-10-31 14:52

终于搞定了,首先感谢大虾给我的帮助,我找到了问题:
在VC和C::B中有如下不同
1.首先头文件中的.h要省略,如,<iostream.h>写成<iostream>;
2.将void main(void)写成int main(void) (关于这一点我还有很多疑问呢,怎么明明是无返回值void,但怎么一定要写成int?如果知道的,请一定告诉我哟)
3.将cout写成std::cout
如上所述问题即可解决!
人应“愤“发自强,没有愤怒,怎能自强!加油,洪哥!
头像
wenstream
帖子: 186
注册时间: 2008-06-18 22:02

Re: 在Code::Blocks中出现编译错误,急待解决?

#5

帖子 wenstream » 2008-10-31 18:33

小case了,大家共同学习吧
1.带.h的是旧版的头文件,后来C++标准委员会为了标准化,同时引入了名空间,就把头文件标准化为没有后缀的版本
当然一般情况编译器新旧两个版本都会提供,比如在VC中

#include <iostream.h>
...
cout<<endl;
..


#include <iostream>
...
std::cout<<std::endl;
..

都可以。但是建议用后一种。

2.unix由c语言写成,unix中常通过返回一个整型值来表示程序运行的结果状态,在C程序的main()中常常返回int,C++选择兼容C,那么也常常是这样

3.使用指令(using directive)或使用声明(using declaration)可以帮助我们摆脱输入一个名字空间中完整标识符的烦恼
比如有了using namespace std;在当前文件打开整个名字空间
std::cout 简写作 cout 即可。
不过打开整个名字空间这种做法在头文件中慎用。
头像
smhong
帖子: 11
注册时间: 2008-10-24 10:59
来自: 重庆
联系:

Re: 在Code::Blocks中出现编译错误,急待解决?

#6

帖子 smhong » 2008-11-03 15:22

谢谢!搞清楚了!
大家帮忙看看这段程序,看有什么问题?在VC下没有问题,在C::B下编译不能通过,为什么阿?。。

代码: 全选

#include<iostream>
#include<string>

class Employee
{
    private:
        char name[30];
        float salary;
        static float allSalary;
    public:
        Employee(char *n,float s)
        {
            strcpy(name,n);
            salary=s;
            allSalary=allSalary+salary;
        }

        ~Employee(void)
        {
            allSalary=allSalary-salary;
        }

        static float GetAllSalary(void)
        {
            return allSalary;
        }
};

float Employee::allSalary=0;
int main(void)
{
    float all;
    Employee *e1=new Employee("张三",100);//报错
    all=Employee::GetAllSalary();
    std::cout<<"AllSalary="<<all<<std::endl;

    Employee *e2=new Employee("王四",200);//有问题
    all=Employee::GetAllSalary();
    std::cout<<"AllSalary="<<all<<std::endl;

   Employee *e3=new Employee("李五",300);//有问题
    all=Employee::GetAllSalary();
    std::cout<<"AllSalary="<<all<<std::endl;

    delete e1;
    all=Employee::GetAllSalary();
    std::cout<<"AllSalary="<<all<<std::endl;

    delete e2;
    all=Employee::GetAllSalary();
    std::cout<<"AllSalary="<<all<<std::endl;

    delete e3;
    all=Employee::GetAllSalary();
    std::cout<<"AllSalary="<<all<<std::endl;
}
编译报错为如下:

代码: 全选

Compiling: /home/hong/c++code/教材/exam3-14.cpp
/home/hong/c++code/教材/exam3-14.cpp: In function ‘int main()’:
/home/hong/c++code/教材/exam3-14.cpp:33: 警告: 不建议使用从字符串常量到‘char*’的转换
/home/hong/c++code/教材/exam3-14.cpp:37: 警告: 不建议使用从字符串常量到‘char*’的转换
/home/hong/c++code/教材/exam3-14.cpp:41: 警告: 不建议使用从字符串常量到‘char*’的转换
Linking console executable: /home/hong/c++code/教材/exam3-14
Process terminated with status 0 (0 minutes, 0 seconds)
3 errors, 0 warnings
请问,这个是什么原因阿?
人应“愤“发自强,没有愤怒,怎能自强!加油,洪哥!
头像
自由建客
帖子: 13468
注册时间: 2008-07-30 23:21
系统: Debian stable AMD64

Re: 在Code::Blocks中出现编译错误,急待解决?

#7

帖子 自由建客 » 2008-11-03 17:45

strcpy 不是 C++ 函数,是 C 函数,在 cstring 里面。C++ 的 string 一个容器,你看看 STL 的书就知道了。至于警告自己琢磨去。

代码: 全选

#include <cstring>
头像
wenstream
帖子: 186
注册时间: 2008-06-18 22:02

Re: 在Code::Blocks中出现编译错误,急待解决?

#8

帖子 wenstream » 2008-11-03 22:38

smhong新手吧
用的这本什么教材,程序是教材的例子吧。。。?
我看见 /c++code/教材/exam3-14.cpp 字样
这本教材好差。。。。

const char * -> char *
汗。。。。
头像
kevinhslcn
帖子: 48
注册时间: 2007-11-19 13:10

Re: 在Code::Blocks中出现编译错误,急待解决?

#9

帖子 kevinhslcn » 2008-11-04 16:03

smhong 写了:终于搞定了,首先感谢大虾给我的帮助,我找到了问题:
在VC和C::B中有如下不同
1.首先头文件中的.h要省略,如,<iostream.h>写成<iostream>;
2.将void main(void)写成int main(void) (关于这一点我还有很多疑问呢,怎么明明是无返回值void,但怎么一定要写成int?如果知道的,请一定告诉我哟)
3.将cout写成std::cout
如上所述问题即可解决!
带.h是旧版的头文件,这个也不标准。估计LZ用的是VC6吧,在VS2003以后的版本应该就不能这样用了。
回复