2016-04-15 38 views
-2

構造体を引数として関数に渡すときに問題があります。私はこのような機能を使用しているメインで構造体への引数としての構造体

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); 

問題がある

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) 
    { 
    } 
}; 

と、このようなのfuctionの定義:私はと構造体を定義していますこれらのエラーは次のとおりです。

C2065: 'というメッセージ':宣言されていない識別子

C2065: 'myMsg':宣言されていない識別子

C2182: 'print_msg':私のための型 'のボイド'

+3

あなたが宣言0を含めることを忘れました ..あなたが同じファイルにすべてのものを入れている、働いていますf 'メッセージ'?投稿[MCVE]してください。 –

+0

私は 'メッセージmyMsg'を書いたときに、 'myMsg'がタイプメッセージの要点であることを明確にしていました。以前はコードの中に埋め込まれていました。 – 19mike95

+0

'message'構造体を使用するには、まずそれを定義しなければなりません。 'print_msg'関数の定義(実装)の前に定義されていますか? 'print_msg'を呼び出す前に' myMsg'変数を定義する前に構造体が定義されていますか? –

答えて

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); 
}