0

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

错误 C2079:'issline' 使用未定义的类 'std::basic_istringstream<_Elem,_Traits,_Alloc>' 1> 和 1>
[ 1> _Elem=char, 1>
_Traits=std::char_traits, 1> _Alloc=std::分配器 1> ] 1>d:\technical\c++study\readparsing\readparsing\main.cpp(49) : 错误 C2440: 'initializing' : 无法从 'std::string' 转换为 'int' 1> 否可以执行此转换的用户定义的转换运算符可用,或者无法调用该运算符 1>d:\technical\c++study\readparsing\readparsing\main.cpp(51) : error C2678: binary '>>' : 未找到采用“int”类型的左操作数的运算符(或没有可接受的转换) 1>
d:\technical\c++study\readparsing\readparsing\timestamp.h(31): could be 'std::istream &operator >>(std::istream &,TimeStamp &)' 1>
同时尝试匹配参数列表'(int,时间戳)'

我在 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;
}
4

2 回答 2

3

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

于 2012-06-28T12:39:03.673 回答
1

你需要

#include <sstream>

在 main.cpp 中。错误消息说您正在使用已(转发)声明但未定义的类。通常这意味着你缺少一个包含。

请注意,#include <iosfwd>在 TimeStamp.h 中就足够了,而不是#include <iostream>

于 2012-06-28T12:46:16.937 回答