2010-07-27 24 views
0

這裏是將輸入字符串複製到緩衝區的代碼問題是如何使代碼顯示我輸出的結果? 代碼字符串上的c函數

#include <stdio.h> 
#include <iostream> 
using namespace std; 
using std::memset; 
using std::cout; 
using std::endl; 
void foo(char *s){ 
    char buf[15]; 
    memset(buf,0,sizeof(buf)); 
    strncat(buf,s,sizeof(buf)-1); 



    cout<<buf; 


} 
int main(){ 


    char *s="dato"; 
     cout<<foo(s)<<endl; 




    return 0; 
} 

錯誤

1>------ Build started: Project: memory_buffer, Configuration: Debug Win32 ------ 
1> memory_buffer.cpp 
1>c:\users\david\documents\visual studio 2010\projects\memory_buffer\memory_buffer.cpp(10): warning C4996: 'strncat': This function or variable may be unsafe. Consider using strncat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 
1>   c:\program files\microsoft visual studio 10.0\vc\include\string.h(175) : see declaration of 'strncat' 
1>c:\users\david\documents\visual studio 2010\projects\memory_buffer\memory_buffer.cpp(22): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(726): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(764): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(811): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(937): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(944): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(951): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(958): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(968): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>,std::string>(std::basic_ostream<_Elem,_Traits> &&,_Ty)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ty=std::string 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(186): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(192): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(199): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(206): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(226): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(260): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(280): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(305): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(325): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(386): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(407): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(427): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(447): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(467): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(487): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   while trying to match the argument list '(std::ostream, std::string)' 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+3

爲什麼不使用'標準: :string' ...? – kennytm 2010-07-27 12:00:59

+0

@Kenny Wow,他也有很多代表。 – NullUserException 2010-07-27 12:03:22

+0

@KennyTM:沒有人在現實世界中使用'std :: string'。人們只使用'char *'和原始指針。這同樣適用於類,也沒有人使用類。 – ereOn 2010-07-27 12:04:05

回答

3

您不必創建一個函數來打印一個char *的輸出,只需使用:

char *s = "dato"; 
cout << s << endl; 

甚至更​​好:

cout << "dato" << endl; 

與您的代碼的問題是,你嘗試打印一個void(即沒有)到輸出 - 因爲你的函數foo返回void。

而且你不應該使用的char *在C++程序,使用std ::字符串,而不是因爲它是更安全,你可以擺脫任何緩衝副本:

#include <string> 
... 

std::string s = "dato"; 
std::string s2 = s; 
cout << s2 << endl; // Prints dato 
1

刪除以下行會有所幫助。

cout<<foo(s)<<endl; 

函數foo返回void,並且cout不能打印void。既然你已經打印了foo(),這個語句根本就不需要。