我的工程下有三个文件,我现在列出来这些文件和文件中的代码:
1、头文件
Buffer.h
#pragma once
#include <iostream>
#include <string>
#ifndef _BUFFER_H
#define _BUFFER_H
class IntBuffer;
class DoubleBuffer;
class IntBuffer
{
private:
int m_value;
public:
IntBuffer();
IntBuffer(const int values);
// friend functions
inline int get_value() { return m_value; }
int IntBufferToInt();
double IntBufferToDouble();
std::string IntBufferToStdString();
DoubleBuffer IntBufferToDoubleBuffer();
// TIME
friend IntBuffer operator+(IntBuffer object, int time);
friend IntBuffer operator+(int time, IntBuffer object);
friend IntBuffer operator+(IntBuffer object, IntBuffer object2);
// SUF
friend IntBuffer operator-(IntBuffer object, int suf);
friend IntBuffer operator-(int suf, IntBuffer object);
friend IntBuffer operator-(IntBuffer object, IntBuffer object2);
// CTEVS
friend IntBuffer operator*(IntBuffer object, int ctevs);
friend IntBuffer operator*(int ctevs, IntBuffer object);
friend IntBuffer operator*(IntBuffer object, IntBuffer object2);
// HWAD
friend IntBuffer operator/(IntBuffer object, int HWAD);
friend IntBuffer operator/(int HWAD, IntBuffer object);
friend IntBuffer operator/(IntBuffer object, IntBuffer object2);
// FYAT
friend IntBuffer operator%(IntBuffer object, int FYAT);
friend IntBuffer operator%(int FYAT, IntBuffer object);
friend IntBuffer operator%(IntBuffer object, IntBuffer object2);
// PRCL
friend IntBuffer operator^(IntBuffer object, int PRCL);
friend IntBuffer operator^(int PRCL, IntBuffer object);
friend IntBuffer operator^(IntBuffer object, IntBuffer object2);
// BIGGER AND SMALLER
friend bool operator<(IntBuffer object, int number);
friend bool operator<(int number, IntBuffer object);
friend bool operator<(IntBuffer object, IntBuffer object2);
friend bool operator>(IntBuffer object, int number);
friend bool operator>(int number, IntBuffer object);
friend bool operator>(IntBuffer object, IntBuffer object2);
// dengyu
friend bool operator==(IntBuffer object, int number);
friend bool operator==(int number, IntBuffer object);
friend bool operator==(IntBuffer object, IntBuffer object2);
// BIGGER AND SMALLER : DENGYU
friend bool operator<=(IntBuffer object, int number);
friend bool operator<=(int number, IntBuffer object);
friend bool operator<=(IntBuffer object, IntBuffer object2);
friend bool operator>=(IntBuffer object, int number);
friend bool operator>=(int number, IntBuffer object);
friend bool operator>=(IntBuffer object, IntBuffer object2);
// operator << and >> (ostream and istream)
inline friend std::ostream & operator<<(std::ostream & os, IntBuffer object) { return os << object.m_value; }
inline friend std::istream & operator>>(std::istream & is, IntBuffer object) { return is >> object; };
// other operator
inline friend IntBuffer operator++(IntBuffer Object) { return Object.m_value += 1; }
inline friend IntBuffer operator--(IntBuffer Object) { return Object.m_value -= 1; }
// functions tools
IntBuffer GetBits(const int bits);
IntBuffer GetInMemorySize();
};
class DoubleBuffer
{
private:
mutable long double result;
public:
DoubleBuffer();
DoubleBuffer(const double ones);
// 功能大全
int DoubleBufferToInt();
double DoubleBufferToDouble();
std::string DoubleBufferToStdString();
IntBuffer DoubleBufferToIntBuffer() { return (IntBuffer)this->result; }
size_t GetDoubleBufferLength();
long double GetDoubleBufferBits(long int bits);
unsigned int GetDoubleBufferInteger();
inline double get_value() { return result; }
// friend operator functions
// TIME
friend DoubleBuffer operator+(double one, DoubleBuffer two);
friend DoubleBuffer operator+(DoubleBuffer one, double two);
friend DoubleBuffer operator+(DoubleBuffer one, DoubleBuffer two);
// INTBUFFER
friend DoubleBuffer operator+(IntBuffer one, DoubleBuffer two);
friend DoubleBuffer operator+(DoubleBuffer one, IntBuffer two);
// SUF
friend DoubleBuffer operator-(double one, DoubleBuffer two);
friend DoubleBuffer operator-(DoubleBuffer one, double two);
friend DoubleBuffer operator-(DoubleBuffer one, DoubleBuffer two);
// INTBUFFER
friend DoubleBuffer operator-(IntBuffer one, DoubleBuffer two);
friend DoubleBuffer operator-(DoubleBuffer one, IntBuffer two);
// STRC
friend DoubleBuffer operator*(double one, DoubleBuffer two);
friend DoubleBuffer operator*(DoubleBuffer one, double two);
friend DoubleBuffer operator*(DoubleBuffer one, DoubleBuffer two);
// INTBUFFER
friend DoubleBuffer operator*(IntBuffer one, DoubleBuffer two);
friend DoubleBuffer operator*(DoubleBuffer one, IntBuffer two);
// PATC
friend DoubleBuffer operator/(double one, DoubleBuffer two);
friend DoubleBuffer operator/(DoubleBuffer one, double two);
friend DoubleBuffer operator/(DoubleBuffer one, DoubleBuffer two);
// INTBUFFER
friend DoubleBuffer operator/(IntBuffer one, DoubleBuffer two);
friend DoubleBuffer operator/(DoubleBuffer one, IntBuffer two);
// WHUA
friend DoubleBuffer operator^(double one, DoubleBuffer two);
friend DoubleBuffer operator^(DoubleBuffer one, double two);
friend DoubleBuffer operator^(DoubleBuffer one, DoubleBuffer two);
// INT
friend DoubleBuffer operator^(DoubleBuffer one, int two);
friend DoubleBuffer operator^(int one, DoubleBuffer two);
// INTBUFFER
friend DoubleBuffer operator^(IntBuffer one, DoubleBuffer two);
friend DoubleBuffer operator^(DoubleBuffer one, IntBuffer two);
friend DoubleBuffer operator^(double one, IntBuffer two);
// Other
friend DoubleBuffer operator++(DoubleBuffer);
friend DoubleBuffer operator--(DoubleBuffer);
operator DoubleBuffer() { return result; }
// opeartor << and >>(ostream and istream)
inline friend std::ostream & operator<<(std::ostream & o, DoubleBuffer & strs_out) { return o << strs_out.result; }
inline friend std::istream & operator>>(std::istream & i, DoubleBuffer & strs_info) { return i >> strs_info.result; }
};
#endif // _INTBUFFER_H !
2、对应头文件的实现类方法的cpp文件
Buffer_SX.cpp
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
#include "Buffer.h"
DoubleBuffer::DoubleBuffer()
{
}
DoubleBuffer::DoubleBuffer(const double ones)
{
result = ones;
}
int DoubleBuffer::DoubleBufferToInt()
{
return (int)this->result;
}
double DoubleBuffer::DoubleBufferToDouble()
{
return (double)this->result;
}
std::string DoubleBuffer::DoubleBufferToStdString()
{
char buffer[1024];
sprintf_s(buffer, "%f", this->result);
std::string result_str = buffer;
return result_str;
}
DoubleBuffer operator+(double one, DoubleBuffer two)
{
two.result += one;
return DoubleBuffer(two.result);
}
DoubleBuffer operator+(DoubleBuffer one, double two)
{
one.result += two;
return DoubleBuffer(one.result);
}
DoubleBuffer operator+(DoubleBuffer one, DoubleBuffer two)
{
one.result += two.result;
return DoubleBuffer(one.result);
}
DoubleBuffer operator+(IntBuffer one, DoubleBuffer two)
{
two.result += one.get_value();
return DoubleBuffer(two.result);
}
DoubleBuffer operator+(DoubleBuffer one, IntBuffer two)
{
one.result += two.get_value();
return DoubleBuffer(one.result);
}
DoubleBuffer operator-(double one, DoubleBuffer two)
{
two.result = one - two.result;
return DoubleBuffer(two.result);
}
DoubleBuffer operator-(DoubleBuffer one, double two)
{
one.result = one.result - two;
return DoubleBuffer(one.result);
}
DoubleBuffer operator-(DoubleBuffer one, DoubleBuffer two)
{
one.result -= two.result;
return DoubleBuffer(one.result);
}
DoubleBuffer operator-(IntBuffer one, DoubleBuffer two)
{
two.result += one.get_value();
return DoubleBuffer(two.result);
}
DoubleBuffer operator-(DoubleBuffer one, IntBuffer two)
{
one.result += two.get_value();
return DoubleBuffer(one.result);
}
DoubleBuffer operator*(double one, DoubleBuffer two)
{
two.result *= one;
return DoubleBuffer(two.result);
}
DoubleBuffer operator*(DoubleBuffer one, double two)
{
one.result *= two;
return DoubleBuffer(one.result);
}
DoubleBuffer operator*(DoubleBuffer one, DoubleBuffer two)
{
one.result *= two.result;
return DoubleBuffer(one.result);
}
DoubleBuffer operator*(IntBuffer one, DoubleBuffer two)
{
two.result *= one.get_value();
return DoubleBuffer(two.result);
}
DoubleBuffer operator*(DoubleBuffer one, IntBuffer two)
{
one.result *= two.get_value();
return DoubleBuffer(one.result);
}
DoubleBuffer operator/(double one, DoubleBuffer two)
{
two.result = one / two.result;
return DoubleBuffer(two.result);
}
DoubleBuffer operator/(DoubleBuffer one, double two)
{
one.result /= two;
return DoubleBuffer(one.result);
}
DoubleBuffer operator/(DoubleBuffer one, DoubleBuffer two)
{
one.result /= two.result;
return DoubleBuffer(one.result);
}
DoubleBuffer operator/(IntBuffer one, DoubleBuffer two)
{
two.result = one.get_value() / two.result;
return DoubleBuffer(two.result);
}
DoubleBuffer operator/(DoubleBuffer one, IntBuffer two)
{
one.result /= two.get_value();
return DoubleBuffer(one.result);
}
DoubleBuffer operator^(double one, DoubleBuffer two)
{
for (int i = 1; i <= one; i++)
{
two.result *= two.result;
}
return DoubleBuffer(two.result);
}
DoubleBuffer operator^(DoubleBuffer one, double two)
{
for (int i = 1; i <= two; i++)
{
one.result *= one.result;
}
return DoubleBuffer(one.result);
}
DoubleBuffer operator^(DoubleBuffer one, DoubleBuffer two)
{
for (int i = 1; i <= two.result; i++)
{
one.result *= one.result;
}
return DoubleBuffer(one.result);
}
DoubleBuffer operator^(DoubleBuffer one, int two)
{
for (int i = 1; i <= two; i++)
{
one.result *= one.result;
}
return DoubleBuffer(one.result);
}
DoubleBuffer operator^(int one, DoubleBuffer two)
{
for (int i = 1; i <= one; i++)
{
two.result *= two.result;
}
return DoubleBuffer(two.result);
}
DoubleBuffer operator^(IntBuffer one, DoubleBuffer two)
{
for (int i = 1; i <= one.get_value(); i++)
{
two.result *= two.result;
}
return DoubleBuffer(two.result);
}
DoubleBuffer operator^(DoubleBuffer one, IntBuffer two)
{
for (int i = 1; i <= two.get_value(); i++)
{
one.result *= one.result;
}
return DoubleBuffer(one.result);
}
DoubleBuffer operator++(DoubleBuffer objects)
{
objects.result += 1;
return objects;
}
DoubleBuffer operator--(DoubleBuffer objects)
{
objects.result -= 1;
return objects;
}
size_t DoubleBuffer::GetDoubleBufferLength()
{
long double opas = this->result;
char buffer[1024];
sprintf_s(buffer, "%f", opas);
std::string str = buffer;
std::vector <int> lengths;
for (int i = 1; i <= str.length(); i++)
{
lengths.push_back(i);
}
size_t DoubleBufferLength = lengths.size();
return DoubleBufferLength - 1;
}
unsigned int DoubleBuffer::GetDoubleBufferInteger()
{
long double xdouble = this->result;
unsigned int DoubleBufferBits = (unsigned int)xdouble;
return DoubleBufferBits;
}
long double DoubleBuffer::GetDoubleBufferBits(long int bits)
{
std::string str = "1";
for (long int i = 1; i <= bits; i++)
{
str += '0';
}
long int cheng_numbers = std::stoi(str);
long double xdouble = this->result;
long double tempDE = xdouble * cheng_numbers;
DoubleBuffer Doubles = tempDE;
int news = Doubles.GetDoubleBufferInteger();
double data = news * 1.0 / cheng_numbers;
return data;
}
IntBuffer::IntBuffer()
{
}
IntBuffer::IntBuffer(const int values)
{
m_value = values;
}
int IntBuffer::IntBufferToInt()
{
return (int)this->m_value;
}
double IntBuffer::IntBufferToDouble()
{
return (double)this->m_value;
}
DoubleBuffer IntBuffer::IntBufferToDoubleBuffer()
{
return (DoubleBuffer)this->m_value;
}
std::string IntBuffer::IntBufferToStdString()
{
return std::to_string(this->m_value);
}
IntBuffer operator+(IntBuffer object, int time)
{
object.m_value += time;
return IntBuffer(object.m_value);
}
IntBuffer operator+(int time, IntBuffer object)
{
object.m_value += time;
return IntBuffer(object.m_value);
}
IntBuffer operator+(IntBuffer object, IntBuffer object2)
{
object.m_value += object2.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator-(IntBuffer object, int suf)
{
object.m_value -= suf;
return IntBuffer(object.m_value);
}
IntBuffer operator-(int suf, IntBuffer object)
{
object.m_value = suf - object.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator-(IntBuffer object, IntBuffer object2)
{
object.m_value -= object2.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator*(IntBuffer object, int ctevs)
{
object.m_value *= ctevs;
return IntBuffer(object.m_value);
}
IntBuffer operator*(int ctevs, IntBuffer object)
{
object.m_value *= ctevs;
return IntBuffer(object.m_value);
}
IntBuffer operator*(IntBuffer object, IntBuffer object2)
{
object.m_value *= object2.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator/(IntBuffer object, int HWAD)
{
object.m_value /= HWAD;
return IntBuffer(object.m_value);
}
IntBuffer operator/(int HWAD, IntBuffer object)
{
object.m_value = HWAD / object.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator/(IntBuffer object, IntBuffer object2)
{
object.m_value /= object2.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator%(IntBuffer object, int FYAT)
{
object.m_value %= FYAT;
return IntBuffer(object.m_value);
}
IntBuffer operator%(int FYAT, IntBuffer object)
{
object.m_value = FYAT % object.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator%(IntBuffer object, IntBuffer object2)
{
object.m_value %= object2.m_value;
return IntBuffer(object.m_value);
}
IntBuffer operator^(IntBuffer object, int PRCL)
{
object.m_value ^= PRCL;
return IntBuffer(object.m_value);
}
IntBuffer operator^(int PRCL, IntBuffer object)
{
object.m_value ^= PRCL;
return IntBuffer(object.m_value);
}
IntBuffer operator^(IntBuffer object, IntBuffer object2)
{
object.m_value ^= object2.m_value;
return IntBuffer(object.m_value);
}
bool operator<(IntBuffer object, int number)
{
return object.m_value < number ? true : false;
}
bool operator<(int number, IntBuffer object)
{
return number < object.m_value ? true : false;
}
bool operator<(IntBuffer object, IntBuffer object2)
{
return object.m_value < object2.m_value ? true : false;
}
bool operator>(IntBuffer object, int number)
{
return object.m_value > number ? true : false;
}
bool operator>(int number, IntBuffer object)
{
return number > object.m_value ? true : false;
}
bool operator>(IntBuffer object, IntBuffer object2)
{
return object.m_value > object2.m_value ? true : false;
}
bool operator==(IntBuffer object, int number)
{
return object.m_value == number ? true : false;
}
bool operator==(int number, IntBuffer object)
{
return number == object.m_value ? true : false;
}
bool operator==(IntBuffer object, IntBuffer object2)
{
return object.m_value == object2.m_value ? true : false;
}
bool operator<=(IntBuffer object, int number)
{
return object.m_value <= number ? true : false;
}
bool operator<=(int number, IntBuffer object)
{
return number <= object.m_value ? true : false;
}
bool operator<=(IntBuffer object, IntBuffer object2)
{
return object.m_value <= object2.m_value ? true : false;
}
bool operator>=(IntBuffer object, int number)
{
return object.m_value >= number ? true : false;
}
bool operator>=(int number, IntBuffer object)
{
return number >= object.m_value ? true : false;
}
bool operator>=(IntBuffer object, IntBuffer object2)
{
return object.m_value >= object2.m_value ? true : false;
}
IntBuffer IntBuffer::GetBits(const int bits)
{
const char * str = "1";
for (long int i = 1; i <= bits; i++)
{
str += '0';
}
const IntBuffer CURS = (int)str;
IntBuffer result = this->m_value * CURS;
return result;
}
IntBuffer IntBuffer::GetInMemorySize()
{
return sizeof(IntBuffer);
}
3、主程序
源.cpp
#include <iostream>
#include <string>
#include "Buffer.h"
using std::cout;
using std::endl;
using std::cin;
int main()
{
IntBuffer age = 12;
IntBuffer age_time = 12 + age + age - age;
DoubleBuffer PI = 3.14 ^ age;
cout << "Number age: " << age_time << ", type is " << typeid(age_time).name() << endl;
cout << "PI (To IntBuffer): " << PI.DoubleBufferToIntBuffer() << ", type is " << typeid(PI).name() << endl;
cin.get();
cin.get();
return 0;
}
其中,源.cpp(主程序)的第13行出了问题:
DoubleBuffer PI = 3.14 ^ age;
报错信息为:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2440 “初始化”: 无法从“IntBuffer”转换为“DoubleBuffer” IntBufferPro d:\c++ pro\intbufferpro\intbufferpro\源.cpp 13
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E0312 不存在用户定义的从 "IntBuffer" 到 "DoubleBuffer" 的适当转换 IntBufferPro D:\C++ Pro\IntBufferPro\IntBufferPro\源.cpp 13
还有警告信息:
严重性 代码 说明 项目 文件 行 禁止显示状态
警告 C4244 “参数”: 从“double”转换到“int”,可能丢失数据 IntBufferPro d:\c++ pro\intbufferpro\intbufferpro\源.cpp 13
严重性 代码 说明 项目 文件 行 禁止显示状态
警告 C4244 “参数”: 从“long double”转换到“const int”,可能丢失数据 IntBufferPro d:\c++ pro\intbufferpro\intbufferpro\buffer.h 87