gpt4 book ai didi

c++ - 重载输出运算符时出现错误 c2440 错误

转载 作者:行者123 更新时间:2023-11-27 23:23:21 25 4
gpt4 key购买 nike

我正在编译以下代码并收到以下错误。如何解决这个问题?感谢您的帮助。

error C2079: 'issline' uses undefined class 'std::basic_istringstream<_Elem,_Traits,_Alloc>' 1> with 1>
[ 1> _Elem=char, 1>
_Traits=std::char_traits, 1> _Alloc=std::allocator 1> ] 1>d:\technical\c++study\readparsing\readparsing\main.cpp(49) : error C2440: 'initializing' : cannot convert from 'std::string' to 'int' 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1>d:\technical\c++study\readparsing\readparsing\main.cpp(51) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion) 1>
d:\technical\c++study\readparsing\readparsing\timestamp.h(31): could be 'std::istream &operator >>(std::istream &,TimeStamp &)' 1>
while trying to match the argument list '(int, TimeStamp)'

我在 TimeStamp.h 中有以下代码

#ifndef __TIMESTAMP_
#define __TIMESTAMP_

#include <iostream>

struct DateTime
{
unsigned int dwLowDateTime;
unsigned int dwHighDateTime;
};


class TimeStamp
{
public:
TimeStamp()
{
m_time.dwHighDateTime = 0;
m_time.dwLowDateTime = 0;
}

TimeStamp& operator = (unsigned __int64 other)
{
*( unsigned __int64*)&m_time = other;
return *this;
}
private:
DateTime m_time;
};

std::istream& operator >> (std::istream& input, TimeStamp& timeStamp);

#endif

在 main.cpp 中我有以下内容

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "TimeStamp.h"

std::istream& operator >> (std::istream& input, TimeStamp& timeStamp)
{
// 1.
// use regular stream operator parsing technique to parse individual integer x values (separated in the form "xxxx-xx-xx xx:xx:xx.xxx")
// for year, month, day, hour, minute, seconds, mseconds
unsigned int year;
unsigned int month;
unsigned int day;
unsigned int hour;
unsigned int minute;
unsigned int seconds;
unsigned int milliSeconds;
char dash;
char colon;

input >> year >> dash >> month >> dash >> day >> hour >> colon >> minute >> colon >> seconds >> colon >> milliSeconds;

cout << "Time stamp opeator is called " << std::endl;

// 2.
// code to be written.

return input;
}



int main () {

std::string dateTime = "2012-06-25 12:00:10.000";

TimeStamp myTimeStamp;

std::istringstream issline(dateTime);

issline >> myTimeStamp;


return 0;
}

最佳答案

我认为你需要 #include <sstream>使用 istringstream .

关于c++ - 重载输出运算符时出现错误 c2440 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com