c++一个程序问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 18:25:02
#include"stdafx.h"
#include<iostream>
#include<cstring>
using namespace std;
class Watch_err{
public:
Watch_err() {err_number=1;}//构造子函数,将err_number初始化为1
void print(string err) const//重载函数1
{cout<<"第"<<err_number++<<"次错误:"<<err<<endl;}
void print(string err)//重载函数2
{cout<<"It is"<<err_number++<<"th error:"<<err<<endl;}
private:
mutable int err_number;//mutable数据成员
};
void main()
{int i;
const Watch_err a_err;
Watch_err b_err;
a_err.print("内存溢出");//调用重载函数1
a_err.print("内存溢出");
b_err.print("内存溢出");//调用重载函数2
b_err.print("内存溢出");
}
编译时说的什么d:\program files\vc\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std

哦,一定用的VS吧。
因为你用到了
cout << err;
err的类型是string~

在标准C++中:
ostream中有这样的声明ostream & operator<<(ostream & os,const string & str);如果你用的是G++的话,不用包含#include <cstring> (根本就没有这个文件),就能用这句话了。

如果是VS的话,ostream里没有包含这个声明,但这个声明是存在于#incude <string>和namespace std中,所以你只要将<cstring>替换成<string>就行。

注:cstring中没有"<<"的重载包含也没有用。

//参看cstring
// cstring standard header
#pragma once
#ifndef _CSTRING_
#define _CSTRING_
#include <yvals.h>

#ifdef _STD_USING
#undef _STD_USING
#include <string.h>
#define _STD_USING

#else /* _STD_USING */
#include <string.h>
#endif /* _STD_USING */

#ifndef RC_INVOKED
#if _GLOBAL_USING
_STD_BEGIN
using ::size_t; using ::memchr; using ::memcmp;

using ::memcpy; using ::memmove; using ::memset;
using ::strcat; using ::strchr; using ::strcm