由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 不用头文件,如何调用函数?C++
相关主题
function declarationWhy should i include .cpp instead of .h
c function 在 c里调用和C++调用结果不一样C++ 的 问题
C++ namespace 弱问C++默认的copy constructor的疑惑
c++ does not check const for extern variable?一个关于C++ template和overload的问题
question about const referencewhy copy assignment operator returns non-const type?
which style do you prefer?C++ template function type
怎么隐藏c++template代码?只提供lib 不提供头文件一个诡异的const_cast问题
有关objec access path的问题warning: returning address of local variable or temporary
相关话题的讨论汇总
话题: puts话题: function话题: std话题: main话题: const
进入Programming版参与讨论
1 (共1页)
e****d
发帖数: 333
1
进入未名形象秀
我的博客

[回复文章] [回信给作者] [本篇全文] [进入讨论区] [返回顶部] [修改文章] [删除
文章] [转寄] [转贴] [ 1 ]
发信人: everid (everid), 信区: Programming
标 题: 一道C++习题求解。
发信站: BBS 未名空间站 (Thu Feb 26 00:32:25 2009)
Without including a header file, declare the function puts() from the
Standard C Library. Call this function from main().
我写:
//main.cpp
namespace std{
int puts(const char*);
}
main(){
using namespace std;
puts("h");
}
>g++ main.cpp
报错:
In function `main': undefined reference to `std::puts(char const*)'
怎么回事呢?
t****t
发帖数: 6806
2
this is C function, so you must declare it as
extern "C" int puts(const char *);
alternatively, use C compiler (gcc) and change the file name to .c as
opposed to .cpp
I think the question explicitly said "standard c library". use c++ here is
画蛇添足.

【在 e****d 的大作中提到】
: 进入未名形象秀
: 我的博客
:
: [回复文章] [回信给作者] [本篇全文] [进入讨论区] [返回顶部] [修改文章] [删除
: 文章] [转寄] [转贴] [ 1 ]
: 发信人: everid (everid), 信区: Programming
: 标 题: 一道C++习题求解。
: 发信站: BBS 未名空间站 (Thu Feb 26 00:32:25 2009)
: Without including a header file, declare the function puts() from the
: Standard C Library. Call this function from main().

e****d
发帖数: 333
3
Thank you very much.
the problem is solved exactly as what you said.
I use gcc instead of g++, use .c instead of .cpp, use extern declaration
instead of namesapce. So in general, i switch from c++ to c and the problem
is solved.

【在 t****t 的大作中提到】
: this is C function, so you must declare it as
: extern "C" int puts(const char *);
: alternatively, use C compiler (gcc) and change the file name to .c as
: opposed to .cpp
: I think the question explicitly said "standard c library". use c++ here is
: 画蛇添足.

1 (共1页)
进入Programming版参与讨论
相关主题
warning: returning address of local variable or temporaryquestion about const reference
再一个问题c++which style do you prefer?
请教C++11的rvalue ref怎么隐藏c++template代码?只提供lib 不提供头文件
请教各路C++大神 为什么f(3) 输出是 'dd'有关objec access path的问题
function declarationWhy should i include .cpp instead of .h
c function 在 c里调用和C++调用结果不一样C++ 的 问题
C++ namespace 弱问C++默认的copy constructor的疑惑
c++ does not check const for extern variable?一个关于C++ template和overload的问题
相关话题的讨论汇总
话题: puts话题: function话题: std话题: main话题: const