【问题标题】:error: [typedef inside a class] does not name a type错误:[typedef inside a class] 没有命名类型
【发布时间】:2016-06-03 04:30:46
【问题描述】:

我已经实现了一个类buffer_manger。下面给出了头文件(.hpp)和(.cpp)文件。

buffer_manager.hpp

#ifndef BUFFER_MANAGER_H                                                                                                                                                                                           
#define BUFFER_MANAGER_H

#include <iostream>
#include <exception>
#include <boost/array.hpp>
#include <boost/algorithm/hex.hpp>
#include <algorithm>
#include <iomanip>


class buffer_manager
{
public:
    typedef boost::array<unsigned char, 4096> m_array_type;
    m_array_type recv_buf;
    buffer_manager();
    ~buffer_manager();
    std::string message_buffer(m_array_type &recv_buf);
    m_array_type get_recieve_array();

private:
  std::string message;
};

#endif //BUFFER_MANAGER_H

buffer_manager.cpp

#include <iostream>                                                                                                                                                                                                
#include <boost/array.hpp>
#include <boost/algorithm/hex.hpp>
#include <algorithm>
#include "buffer_manager.hpp"


buffer_manager::buffer_manager()
{

}
buffer_manager::~buffer_manager()
{

}
std::string buffer_manager::message_buffer(m_array_type &recv_buf)
{
    boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(message));
    return message;
}

m_array_type buffer_manager::get_recieve_buffer()
{
  return recv_buf;
}

问题是我在buffer_manager 类中定义了一个类型m_array_type。我还声明了一个名为recv_buf的变量@

我尝试为该成员变量实现访问器函数。我得到的错误是

buffer_manager.cpp:22:1: error: ‘m_array_type’ does not name a type
 m_array_type buffer_manager::get_recieve_buffer()

如何让 buffer_manager.cpp 识别类型m_array_type

【问题讨论】:

  • 函数定义中是buffer_mnager::m_array_type
  • 您还可以使用尾随返回类型来避免限定返回类型 - auto buffer_manager::get_recieve_buffer() -&gt; m_array_type
  • 你最近的错误是因为你在类定义中有get_recieve_array,在cpp文件中有get_recieve_buffer
  • @Praetorian - 啊谢谢 :(

标签: c++ c++11 boost


【解决方案1】:

你只需要限定它:

buffer_manager::m_array_type buffer_manager::get_recieve_buffer()
^^^^^^^^^^^^^^^^
{
    return recv_buf;
}

成员函数名之后的所有内容都将在类的上下文中查找,但不会在返回类型中查找。

作为旁注,您真的要按值返回它吗?也许m_array_type&amp;

【讨论】:

  • 你能看看上面的更新吗?似乎不起作用
  • @liv2hak 我猜你让定义返回一个引用,但声明返回一个值?
  • 对 buffer_manager.hpp 的修改表明我已经重新声明它以返回一个引用 buffer_manager::m_array_type &amp; get_recieve_array();
【解决方案2】:
m_array_type buffer_manager::get_recieve_buffer()

这里的问题是,当编译器看到m_array_type 时,它并不知道它正在编译一个成员函数。所以你必须告诉它该类型是在哪里定义的:

buffer_manager::m_array_type buffer_manager::get_recieve_buffer()

【讨论】:

  • 你能看到上面问题的更新吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 2013-09-09
  • 2015-12-01
  • 2014-12-16
相关资源
最近更新 更多