c++ - 第一个 C++ 类(class),对某些事情不确定。特别是“const std::string& 提示

标签 c++ c++11 parameters

这是我的第一份作业,我有点困惑。它应该询问最喜欢的颜色,整数,然后 float ,然后打印颜色和 float (整数)次数。我创建了一些文件,我相信#included它们是正确的,但是出了问题。我知道它不会起作用,我只是想让它在我“制作”它时做一些事情,但我不知道如何继续前进。

#include <iostream>
#include "image_menu.h"
#include <string>

std::string getString(std::istream& is, std::ostream& os, const std::string& prompt) {
    std::string color;
    os << "What's your favorite color? ";
    is >> color;
    return color;
}

int getInteger(std::istream& is, std::ostream& os, const std::string& prompt) {
    int num;
    os << "What's your favorite number? ";
    is >> num;
    return num;
}

double getDouble(std::istream& is, std::ostream& os, const std::string& prompt) {
    double dec;
    os << "What's your favorite float? ";
    is >> dec;
    return dec;
}

int assignment1(std::istream& is, std::ostream& os){
    getString(is, os);
    getInteger(is, os);
    getDouble(is, os);
    return num;

我的主要

#include "image_menu.h"
#include <iostream>

int main() {
    assignment1(std::cin, std::cout);
    return 0;
}
#ifndef _IMAGE_MENU_H_
#define _IMAGE_MENU_H_
#include <iostream>
std::string getString( std::istream& is, std::ostream& os, const std::string& prompt );
int getInteger( std::istream& is, std::ostream& os, const std::string& prompt );
double getDouble( std::istream& is, std::ostream& os, const std::string& prompt );
int assignment1( std::istream& is, std::ostream& os );
#endif // _IMAGE_MENU_H_

和我的 Makefile

all: assignment

assignment: ppm_menu.o image_menu.o
    g++ -std=c++11 -o assignment ppm_menu.o image_menu.o

ppm_menu.o: ppm_menu.cpp image_menu.h
    g++ -std=c++11 -c ppm_menu.cpp

image_menu.o: image_menu.cpp image_menu.h
    g++ -std=c++11 -c image_menu.cpp

我知道让你们看一下它要求很高。我并不是要求你帮我做作业,只是为了帮助我理解 const std::string& Prompt 的东西。我有足够的基本了解,我认为可以完成作业,只是不确定如何处理该参数。 (函数名和参数都是老师给的,我们自己做)

非常感谢您的帮助!

有关我遇到问题的文件的说明

std::string getString( std::istream& is, std::ostream& os, const std::string& prompt ); This function must display the prompt to the output stream, read a string response from the input stream and return the string.
int getInteger( std::istream& is, std::ostream& os, const std::string& prompt ); This function must display the prompt to the output stream, read an integer response from the input stream and return the integer.
double getDouble( std::istream& is, std::ostream& os, const std::string& prompt ); This function must display the prompt to the output stream, read a double precision floating point response from the input stream and return the double.
int assignment1( std::istream& is, std::ostream& os ); This function must get a string from the input stream, using the prompt “What’s your favorite color? “, an integer using the prompt “What’s your favorite integer? “, and a double precision floating point number using the prompt “What’s your favorite number? “. It must then repeatedly send the line described above in the assignment description to the output stream. Returns the integer number given by the user.

最佳答案

您可能会注意到的第一件事是您的代码无法编译:

getString(is, os);

该函数需要三个参数,但您只提供了两个。至少,您会提供一些虚拟值,例如。 G。空字符串:

getString(is, os, "");

由于有一个非显式构造函数接受 std::stringchar const*,因此将创建一个临时对象,该对象将在完成后立即再次销毁上面的表达式(即函数返回之后)。

到目前为止,都是技术方面的内容。但是,在进一步的任务中可能会要求您提供不同的字符串。像名字、姓氏、你居住的城市……你想为所有这些编写单独的函数吗?提示开始发挥作用:提供您想要提出的实际问题作为提示。那么 getString 可能看起来像这样:

std::string getString(std::istream& is, std::ostream& os, const std::string& prompt)
{
    std::string color; // perhaps rather name it 'value', you won't be asking
                       // for colors all the time...
    os << prompt << ' ';
    is >> color;
    return color;
}

你可以这样调用它:

getString(is, os, "What's your favorite color?");

其他函数类似。

<小时/>

旁注(编辑问题后):这确实是任务实际需要的......

关于c++ - 第一个 C++ 类(class),对某些事情不确定。特别是“const std::string& 提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59702822/

相关文章:

c++ - std::piecewise_construct 语法如何工作?

c# - 在 C# 中使用反射确定参数是否使用 "params"?

Java 选择性泛型

c++ - 什么时候写异步日志

c++ bool 连接 |=

c++ - 尝试设置 char 数组的字符时程序崩溃

c++ - 为什么填充我的 std::vector 的运行时间在 0 到 ~16 毫秒之间跳跃?

templates - 模板上的 begin() 和 end() 自由函数重载

c++ - 静态链接ActiveMQ-cpp

parameters - Facebook "like"在 url ex : fb_action_ids fb_action_types 中添加额外参数