构建混合的c / c ++代码

时间:2022-09-01 19:12:01

I have a .cc file which uses both iostream and malloc. How can I compile that? using g++, it says

我有一个.cc文件,它使用iostream和malloc。我怎么编译呢?它说,使用g ++

 error: 'malloc' was not declared in this scope

using gcc, it says

使用gcc,它说

 fatal error: iostream: No such file or directory

The source code is located at http://sequitur.info/sequitur_simple.cc

源代码位于http://sequitur.info/sequitur_simple.cc

UPDATE

I changed malloc to new and chaned free to delete. Still I get a lot of errors. For example

我将malloc更改为new并且chaned free可以删除。我仍然遇到很多错误。例如

 /usr/include/c++/4.6/new:103:14: error:   initializing argument 2 of âvoid* operator new(std::size_t, void*)â [-fpermissive]

3 个解决方案

#1


5  

Either include <stdlib.h> or include <cstdlib> and change malloc to std::malloc - compile with g++. Including <cstdlib> is the prefered way for new C++ code, "name.h" style is deprecated in C++.

包括 或include 并将malloc更改为std :: malloc - 使用g ++编译。包含 是新C ++代码的首选方式,“name.h”样式在C ++中已弃用。

While this will fix you problem, it might be a better idea to migrate to new/delete, to be more consistantly C++.

虽然这将解决您的问题,但迁移到新/删除可能是更好的主意,以便更加一致地使用C ++。

#2


0  

Have you tried to include

你试过包括吗?

#include <stdio.h>      
#include <stdlib.h>   

and use g++?

并使用g ++?

#3


0  

use new and delete in C++ code. Don't mix new and malloc. From the code you posted, there isn't any reason AFAIK you can't use new and delete

在C ++代码中使用new和delete。不要混用new和malloc。从你发布的代码中,没有任何理由AFAIK你不能使用new和delete

#1


5  

Either include <stdlib.h> or include <cstdlib> and change malloc to std::malloc - compile with g++. Including <cstdlib> is the prefered way for new C++ code, "name.h" style is deprecated in C++.

包括 或include 并将malloc更改为std :: malloc - 使用g ++编译。包含 是新C ++代码的首选方式,“name.h”样式在C ++中已弃用。

While this will fix you problem, it might be a better idea to migrate to new/delete, to be more consistantly C++.

虽然这将解决您的问题,但迁移到新/删除可能是更好的主意,以便更加一致地使用C ++。

#2


0  

Have you tried to include

你试过包括吗?

#include <stdio.h>      
#include <stdlib.h>   

and use g++?

并使用g ++?

#3


0  

use new and delete in C++ code. Don't mix new and malloc. From the code you posted, there isn't any reason AFAIK you can't use new and delete

在C ++代码中使用new和delete。不要混用new和malloc。从你发布的代码中,没有任何理由AFAIK你不能使用new和delete