错误:[类中的typedef]没有命名类型

时间:2016-02-21 20:56:09

标签: c++ c++11 boost

我已经实现了一个类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;
}

问题是我已经定义了一个类型m_array_type insde类buffer_manager。我还声明了一个名为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

2 个答案:

答案 0 :(得分:8)

您只需要对其进行限定:

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

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

作为旁注,你真的想按价值归还吗?也许是m_array_type&

答案 1 :(得分:3)

m_array_type buffer_manager::get_recieve_buffer()

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

buffer_manager::m_array_type buffer_manager::get_recieve_buffer()