它是如何工作的?
struct Person
{
std::string name;
std::string address;
};
std::istream &read(std::istream &is, Person &person)
{
is>> person.name;
getline(is, person.address);
return is;
}
int main()
{
Person p;
read(cin,p);
}
Run Code Online (Sandbox Code Playgroud)
怎么return is回归两者person.name 和person.address一起getline似乎是分开的is?
问题在于main我想在返回 const 引用的函数set()上调用函数。display()
#include "stdafx.h"
#include "iostream"
#include "string"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostream;
class screen
{
public:
typedef string::size_type pos;
screen() = default;
screen(pos ht, pos wd, char c) :height(ht), width(ht),contents(ht*wd, c){}
screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' '){}
char get()const{ return contents[cursor]; }
inline char get(pos r, pos c)const;
screen &move(pos r, pos c);
screen &set(char);
screen &set(pos r, pos c,char);
const screen& display(ostream &os)const;
pos …Run Code Online (Sandbox Code Playgroud) 我已经读过constexpr函数返回类型可以是非常量的,在我的书中也有这样的代码:
constexpr bool isShorter(const string& str1, const string& str2)
{
return str1.size() < str2.size();
}
Run Code Online (Sandbox Code Playgroud)
但是在str1.size()这下面有一个错误:constexpr函数返回是非常量的.根据这本书,它必须是正确的,但编译器不同意.
每次我constexpr在不同的代码中使用时总会出现这样的错误:缺少类型说明符 - 假设为int.注意:C++不支持default-int.但我不知道这意味着什么.