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

error: [typedef inside a class] does not name a type

本文关键字:typedef 未命名 类型 错误      更新时间:2023-10-16

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

你只需要限定它:

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

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

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

m_array_type buffer_manager::get_recieve_buffer()

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

buffer_manager::m_array_type buffer_manager::get_recieve_buffer()