boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 不用头文件,如何调用函数?C++
相关主题
function declaration
c function 在 c里调用和C++调用结果不一样
C++ namespace 弱问
c++ does not check const for extern variable?
question about const reference
which style do you prefer?
怎么隐藏c++template代码?只提供lib 不提供头文件
有关objec access path的问题
Why should i include .cpp instead of .h
C++ 的 问题
相关话题的讨论汇总
话题: 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版参与讨论
相关主题
C++ 的 问题
C++默认的copy constructor的疑惑
一个关于C++ template和overload的问题
why copy assignment operator returns non-const type?
C++ template function type
一个诡异的const_cast问题
warning: returning address of local variable or temporary
再一个问题c++
请教C++11的rvalue ref
请教各路C++大神 为什么f(3) 输出是 'dd'
相关话题的讨论汇总
话题: puts话题: function话题: std话题: main话题: const