【问题标题】:C++ protected typedef as return valueC++ 保护 typedef 作为返回值
【发布时间】:2017-10-01 13:55:17
【问题描述】:

我不确定这是否可能,但基本上我有这个指针 typedef,它在我的线程之间共享的一个类中受到保护,我想创建一个返回该 typedef 指针的函数。

我可以毫无问题地在我的 .h 中声明该函数,但 .cpp 说它无法识别它。我也可以在函数中使用我的 typedef。我说的唯一我不能做的就是将它作为返回值。

我怀疑你们甚至必须看到代码,但我发布它只是为了让它完整。

共享标头:

#pragma once


class CR
{


public:


private:

public:

    int c_outPut(std::string &output);

protected:
    typedef std::unordered_map<in_addr, SOCKET>::const_iterator cit;
};

另一个.h的短版

#pragma once
#include "commonRec.h"

#include <unordered_map>
#include <WinSock2.h>
#include <iterator>

class CH : CR
{

    std::string sendToCon(cit &const_it, const std::string &command);
    cit findHost(std::string &searchHost);
};

.cpp 函数声明 - 错误“cit is undeclared identifier”

cit CC::_translateCommand(string &command)
{
}

【问题讨论】:

标签: c++ inheritance return-value typedef protected


【解决方案1】:

我也可以在函数中使用我的 typedef。

这里的关键词是within

我所说的唯一我不能做的就是将它作为返回值。

您的返回类型声明超出了函数的范围。在CR的范围之外,cit只能参考::cit。但是你没有定义类型::cit;你定义了CR::cit。因此,当您超出CR 的范围时,例如当您声明一个成员函数外联时,您必须显式解析范围:

CR::cit CC::_translateCommand(string &command)

尾随返回类型声明在函数范围内,因此如果您使用它,则不需要显式解析:

auto CC::_translateCommand(string &command) -> cit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2018-11-02
    • 2020-06-05
    • 2013-07-10
    • 2017-11-24
    • 2022-06-30
    • 1970-01-01
    相关资源
    最近更新 更多