c++ - 将 std::istringstream 作为参数传递无效

标签 c++

我刚开始学习 C++ 11,我有这个头文件:

#pragma once
#include <string>
#include <fstream>
#include <sstream>

class Parser
{
public:
    Parser();
    ~Parser();

    void Parse(const std::string& path);

private:

    std::ifstream inFile;

    void LoadFile(const std::string& path);

    void Process(const std::istringstream& in);
};

还有这段代码:

void Parser::LoadFile(const std::string& path)
{
    if (!Exists(path))
        throw std::exception("File not found.");
    else
    {
        inFile.open(path);

        std::string line;
        while (std::getline(inFile, line))
        {
            // Input stream.
            std::istringstream iss(line);
            Process(&iss);
        }
    }
}

void Parser::Process(const std::istringstream& in)
{
}

但我在 Process(&iss); 行出现以下错误:

there is no proper constructor to convert from "std::istringstream *" to "std::basic_istringstream <char, std::char_traits , std::allocator >"

我必须如何声明 Process 方法?

最佳答案

Process(&iss);

iss 是一个 std::istringstream。在表达式中使用时,& 是寻址运算符。

&iss 表达式的结果是指向 std::istringstreamstd::istringstream * 的指针。

void Process(const std::istringstream& in);

这将此函数声明为将对 const std::istringstream &引用作为参数,而不是指针。

声明的上下文中,& 表示引用类型

& 在表达式或声明中使用时有不同的含义。事实上,& 在表达式中可能有完全不同的含义,具体取决于它出现的位置。

关于c++ - 将 std::istringstream 作为参数传递无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68510870/

相关文章:

c++ - 线段树标准模板库

c++ - 无法从文件中读取,奇怪的行为

c++ - Nginx - Cygwin 中的 nchan 模块

c++ - 在 opengl 中应用转换函数后未显示对象

c++ - 使用 #define 指令作为代码的 "part",而不是在文件顶部

c++ - 在 STL 列表中找到一对,其中只有第一个元素是已知的

c++ - 如何优化简单的高斯滤波器的性能?

c++ - XSD : How to set attributes values in children element type?

c++ - 将 time_t 转换为 int

c++ - 我可以直接从 C++ 中的 vector 元素指针获取索引吗?