我真的不知道为什么,但我正在努力让我的>>运算符让它与getline正常工作。基本上,我有一个命令类,我想在其中将用户输入分配给其 chain
属性,然后我想将用户输入与空格作为分隔符拆分为其字符串数组param
只想让第一个正常工作,对于拆分,我稍后会用strok
来做.
编辑:
我的错误:
Commande.cpp: In function 'std::istream& operator>>(std::istream&, Commande&)':
Commande.cpp:82:39: error: no matching function for call to 'std::basic_istream<char>::getline(std::__cxx11::string&, int)'
stream.getline(commande.chaine, 256);
命令.cpp
Command::Command() {
}
Command::Command(std::string _chain) {
chaine = _chain;
}
Command::Command(const Command& orig) {
}
Command::~Command() {
}
std::istream& operator >> (std::istream &stream, Command& command)
{
stream.getline(command.chain, 256);
return stream;
}
命令.h
#ifndef COMMAND_H
#define COMMAND_H
#include <iostream>
#include <assert.h>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include "Command.h"
public:
std::string chain;
std::string param[10];
Command();
Command(std::string _chain);
Command(const Command& orig);
virtual ~Command();
private:
friend std::istream& operator >> (std::istream&, Command&);
};
#endif /* COMMAND_H */
主.cpp
include "command.h"
int main(int argc, char** argv) {
Command command;
std::cin >> command;
}
我按照马修斯的建议改变了我的喜好@Thomas它工作得很好。 对所有其他答案也竖起大拇指。
std::istream& operator >> (std::istream &stream, Command& command)
{
std::getline(stream, command.chain);
return stream;
}