0

これは私のサンプルコードです:

#include <iostream>
#include <cstring>
#include <vector>
#include <iterator>
#include "MQmessage.h"

using namespace std;

int main()
{
    // declaring an array to store name/value pair
    struct Property  user_property[15];
    const char* const list[] = {"stateCode","errorCode"};
    const size_t len = sizeof(list) / sizeof(list[0]);
    for (size_t i = 0; i < len; ++i) {
        strcpy(user_property[0].name,list[i]);
    }
    for (size_t i = 0; i < len; ++i) {
        std::cout<<user_property[i]<<endl;
    }
    return 0;
}

コードで次のエラーが発生します:

std::coutの'operator<<'に一致しません

誰かが私が間違っていることを教えてもらえますか?

4

3 に答える 3

3

そうしないと、 の演算子std::cout<<user_property[i].name<<endlをオーバーロードする必要があります。<<Property

于 2012-10-09T07:04:29.690 に答える
1

operator<<のためにオーバーロードする必要がありますstruct Property

ちょうど出力したい場合Property::nameは、それも利用可能にするstd::string必要があることに注意してください。#include <string>operator<<std::string

于 2012-10-09T07:04:01.417 に答える
0

次のような「プロパティ」の印刷関数を追加するだけです。

void print()
{
    std::cout<<this->name<<endl;
    std::cout<<this->othermember<<endl;  //if you have some other members
}

そしてあなたのメインで次のことをしてください:

for (size_t i = 0; i < len; ++i) {
    user_property[i].print(); }
于 2012-10-09T07:21:05.270 に答える