为什么我在 C++ 中尝试将字符串分配给 visual studio 中的 istringstream 对象时出现错误?
Why am i getting error when i try to assign string to istringstream object in visual studio in c++?
下面的 class 需要 string
行作为用户的输入。我的目标是将它存储在 istringstream iss
中,然后从 istringstream iss
中提取各种数据到各种变量中。但是,当我尝试用 string user_input
初始化 iss
时,我在编译文件时收到此错误:
error C2064: term does not evaluate to a function taking 1 arguments
我认为问题出在行 iss{user_input};
请解释为什么我会收到此错误。
_input.h
头文件
#pragma once
#include <string>
#include<sstream>
#include "_date.h"
#include "_time.h"
using namespace std;
//definition of class input
class _input
{
string user_input;
istringstream iss;
//datamembers to be sent
int esno,eday, emonth, eihours, eimins, eohours, eomins, emo;
char eiap, eoap;
string enotes;
public:
_input();
void get_input();
void process_data();
};
_input.cpp
文件
#include "_input.h"
_input::_input() : user_input { "Nothing Entered" }, iss{ "" }, esno{ 0 }, eday{ 0 }, emonth{ 0 },
eihours{ 0 }, eimins{ 0 }, eohours{ 0 }, eomins{ 0 }, emo{ 0 }, eiap{ 'n' }, eoap{ 'n' }, enotes{ "" }
{
}
void _input::get_input() // to store in iss
{
cout << "Enter the entry : Format (Date - In Time - Out Time - Money Owed - Notes)" << endl;
getline(cin, user_input);
iss{user_input}; //THIS IS WHERE I GET THE ERROR
}
void _input::process_data()
{
iss >> eday >> emonth >> eihours >> eimins >> eiap >> eohours >> eomins >> eoap >> emo >> enotes;
++esno;
}
iss{user_input}; //THIS IS WHERE I GET THE ERROR
在 get_input
中 re/construct iss
为时已晚(并且非法),语法建议如此。相反,流必须是 re/initialized 和新的 user_input
.
iss.clear();
iss.str(user_input);
简而言之,您不能初始化 一个对象两次。 iss
被声明并构造为 class 的私有成员。当您尝试 iss{user_input}
时它已经存在——这是不允许的。你不想要 Initialization, instead you simply want to replace the content of iss
with what is in user_input
. For that you use std::basic_istringstream::str。成员函数 str
允许您获取或设置 istringstream
.
的内容
一个简短的例子:
#include <iostream>
#include <sstream>
#include <string>
int main (void) {
std::istringstream iss {}; /* iss is declared/constructed */
std::string s {};
std::cout << "enter s: ";
if (!getline(std::cin, s))
return 1;
iss.str(s); /* replace iss content with s */
std::cout << "iss has: "<< iss.str() << '\n'; /* check iss contains s */
while (iss >> s) /* read each string in iss */
std::cout << s << '\n'; /* output string */
}
例子Use/Output
$ ./bin/iss
enter s: my dog has fleas
iss has: my dog has fleas
my
dog
has
fleas
额外提示,看看Why is “using namespace std;” considered bad practice?。
检查一下,如果您还有其他问题,请告诉我。
下面的 class 需要 string
行作为用户的输入。我的目标是将它存储在 istringstream iss
中,然后从 istringstream iss
中提取各种数据到各种变量中。但是,当我尝试用 string user_input
初始化 iss
时,我在编译文件时收到此错误:
error C2064: term does not evaluate to a function taking 1 arguments
我认为问题出在行 iss{user_input};
请解释为什么我会收到此错误。
_input.h
头文件
#pragma once
#include <string>
#include<sstream>
#include "_date.h"
#include "_time.h"
using namespace std;
//definition of class input
class _input
{
string user_input;
istringstream iss;
//datamembers to be sent
int esno,eday, emonth, eihours, eimins, eohours, eomins, emo;
char eiap, eoap;
string enotes;
public:
_input();
void get_input();
void process_data();
};
_input.cpp
文件
#include "_input.h"
_input::_input() : user_input { "Nothing Entered" }, iss{ "" }, esno{ 0 }, eday{ 0 }, emonth{ 0 },
eihours{ 0 }, eimins{ 0 }, eohours{ 0 }, eomins{ 0 }, emo{ 0 }, eiap{ 'n' }, eoap{ 'n' }, enotes{ "" }
{
}
void _input::get_input() // to store in iss
{
cout << "Enter the entry : Format (Date - In Time - Out Time - Money Owed - Notes)" << endl;
getline(cin, user_input);
iss{user_input}; //THIS IS WHERE I GET THE ERROR
}
void _input::process_data()
{
iss >> eday >> emonth >> eihours >> eimins >> eiap >> eohours >> eomins >> eoap >> emo >> enotes;
++esno;
}
iss{user_input}; //THIS IS WHERE I GET THE ERROR
在 get_input
中 re/construct iss
为时已晚(并且非法),语法建议如此。相反,流必须是 re/initialized 和新的 user_input
.
iss.clear();
iss.str(user_input);
简而言之,您不能初始化 一个对象两次。 iss
被声明并构造为 class 的私有成员。当您尝试 iss{user_input}
时它已经存在——这是不允许的。你不想要 Initialization, instead you simply want to replace the content of iss
with what is in user_input
. For that you use std::basic_istringstream::str。成员函数 str
允许您获取或设置 istringstream
.
一个简短的例子:
#include <iostream>
#include <sstream>
#include <string>
int main (void) {
std::istringstream iss {}; /* iss is declared/constructed */
std::string s {};
std::cout << "enter s: ";
if (!getline(std::cin, s))
return 1;
iss.str(s); /* replace iss content with s */
std::cout << "iss has: "<< iss.str() << '\n'; /* check iss contains s */
while (iss >> s) /* read each string in iss */
std::cout << s << '\n'; /* output string */
}
例子Use/Output
$ ./bin/iss
enter s: my dog has fleas
iss has: my dog has fleas
my
dog
has
fleas
额外提示,看看Why is “using namespace std;” considered bad practice?。
检查一下,如果您还有其他问题,请告诉我。