将Struct作为参数传递给函数时遇到问题。我已将结构定义为:
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:'message':未声明的标识符
C2065:'myMsg':未声明的标识符
C2182:'print_msg':非法使用'void'类型
答案 0 :(得分:3)
对我而言它是有效的,你把所有的东西放在同一个文件中吗? 这工作正常
#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);
}