编译时字符串数组错误

时间:2012-10-09 07:01:33

标签: c++ string

这是我的示例代码:

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

我在代码中遇到了以下错误:

  

与'operator&lt;&lt;'不匹配在std :: cout

有人能告诉我,我做错了什么吗?

3 个答案:

答案 0 :(得分:3)

我想你想要std::cout<<user_property[i].name<<endl,否则你将不得不重载<<的{​​{1}}运算符。

答案 1 :(得分:1)

您需要为operator<<重载struct Property

请注意,如果您只想输出Property::name并且std::string,则还需要#include <string>使operator<< std::string可用。

答案 2 :(得分:0)

只需添加“Property”的打印功能,如下所示:

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