0

This is my sample code:

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

I am getting follwoing errors in the code:

no match for 'operator<<' in std::cout

Can someone tell me what is that I am doing wrong?

2
  • std::cout<<user_property[i].name<<endl; ??? Commented Oct 9, 2012 at 7:03
  • 1
    The main problem is as described by the answers below, but as an aside note: In the strcpy call you refer to user_property[0] independent of the value of i. Also, there are always 15 Property objects, independent of len. Finally, I hope the constructor of Property allocates sufficient space in name for the strcpy to work without problems. Commented Oct 9, 2012 at 7:12

3 Answers 3

3

I guess you want std::cout<<user_property[i].name<<endl, otherwise you'll have to overload the << operator of Property.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to overload operator<< for struct Property.

Please note, that if you want to output just Property::name and it is std::string you also need to #include <string> to make operator<< for std::string available.

1 Comment

If std::string is a member of Property the header will already be included. However, they're using strcpy so it probably isn't a std::string.
0

Just add a printing function for the "Property" like this one :

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

and in your main just do the following:

for (size_t i = 0; i < len; ++i) {
    user_property[i].print(); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.