簡體   English   中英

沒有運算符==與std :: string匹配?

[英]No operator== match for std::string?

我的程序遇到一個奇怪的問題。 所以在標題中我得到了這樣的東西:

#ifndef SET1_INCLUDED
#define SET1_INCLUDED

#include <iostream>
using namespace std;

typedef std::string ItemType;
class Set1{
  public:
  ------some public constructor and method in here-------
  private:
  ItemType setMember[100];
}

在Set1.cpp文件的函數的1部分中,我得到了以下內容:

if (setMember[i] == "foo") {exist = true;}

在這種情況下,我收到一條錯誤消息,內容為“未找到采用'ItemType'類型的左操作數的運算符”。 但是,如果我將typedef中的std :: string更改為int或unsigned long,並將“ foo”更改為某個隨機數,則代碼可以正常工作。 有什么建議嗎? 謝謝

您缺少頭文件<string> ,這意味着您沒有在程序中看到所有全局operator ==定義。 這很可能是您遇到的問題。

要解決此問題,請嘗試添加以下行:

#include <string>

希望這可以幫助!

您需要包括字符串頭文件,以將相對類型和運算符引入范圍。

#include <string>

注意:從頭文件的std名稱空間提取所有內容是一種不好的編碼習慣。

//using namespace std;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM