C++ 结构作为函数中的参数

C++ 结构作为函数中的参数,c++,function,pointers,struct,arguments,C++,Function,Pointers,Struct,Arguments,我在将结构作为参数传递给函数时遇到问题。我已将结构定义为: struct message{ static unsigned int last_id; unsigned int id; std::string msg; std::string timestamp; message(void) { } message(const std::string& recvbuf_msg,const std::string& a_

我在将结构作为参数传递给函数时遇到问题。我已将结构定义为:

struct message{
    static unsigned int last_id;
    unsigned int id;
    std::string msg;
    std::string timestamp;
    message(void)
    {
    }
    message(const std::string& recvbuf_msg,const std::string& a_timestamp) :
    msg(recvbuf_msg), timestamp(a_timestamp), id(++last_id)
  {
  }
};
功能定义如下:

void print_msg(message *myMsg,std::string recv_usrn){
    cout << "#" << myMsg->id << " @" << recv_usrn << ": " << myMsg->msg << " at " << myMsg->timestamp << endl;
}
message myMsg;
string recv_usrn;
print_msg(&myMsg,recv_usrn);
问题是它会产生以下错误:

C2065:“消息”:未声明的标识符

C2065:“myMsg”:未声明的标识符

C2182:“打印消息”:非法使用类型“void”


对我来说,它是有效的,你是否把所有的东西都放在同一个文件中。。 这很好用

#include <iostream>
#include <cstdio>
using namespace std;
struct message{
    static unsigned int last_id;
    unsigned int id;
    std::string msg;
    std::string timestamp;
    message(void)
    {
    }
    message(const std::string& recvbuf_msg,const std::string& a_timestamp) :
    msg(recvbuf_msg), timestamp(a_timestamp), id(++last_id)
  {
  }
};

void print_msg(message *myMsg,std::string recv_usrn){
    cout << "#" << myMsg->id << " @" << recv_usrn << ": " << myMsg->msg << " at " << myMsg->timestamp << endl;
}

int main()
{
message myMsg;
string recv_usrn;
print_msg(&myMsg,recv_usrn);
}
#包括
#包括
使用名称空间std;
结构消息{
静态无符号int-last_-id;
无符号整数id;
std::字符串msg;
std::字符串时间戳;
信息(无效)
{
}
消息(const std::string和recvbuf_msg,const std::string和a_时间戳):
msg(recvbuf_msg),时间戳(a_时间戳),id(++last_id)
{
}
};
无效打印消息(消息*myMsg,std::string recv\u usrn){

难道你忘了包含
消息的声明了吗?
?请发一条帖子。当我写“message myMsg”时,只是为了说明“myMsg”是message类型的stuct。它以前是在代码中填充的。要使用
消息
结构,你必须先定义它。它是在定义(实现)之前定义的吗在调用
print\u msg
之前定义
myMsg
变量之前,是否定义了
print\u msg
函数的结构?